def test_image_find_peaks(): print('test two points') input_pts = [[300, 400, 1], [300, 500, 1]] image_size = [800, 600] std = 50 heatmap, _, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) img = 1 - heatmap[:, :, -1] visualize_image(img, vis=True) peak_array, peak_global = image_find_peaks(img) CHECK_EQ_NUMPY(peak_array, np.array(input_pts).transpose()) visualize_image_with_pts(img, peak_array, vis=True) CHECK_EQ_NUMPY(peak_global, np.array([300, 400, 1]).reshape((3, 1))) print('test five points') input_pts = [[300, 350, 1], [300, 400, 1], [200, 350, 1], [280, 320, 1], [330, 270, 1]] image_size = [800, 600] std = 50 heatmap, _, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) img = 1 - heatmap[:, :, -1] visualize_image(img, vis=True) peak_array, peak_global = image_find_peaks(img) CHECK_EQ_NUMPY(peak_array, np.array(input_pts).transpose()) visualize_image_with_pts(img, peak_array, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def extract_images_from_video_opencv(video_file, save_dir, debug=True): ''' extract a list of images from a video file using opencv package Note that if the VideoCapture does not work, uninstall python-opencv and reinstall the newest version Parameters: video_file: a file path to a video file save_dir: a folder to save the images extracted from the video debug: boolean, debug mode to check format Returns: ''' if debug: assert is_path_exists(video_file), 'the input video file does not exist' mkdir_if_missing(save_dir) cap = cv2.VideoCapture(video_file) frame_id = 0 while(True): ret, frame = cap.read() if not ret: break save_path = os.path.join(save_dir, 'image%05d.png' % frame_id) visualize_image(frame, bgr2rgb=True, save_path=save_path) frame_id += 1 print('processing frame %d' % frame_id) cap.release() return
def test_image_draw_mask(): mask = '../rainbow.jpg' mask = Image.open(mask).convert('RGB') mask = image_resize(mask, target_size=(500, 500)) visualize_image(mask, vis=True) image_path = '../lena.png' print('test with pil image') img = Image.open(image_path).convert('RGB') masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) print('test with numpy image with different transparency') img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. img_bak = img.copy() masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask, transparency=0.9) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) masked_img += 1 assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_load_image(): image_path = '../lena.png' print('basic') img = load_image(image_path) assert img.shape == (512, 512, 3) print('testing for resizing') img = load_image(image_path, resize_factor=2.0) assert img.shape == (1024, 1024, 3) print('testing for resizing') img = load_image(image_path, target_size=[1033, 1033]) assert img.shape == (1033, 1033, 3) print('testing for rotation') img = load_image(image_path, input_angle=45) visualize_image(img, vis=True) assert img.shape == (726, 726, 3) print('testing for rotation') img = load_image(image_path, input_angle=450) visualize_image(img, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_hwc2chw(): print('test same channel image, should be the same') image_path = '../lena.jpg' img = np.array(Image.open(image_path).convert('RGB')) visualize_image(img[:, :, 0], vis=True) img_shape = img.shape chw_img = hwc2chw(img) visualize_image(chw_img[0, :, :], vis=True) print(chw_img.shape) print(img_shape) assert chw_img.shape[0] == img_shape[2] and chw_img.shape[1] == img_shape[ 0] and chw_img.shape[2] == img_shape[1] print('test different channel image, should not be the same') image_path = '../lena.jpg' img = np.array(Image.open(image_path).convert('RGB')) visualize_image(img[:, :, 0], vis=True) img_shape = img.shape chw_img = hwc2chw(img) visualize_image(chw_img[1, :, :], vis=True) print(chw_img.shape) print(img_shape) assert chw_img.shape[0] == img_shape[2] and chw_img.shape[1] == img_shape[ 0] and chw_img.shape[2] == img_shape[1] print('\n\nDONE! SUCCESSFUL!!\n')
def test_hist_equalization(): print('testing for gaussian distribution') random_data = np.random.normal(0.5, 0.1, 10000) visualize_distribution(random_data, vis=True) num_bins = 100 data_equalized = hist_equalization(random_data, num_bins=num_bins) visualize_distribution(data_equalized, vis=True) print('testing for image data') image_path = 'lena.jpg' img = np.array(Image.open(image_path).convert('L')) visualize_image(img, vis=True) num_bins = 256 data_equalized = hist_equalization(img, num_bins=num_bins) visualize_image(data_equalized, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def extract_images_from_video_opencv(video_file, save_dir, debug=True): ''' if the VideoCapture does not work, uninstall python-opencv and reinstall the newest version ''' if debug: assert is_path_exists(video_file), 'the input video file does not exist' mkdir_if_missing(save_dir) cap = cv2.VideoCapture(video_file) frame_id = 0 while(True): ret, frame = cap.read() if not ret: break save_path = os.path.join(save_dir, 'image%05d.png' % frame_id) visualize_image(frame, bgr2rgb=True, save_path=save_path) frame_id += 1 print('processing frame %d' % frame_id) cap.release()
def test_image_concatenate(): image_path = '../lena.png' image = np.array(Image.open(image_path).convert('RGB')) print('test a single image') target_size = [600, 600] image_concatenated = image_concatenate(image, target_size=target_size) visualize_image(image_concatenated, vis=True) print('test multiple images') image_list = [image, image, image, image, image, image, image] image_all = np.stack(image_list, axis=0) target_size = [1200, 1800] image_concatenated = image_concatenate(image_all, target_size=target_size) visualize_image(image_concatenated, vis=True) print('test multiple images with grid size') image_list = [image, image, image, image, image, image, image] image_all = np.stack(image_list, axis=0) target_size = [600, 3600] image_concatenated = image_concatenate(image_all, target_size=target_size, grid_size=[1, 7]) visualize_image(image_concatenated, vis=True) print('test multiple images with grid size') image_list = [image, image, image, image, image, image, image] image_all = np.stack(image_list, axis=0) target_size = [1800, 300] image_concatenated = image_concatenate(image_all, target_size=target_size, grid_size=[7, 1]) visualize_image(image_concatenated, vis=True) print('test multiple images with edge factor') image_list = [image, image, image, image, image, image, image] image_all = np.stack(image_list, axis=0) target_size = [1200, 1800] image_concatenated = image_concatenate(image_all, target_size=target_size, edge_factor=0.5) visualize_image(image_concatenated, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_gray2rgb(): image_path = '../lena.jpg' img = Image.open(image_path).convert('L') img = np.array(img) print('input grayscale image has dimension of: '), print(img.shape) assert isgrayimage(img), 'the input image is not a gray image' visualize_image(img, vis=True) img_rgb = gray2rgb(img, with_color=True) print('converted rgb image has dimension of: '), print(img_rgb.shape) assert iscolorimage(img_rgb), 'the converted image is not a color image' visualize_image(img_rgb, vis=True) # test when input image is float image test_floatimage = (img.astype('float32')) / 255. img_rgb = gray2rgb(test_floatimage, with_color=True) assert iscolorimage(img_rgb), 'the converted image is not a color image' visualize_image(img_rgb, vis=True) # test when input image is PIL image test_pil_format_image = Image.fromarray(img) img_rgb = gray2rgb(test_pil_format_image, with_color=True) assert iscolorimage(img_rgb), 'the converted image is not a color image' print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_draw_mask(): # mask = '../rainbow.jpg' mask = '/home/xinshuo/Dropbox/test7.png' mask = Image.open(mask).convert('RGB') # mask = image_resize(mask, target_size=(500, 500)) mask = image_resize(mask, target_size=(1148, 749)) visualize_image(mask, vis=True) # image_path = '../lena.png' image_path = '/home/xinshuo/test8.png' print('test with pil image') img = Image.open(image_path).convert('RGB') img = image_resize(img, target_size=(1148, 749)) print(img.shape) print(mask.shape) masked_img = image_draw_mask(img, mask, transparency=0.5) # visualize_image(img, vis=True) # visualize_image(masked_img, vis=True) save_image(masked_img, save_path='7716_new.png') print('test with numpy image with different transparency') img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. img_bak = img.copy() masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask, transparency=0.9) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) masked_img += 1 assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_rgb2bgr(): print('test input image as jpg') image_path = '../lena.jpg' img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) bgr_img = rgb2bgr(img) visualize_image(bgr_img, vis=True) print('test input image as png') image_path = '../lena.png' img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) bgr_img = rgb2bgr(img) visualize_image(bgr_img, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_chw2hwc(): print('test input image from hwc to chw to hwc') image_path = '../lena.png' img = np.array(Image.open(image_path).convert('RGB')) visualize_image(img[:, :, 0], vis=True) img_shape = img.shape chw_img = hwc2chw(img) visualize_image(chw_img[0, :, :], vis=True) hwc_img = chw2hwc(chw_img) visualize_image(hwc_img[:, :, 0], vis=True) assert hwc_img.shape == img_shape print('\n\nDONE! SUCCESSFUL!!\n')
def test_gaussian_filter(): image_path = '../lena.png' print('testing for grayscale image with Gaussian filter') img = Image.open(image_path).convert('L') filter = linear_filter() gaussian_kernel = filter.gaussian() filtered_img = filter.convolve(img) visualize_image(img, vis=True) visualize_image(filtered_img, vis=True) print('testing for color image with Gaussian filter') img = Image.open(image_path).convert('RGB') filter = linear_filter() gaussian_kernel = filter.gaussian() filtered_img = filter.convolve(img) visualize_image(img, vis=True) visualize_image(filtered_img, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_resize(): print('test input image as numpy uint8 image with resize_factor') img = (np.random.rand(400, 300, 1) * 255.).astype('uint8') resized_img = image_resize(img, resize_factor=0.5) assert resized_img.shape == (200, 150) image_path = '../lena.png' img = Image.open(image_path).convert('RGB') im_height, im_width = img.size visualize_image(img, vis=True) print('test input image as pil image') resized_img = image_resize(img, resize_factor=0.3) assert resized_img.shape == (int(round(im_height * 0.3)), int(round(im_width * 0.3)), 3) print('test input image as numpy float32 image with target_size') resized_img = image_resize(img, target_size=(1000, 1000)) assert resized_img.shape == (1000, 1000, 3) visualize_image(resized_img, vis=True) print( 'test input image as numpy float32 image with target_size and bilinear' ) resized_img = image_resize(img, target_size=(1000, 1000), interp='bilinear') assert resized_img.shape == (1000, 1000, 3) visualize_image(resized_img, vis=True) ######################################## test failure cases print('test random interp') try: resized_img = image_resize(img, target_size=(800, 600), interp='random') sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the interp is not correct') print('test both resize_factor and target_size') try: resized_img = image_resize(img, resize_factor=0.4, target_size=(800, 600), interp='random') sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the resize_factor and target_size coexist') print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_rotate(): print('test input image as numpy uint8 image') image_path = '../lena.png' img = np.array(Image.open(image_path).convert('RGB')).astype('uint8') visualize_image(img, vis=True) rotated_img = image_rotate(img, input_angle=45) visualize_image(rotated_img, vis=True) print('test input image with a out of general range rotation angle') visualize_image(img, vis=True) rotated_img = image_rotate(img, input_angle=720) visualize_image(rotated_img, vis=True) # ######################################## test failure cases print('test two input angles') try: rotated_img = image_rotate(img, input_angle=[45, 90]) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the rotation angle should be a scalar') except TypeError: print('the rotation angle should be a scalar') print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_pad_around(): image_path = '../lena.jpg' img = Image.open(image_path) print('test 2d matrix') np_data = (np.random.rand(3, 3) * 255.).astype('uint8') pad_rect = [0, 2, 1, 1] img_padded = image_pad_around(np_data, pad_rect=pad_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 6, 4), 'the padded image does not have a good shape' print('test 2d matrix with negative pad_rect') np_data = (np.random.rand(3, 3) * 255.).astype('uint8') pad_rect = [-1, 2, 1, 1] try: img_padded = image_pad_around(np_data, pad_rect=pad_rect, pad_value=10) except AssertionError: print('the pad rect should not include negative integers') print('test 2d matrix with float pad_rect') np_data = (np.random.rand(3, 3) * 255.).astype('uint8') pad_rect = [0, 0.5, 1, 1] try: img_padded = image_pad_around(np_data, pad_rect=pad_rect, pad_value=10) except AssertionError: print('the pad rect should not include floating number') visualize_image(img, vis=True) img_padded = image_pad_around(img, [50, 100, 150, 200], pad_value=150) visualize_image(img_padded, vis=True) img_padded = image_pad_around(img.convert('L'), [50, 100, 150, 200], pad_value=10) visualize_image(img_padded, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_sobel_filter(): image_path = '../lena.png' print('testing for grayscale image with sobel along x axis') img = Image.open(image_path).convert('L') filter = linear_filter() sobel_kernel = filter.sobel() filtered_img = filter.convolve(img) visualize_image(img, vis=True) visualize_image(filtered_img, vis=True) print('testing for grayscale image with sobel along y axis') img = Image.open(image_path).convert('L') filter = linear_filter() sobel_kernel = filter.sobel(axis='y') filtered_img = filter.convolve(img) visualize_image(img, vis=True) visualize_image(filtered_img, vis=True) print('testing for color image with sobel along X axis') img = np.array( Image.open(image_path).convert('RGB')).astype('float64') / 255. filter = linear_filter() sobel_kernel = filter.sobel(axis='y') sobel_kernel = filter.expand_3d() filtered_img = ndimage.filters.convolve(img, sobel_kernel) visualize_image(img, vis=True) visualize_image(filtered_img, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_generate_gaussian_heatmap(): print('test single point') input_pts = [300, 400, 1] image_size = [800, 600] std = 10 heatmap, mask, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 2) assert mask.shape == (1, 1, 2) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 1]]])) visualize_image(heatmap[:, :, 0], vis=True) print('test two points') input_pts = [[300, 400, 1], [400, 400, 1]] image_size = [800, 600] std = 10 heatmap, mask, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 1, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('test two points with invalid one') input_pts = [[300, 400, 1], [400, 400, -1]] image_size = [800, 600] std = 10 heatmap, mask, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 0, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('test two points with invisible one') input_pts = [[300, 400, 1], [-400, -400, 0]] image_size = [800, 600] std = 10 heatmap, mask, mask_visible = generate_gaussian_heatmap( input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 1, 1]]])) assert CHECK_EQ_NUMPY(mask_visible, np.array([[[1, 0, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('test two points with invisible and invalid') input_pts = [[300, 400, -1], [-400, -400, 0]] image_size = [800, 600] std = 10 heatmap, mask, mask_visible = generate_gaussian_heatmap( input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[0, 1, 1]]])) assert CHECK_EQ_NUMPY(mask_visible, np.array([[[0, 0, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
config = CocoConfig() dataset_coco = CocoDataset() dataset_coco.load_data(dataset_dir, 'val', year=year, auto_download=False) dataset_coco.prepare() # print(dataset_coco.image_info[3]['id']) masks, class_ids = dataset_coco.load_mask(0) print(masks.shape) print(masks.dtype) print(class_ids.shape) print(class_ids.dtype) print(class_ids) img = dataset_coco.load_image(0) save_image(img, save_path='img.jpg') visualize_image(masks[:, :, -1], save_path='test.jpg') image = dataset_coco.load_image(0) # print(dataset_coco.image_ids) print(dataset_coco.num_classes) print(dataset_coco.source_class_ids) # zxc # Data generators train_set = Mask_RCNN_Dataset(dataset_coco, config, augment=True) train_generator = torch.utils.data.DataLoader(train_set, batch_size=1, shuffle=True, num_workers=1, pin_memory=False)
def test_crop_center(): image_path = '../lena.jpg' img = Image.open(image_path).convert('RGB') #################################### test with 4 elements in center_rect ######################################### print('test 2d matrix intersected on the top left') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [1, 2, 4, 6] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) print(img_padded.shape) assert img_padded.shape == ( 6, 4), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[-1, -1, 4, 6]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 3, 5]])) print('test 2d matrix intersected on the bottom right') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [3, 3, 5, 6] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 6, 5), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[1, 0, 5, 6]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[1, 0, 4, 5]])) print('test with color image, clipped on the left') center_rect = [0, 50, 100, 100] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img, center_rect=center_rect, pad_value=100) assert CHECK_EQ_NUMPY(crop_bbox, np.array([[-50, 0, 100, 100]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 50, 100]])) visualize_image(img, vis=True) visualize_image(img_cropped, vis=True) #################################### test with 2 elements in center_rect ######################################### print('test 2d matrix - basic') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [3, 3] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) print(img_padded.shape) assert img_padded.shape == ( 3, 3), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[1, 1, 3, 3]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[1, 1, 3, 3]])) print('test 2d matrix - boundary') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [5, 5] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 5, 5), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[0, 0, 5, 5]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 5, 5]])) print('test 2d matrix - intersected') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [7, 7] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 7, 7), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[-1, -1, 7, 7]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 5, 5]])) print('test with color image, clipped on the center') center_rect = [100, 100] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img, center_rect=center_rect, pad_value=100) visualize_image(img, vis=True) visualize_image(img_cropped, vis=True) print('test with color image, interesected') center_rect = [600, 600] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img, center_rect=center_rect, pad_value=100) visualize_image(img, vis=True) visualize_image(img_cropped, vis=True) print('test with grayscale image') center_rect = [100, 100] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img.convert('L'), center_rect=center_rect, pad_value=100) visualize_image(img.convert('L'), vis=True) visualize_image(img_cropped, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_hist_equalization(): image_path = '../lena.png' print('testing for grayscale pil image') img = Image.open(image_path).convert('L') visualize_image(img, vis=True) data_equalized = image_hist_equalization_hsv(img) visualize_image(data_equalized, vis=True) print('testing for grayscale numpy image') img = np.array(Image.open(image_path).convert('L')) visualize_image(img, vis=True) data_equalized = image_hist_equalization(img) visualize_image(data_equalized, vis=True) print('testing for color pil image') img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) data_equalized = image_hist_equalization_hsv(img) visualize_image(data_equalized, vis=True) print('testing for color numpy image') img = np.array(Image.open(image_path).convert('RGB')) visualize_image(img, vis=True) data_equalized = image_hist_equalization(img) visualize_image(data_equalized, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_hist_equalization(): image_path = '../lena.png' print('testing for grayscale pil image') img = Image.open(image_path).convert('L') visualize_image(img, vis=True) data_equalized = image_clahe(img) visualize_image(data_equalized, vis=True) print('testing for color pil image') img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) data_equalized = image_clahe(img) visualize_image(data_equalized, vis=True) print( 'testing for color pil image with large grid. The pixel value will not be interpolated very well' ) img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) data_equalized = image_clahe(img, grid_size=100) visualize_image(data_equalized, vis=True) print( 'testing for color pil image without limited contrast. The noise will be amplified' ) img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) data_equalized = image_clahe(img, clip_limit=1000000) visualize_image(data_equalized, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_hsv2rgb(): print('test input image as jpg') image_path = '../lena.jpg' img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) hsv_img = rgb2hsv(img) visualize_image(hsv_img, vis=True) rgb_img = hsv2rgb(hsv_img) visualize_image(rgb_img, vis=True) print('test input image as png') image_path = '../lena.png' img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) hsv_img = rgb2hsv(img) visualize_image(hsv_img, vis=True) rgb_img = hsv2rgb(hsv_img) visualize_image(rgb_img, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_rgb2hsv(): image_path = '../lena.png' # test for rgb pil image img = Image.open(image_path).convert('RGB') visualize_image(img, vis=True) hsv_img = rgb2hsv(img) visualize_image(hsv_img, vis=True) # test for rgb numpy image img = np.array(Image.open(image_path).convert('RGB')) visualize_image(img, vis=True) hsv_img = rgb2hsv(img) visualize_image(hsv_img, vis=True) # test for rgb numpy float image img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. visualize_image(img, vis=True) hsv_img = rgb2hsv(img) visualize_image(hsv_img, vis=True) # test for gray pil image img = Image.open(image_path).convert('L') visualize_image(img, vis=True) try: hsv_img = rgb2hsv(img) visualize_image(hsv_img, vis=True) except AssertionError: print('the function does not work when the input is not a rgb image') print('\n\nDONE! SUCCESSFUL!!\n')
def preprocess_image_caffe(image_datalist, debug=True, vis=False): ''' this function preprocesses image for caffe only, including transfer from rgb to bgr from HxWxC to NxCxHxW ''' if debug: print( 'debug mode is on during preprocessing. Please turn off after debuging' ) assert islist(image_datalist), 'input is not a list of image' assert all( isimage(image_data, debug=debug) for image_data in image_datalist), 'input is not a list of image' shape_list = [image_data.shape for image_data in image_datalist] assert CHECK_EQ_LIST_SELF( shape_list), 'image shape is not equal inside one batch' data_warmup = image_datalist[0] if iscolorimage(data_warmup, debug=debug): color = True elif isgrayimage(data_warmup, debug=debug): color = False if data_warmup.ndim == 2: image_datalist = [ np.reshape(image_data, image_data.shape + (1, )) for image_data in image_datalist ] else: assert False, 'only color or gray image is supported' if isuintimage(data_warmup, debug=debug): uint = True elif isfloatimage(data_warmup, debug=debug): uint = False else: assert False, 'only uint8 or float image is supported' if debug: if color: assert all( iscolorimage(image_data, debug=debug) for image_data in image_datalist), 'input should be all color image format' else: assert all( isgrayimage(image_data, debug=debug) and image_data.ndim == 3 and image_data.shape[-1] == 1 for image_data in image_datalist), 'input should be all grayscale image format' if uint: assert all( isuintimage(image_data, debug=debug) for image_data in image_datalist), 'input should be all color image format' else: assert all( isfloatimage(image_data, debug=debug) for image_data in image_datalist), 'input should be all grayscale image format' batch_size = len(image_datalist) caffe_input_data = np.zeros((batch_size, ) + image_datalist[0].shape, dtype=data_warmup.dtype) # fill one minibatch data index = 0 for image_data in image_datalist: caffe_input_data[index, :, :, :] = image_data index += 1 if color: caffe_input_data = caffe_input_data[:, :, :, [ 2, 1, 0 ]] # from rgb to bgr, currently [batch, height, weight, channels] if debug: if vis: # visualize swapped channel print( 'visualization in debug mode is on during preprocessing. Please turn off after confirmation' ) for index in xrange(caffe_input_data.shape[0]): image_tmp_swapped = caffe_input_data[index] print( '\n\nPlease make sure the image is not RGB after swapping channel' ) visualize_image(image_tmp_swapped, debug=debug) assert caffe_input_data.shape[-1] == 3 or caffe_input_data.shape[ -1] == 1, 'channel is not correct' caffe_input_data = np.transpose( caffe_input_data, (0, 3, 1, 2)) # permute to [batch, channel, height, weight] if debug: assert caffe_input_data.shape[1] == 3 or caffe_input_data.shape[ 1] == 1, 'channel is not correct' return caffe_input_data