def process_night_image(img, img_out=None): # inc_contrast = ImageProcessor(img).change_constrast() grey = ImageProcessor(img).convert_to_greyscale() bw = ImageProcessor(grey).convert_to_bw_given_thresh(128) if img_out: cv2.imwrite(img_out, bw) return bw
def worker_super_resolution_one(image, new_heigth=None, new_width=None, save_filename="huey_SR_test"): current_heigth, current_width = image.shape[:2] resized_image = image if new_heigth is not None: if new_width is not None: resized_image = cv2.resize(image, (new_width, new_heigth)) else: resized_image = cv2.resize(image, (current_width, new_heigth)) else: if new_width is not None: resized_image = cv2.resize(image, (new_width, current_heigth)) logging.info("Image has been resized from {} to {}".format( image.shape, resized_image.shape)) processor = ImageProcessor() model = get_model_by_name("super_resolution_model") new_image = processor.process_image(image=resized_image, model=model, overlay=20) cv2.imwrite("./processed_images/{}.png".format(save_filename), new_image) return new_image
def process_map_image(img, img_out=None): roads = ImageProcessor(img).get_roads_in_map() roads_grey = ImageProcessor(roads).convert_to_greyscale() roads_bw = ImageProcessor(roads_grey).convert_to_bw_given_thresh(128) if img_out: cv2.imwrite(img_out, roads_bw) return roads_bw
def __init__(self, dataset_name, img_res=(224, 224), processing_type=Processes.SUPER_RESOLUTION): self.dataset_name = dataset_name self.img_res = img_res self.processing_type = processing_type self.image_processor = ImageProcessor()
def worker_general(image, model_name, save_filename="bad_filename"): model = get_model_by_name(model_name) processor = ImageProcessor() new_image = processor.process_image(image=image, model=model, overlay=20) cv2.imwrite("./processed_images/{}.png".format(save_filename), new_image) return new_image
def test_split_image(self): test_image = cv2.imread("./test_data/test_image.jpg") proc = ImageProcessor() results = proc._split_image(deepcopy(test_image), overlay=20) self.assertEqual(len(results), 3) self.assertEqual(len(results[0]), 4) self.assertEqual(results[0][1][0], 1) self.assertEqual(results[0][1][1], 0)
def test_merge_image(self): test_image = cv2.imread("./test_data/test_image.jpg") proc = ImageProcessor() results = proc._split_image(deepcopy(test_image), overlay=20) new_image = proc._recreate_image(results[0], 224, 224, results[1], results[2]) self.assertEqual(test_image[0, 0, 0], new_image[0, 0, 0]) self.assertEqual(test_image[223, 223, 2], new_image[223, 223, 2]) self.assertEqual(test_image[223, 0, 2], new_image[223, 0, 2]) self.assertEqual(test_image[0, 223, 2], new_image[0, 223, 2])
def test_image_processing(self): test_image = cv2.imread("./test_data/test_image.jpg")[:224, :224, :] proc = ImageProcessor() name = "super_resolution_model" with open(join(ROOT_DIR, 'GAN/Models/{}.json'.format(name)), 'r') as json_file: model = model_from_json(json_file.read()) model.load_weights(join(ROOT_DIR, "GAN/Models/{}.h5".format(name))) model._make_predict_function() result = proc.process_image(image=test_image, model=model, overlay=20) processed_test_image = cv2.imread( "./test_data/test_image_processed.png") self.assertListEqual(list(result[100][100]), list(processed_test_image[100][100]))
def process_images(argv): ap = argparse.ArgumentParser() ap.add_argument("--map_image", help="path to the map image") ap.add_argument("--night_image", help="path to night image") ap.add_argument("--output", help="path to store output image") ap.add_argument("--background", help="background image to overlay final result on") args = vars(ap.parse_args()) night_img = cv2.imread(args["night_image"]) map_img = cv2.imread(args["map_image"]) output_night_img = "night_image_bw.png" output_map_img = "map_image_bw.png" night_img_bw = process_night_image(night_img, output_night_img) map_img_bw = process_map_image(map_img, output_map_img) # try to account for skew in night_img_bw # - shift image by 5 pixels in each direction # - compute bitwise_and of each of these images with the original image rows, cols = night_img_bw.shape M_list = [ np.float32([[1, 0, 0], [0, 1, -5]]), # shift up np.float32([[1, 0, 0], [0, 1, 5]]), # shift down np.float32([[1, 0, -5], [0, 1, 0]]), # shift left np.float32([[1, 0, 5], [0, 1, 0]]) # shift right ] shift_result_list = [] for M in M_list: night_img_shift = cv2.warpAffine(night_img_bw, M, (cols, rows)) shift_result_list.append( cv2.bitwise_and(map_img_bw, cv2.bitwise_not(night_img_shift)) ) # map_img_bw AND (NOT night_img_bw) # - take shift_result_list also into account to compute result result = cv2.bitwise_and(map_img_bw, cv2.bitwise_not(night_img_bw)) for r in shift_result_list: result = cv2.bitwise_and(result, r) # convert white to blue result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR) result[np.where((result == [255, 255, 255]).all(axis=2))] = [0, 0, 255] # convert black to transparent result = cv2.cvtColor(result, cv2.COLOR_BGR2RGBA) result[np.all(result == [0, 0, 0, 255], axis=2)] = [0, 0, 0, 0] result = cv2.cvtColor(result, cv2.COLOR_RGBA2RGB) # overlay result on map image background = cv2.imread(args['background']) final_image = ImageProcessor(background).blend_non_transparent(result) cv2.imwrite(args['output'], final_image)
def test_reduce_yellow(self): proc = ImageProcessor() test_image = [[ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ]] test_image = np.asarray(test_image, dtype='uint8') new_image = proc._reduce_yellow(deepcopy(test_image), 1) self.assertEqual(test_image[0, 0, 0] - 1, new_image[0, 0, 0]) self.assertEqual(test_image[0, 0, 1] - 1, new_image[0, 0, 1]) self.assertEqual(test_image[0, 0, 2], new_image[0, 0, 2])
parser.add_option('--input', dest='input', action='store', default='', type='str', help='путь к *.ppm или *.pgm файлу') parser.add_option('--output', dest='output', action='store', default='', type='str', help='путь к файлу, который следует записать.') parser.add_option('--encode', dest='encode', action='store_true', default=False, help='режим кодирования') parser.add_option('--decode', dest='decode', action='store_true', default=False, help='режим декодирования') parser.add_option('--times', dest='times', action='store', default=1, type='int', help='сколько раз выполнить преобразование') options, args = parser.parse_args() filename = ''.join(options.input.split('.')[:-1]) type = options.input.split('.')[-1] proc = ImageProcessor() try: if options.encode or options.decode: if options.encode: image = proc.read(options.input) result = proc.haar_encode(image, options.times) else: image = proc.read(options.input, compressed=True) result = proc.haar_decode(image, options.times) if not options.output: filename_output = filename + '_tmp.' + type else: filename_output = options.output
class DataLoader: def __init__(self, dataset_name, img_res=(224, 224), processing_type=Processes.SUPER_RESOLUTION): self.dataset_name = dataset_name self.img_res = img_res self.processing_type = processing_type self.image_processor = ImageProcessor() def load_data(self, batch_size=1, is_testing=False): path = glob(self.dataset_name + "/*.jpg") batch_images = np.random.choice(path, size=batch_size) good_imgs = [] bad_imgs = [] for img_path in batch_images: img = self.imread(img_path) h, w = self.img_res good_img = cv2.resize(img, self.img_res) bad_img = good_img if self.processing_type is Processes.SUPER_RESOLUTION: resize_factor = random.randint(2, 4) low_h, low_w = int(h / resize_factor), int(w / resize_factor) bad_img = cv2.resize(bad_img, (low_h, low_w)) bad_img = cv2.resize(bad_img, self.img_res) elif self.processing_type is Processes.DEBLUR: blur_kernel_size = random.randint(1, 4) * 2 + 1 bad_img = cv2.blur(good_img, (blur_kernel_size, blur_kernel_size)) # elif self.processing_type is Processes.COLORIZE: # # bad_img = rgb2gray(bad_img) # bad_img = gray2rgb(bad_img) # elif self.processing_type is Processes.STYLE_TRANSFER: # # bad_img = np.array(bad_img, dtype=np.uint8) # bad_img = cv2.cvtColor(bad_img, cv2.COLOR_RGB2BGR) elif self.processing_type is Processes.DENOISE: noise_amount = random.randint(0, 15) bad_img = self.image_processor.add_noise(image=bad_img, ammount=noise_amount) else: logging.error("Not a valid processing operation") if not is_testing and np.random.random() < 0.5: good_img = np.fliplr(good_img) bad_img = np.fliplr(bad_img) good_imgs.append(np.asarray(good_img, "int16")) bad_imgs.append(np.asarray(bad_img, "int16")) imgs_hr = np.array(good_imgs) # / 127.5 - 1. imgs_lr = np.array(bad_imgs) # / 127.5 - 1. return imgs_hr, imgs_lr def imread(self, path): return cv2.imread(path).astype(np.float) def imread_gray(self, path): return cv2.imread(path, 0).astype(np.float)
dest='output', action='store', default='', type='str', help='путь к файлу, который следует записать.') parser.add_option('--kernel', dest='kernel', action='store', default=None, help='Путь к текстовому файлу с ядром.') options, args = parser.parse_args() filename = ''.join(options.input.split('.')[:-1]) type = options.input.split('.')[-1] proc = ImageProcessor() try: image = proc.read(options.input) with open(options.kernel, 'r') as f: kernel = [[float(value) for value in line.split()] for line in f.readlines()] result = proc.convolve(image, kernel) result = proc.crop_image(result, top=len(kernel), left=len(kernel)) if not options.output: filename_output = filename + '_tmp.' + type else: filename_output = options.output
else: with open('E:/Licenta/Licenta/GAN/Models/super_resolution_model.json', 'r') as json_file: loaded_model = model_from_json(json_file.read()) loaded_model.load_weights( "E:/Licenta/Licenta/GAN//Models/super_resolution_model.h5") # loaded_model.summary() global model_graph model_graph = tf.get_default_graph() # Test on image processor = ImageProcessor() image = cv2.imread("E:/AiDatasets/Licenta/Test/data/test1.jpg") cv2.imwrite("E:/AiDatasets/Licenta/Test/results/test1_3_original.jpg", image) img_h, img_w, _ = image.shape image = cv2.resize(image, (img_w // 4, img_h // 4)) image = cv2.resize(image, (img_w, img_h)) cv2.imwrite("E:/AiDatasets/Licenta/Test/results/test1_1_downRes.jpg", image) new_image = processor.process_image(image=image, model=loaded_model, model_graph=model_graph,
dest='compress', action='store_true', default=False, help='используйте этот флаг, если нужно сжать файл') parser.add_option('--window', dest='window_size', action='store', default=64, type='int', help='размер окна для сжатия LZ77') options, args = parser.parse_args() filename = ''.join(options.input.split('.')[:-1]) type = options.input.split('.')[-1] proc = ImageProcessor() try: if options.compressor: compressor_name = options.compressor.lower() if options.compress: image = proc.read(options.input) print(image.to_matrix().shape) if compressor_name == 'rle': print('Сжатие изображения при помощи алгоритма RLE.') result = proc.compress_rle(image) elif compressor_name == 'lz77': print('Сжатие изображения при помощи алгоритма LZ77.') result = proc.compress_lz77(image, options.window_size) elif compressor_name == 'huffman': print('Сжатие изображения при помощи алгоритма Хаффмана.')