def display_diff(im1, im2): im_diff = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY) - cv2.cvtColor( im2, cv2.COLOR_BGR2GRAY) dip.figure() dip.imshow(im_diff) dip.title('Difference image') dip.show()
def show_image(IMG, IMG_DCT, IMG_DIFF, IMG_RECONS): '''Function to display image''' IMG = floater(IMG) IMG_DCT = floater(IMG_DCT) IMG_DIFF = floater(IMG_DIFF) IMG_RECONS = floater(IMG_RECONS) dip.figure() dip.subplot(2, 2, 1) dip.imshow(IMG, 'gray') dip.title('Grayscale Image', fontsize='x-small') dip.subplot(2, 2, 2) dip.imshow(IMG_DCT, 'gray') dip.title('DCT of Image', fontsize='x-small') dip.subplot(2, 2, 3) dip.imshow(IMG_DIFF) dip.colorbar() dip.title('Error image', fontsize='x-small') dip.subplot(2, 2, 4) dip.imshow(IMG_RECONS, 'gray') dip.title('Reconstructed Image', fontsize='x-small') dip.show()
def noise_estimation(im: np.ndarray): """ Given an image, this function determines the variance in its pixel values and displays a histogram of the image along with a fitted normal distribution. """ # Calculate histogram im_hist, bin_edges = np.histogram(im, 30) bin_centers = bin_edges[:-1] + (np.diff(bin_edges) / 2) # Normalize histogram to make a PDF im_hist = im_hist.astype(float) im_hist /= np.sum(im_hist) # Calculate the mean and variance values mean = np.sum(im_hist * bin_centers) var = np.sum(im_hist * (bin_centers**2)) - (mean**2) # Calculate the values on the normal distribution PDF norm_vals = np.exp(-((bin_centers - mean) ** 2) / (2 * var)) \ / np.sqrt(2 * np.pi * var) # Normalize the norm_vals norm_vals /= np.sum(norm_vals) # Rescale variance var /= (255**2) print('Variance: {}'.format(var)) dip.figure() dip.bar(bin_centers, im_hist) dip.plot(bin_centers, norm_vals, 'r') dip.legend(['Fitted Gaussian PDF', 'Histogram of image']) dip.xlabel('Pixel value') dip.ylabel('Occurrence') dip.show()
def display_images(images, text): num = len(images) dip.figure() for i in range(num): dip.subplot(1, num, i + 1) dip.title(text[i]) dip.imshow(images[i]) dip.show()
def _test_noise_mode(self, file, deg_type): fh = ImageFileHandler() im = fh.open_image_file_as_matrix(file) degraded_im = self.degrade(im, degradation_type=deg_type) dip.figure() dip.subplot(121) dip.imshow(im, cmap="gray") dip.subplot(122) dip.imshow(degraded_im, cmap="gray") dip.xlabel("PSNR: {0:.2f}".format(dip.PSNR(im, degraded_im))) dip.show()
def dft(args): '''Compute and show DFT of image''' X = basic_image_ip(IMAGE_PATH, args) if args.verbose: print (X.shape) fX = dip.fft2(X) fX = dip.fftshift(fX) fX = np.log(np.abs(fX)) if args.verbose: print ("Done with FFT") dip.imshow(fX) dip.show()
def float_image_op(IMG, save_path, args, downsample = True): '''Function to display and save an float image''' IMG = dip.float_to_im(IMG/255) if downsample: save_path = os.path.join (SAVE_PATH, DOWNSAMPLE_DIR,save_path) else: save_path = os.path.join (SAVE_PATH, UPSAMPLE_DIR,save_path) dip.image_io.im_write(IMG, save_path) if args.verbose: if downsample: print ("Downsampled Image is saved") else: print ("Upsampled Image is saved") dip.imshow(IMG) dip.show()
def display_image(IMAGES): '''Display the Images and save''' IMAGES = [dip.float_to_im(IMG / 255) for IMG in IMAGES] dip.figure() dip.subplot(2, 2, 1) dip.imshow(IMAGES[0], 'gray') dip.title('Airplane Original Image') dip.subplot(2, 2, 2) dip.imshow(IMAGES[1], 'gray') dip.title('Brussels Original Image') dip.subplot(2, 2, 3) dip.imshow(IMAGES[2], 'gray') dip.title('Reconstructed Airplane Image') dip.subplot(2, 2, 4) dip.imshow(IMAGES[3], 'gray') dip.title('Reconstructed Brussels Image') dip.show()
def _test_restore_mode(self, file, deg_type, save_images=False, name=None): fh = ImageFileHandler() im_degrader = ImageDegrader() im = fh.open_image_file_as_matrix(file) degraded_im = im_degrader.degrade(im, degradation_type=deg_type, severity_value=.5) restored_im, clustered_im, h_params = self.multiplicative_clustering_restore( degraded_im) restored_im_2 = self.fast_multiplicative_restore( degraded_im, h_param=np.mean(h_params), search_window_size=21) if save_images: dip.im_write(dip.float_to_im(degraded_im), "./" + name + "_degraded_image.jpg", quality=95) dip.im_write(dip.float_to_im(restored_im), "./" + name + "_restored_image.jpg", quality=95) dip.im_write(dip.float_to_im(clustered_im), "./" + name + "_clustered_image.jpg", quality=95) dip.figure() dip.subplot(141) dip.imshow(im, cmap="gray") dip.subplot(142) dip.imshow(degraded_im, cmap="gray") dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format( dip.PSNR(im, degraded_im), dip.SSIM(im, degraded_im)[0])) dip.subplot(143) dip.imshow(restored_im, cmap="gray") dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format( dip.PSNR(im, restored_im), dip.SSIM(im, restored_im)[0])) dip.subplot(144) dip.imshow(restored_im_2, cmap="gray") dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format( dip.PSNR(im, restored_im_2), dip.SSIM(im, restored_im_2)[0])) dip.show()
def display_image(args, IMAGES): '''Display a float image and save''' if args.verbose: msg = bcolors.OKBLUE + "Displaying and saving the image" + bcolors.ENDC name = "lena_interp.png" IMAGES = [dip.float_to_im(IMG / 255) for IMG in IMAGES] dip.figure() dip.subplot(2, 2, 1) dip.imshow(IMAGES[0], 'gray') dip.title('Original Image') dip.subplot(2, 2, 2) dip.imshow(IMAGES[1], 'gray') dip.title('Interpolated Image') dip.subplot(2, 2, 3) dip.imshow(IMAGES[2], 'gray') dip.title('Reconstructed Image') dip.subplot(2, 2, 4) dip.imshow(IMAGES[3], 'gray') dip.title('Difference Image') dip.show()
#(c) Reading an image X = dip.im_read(picture_link) #(d) Converting the image to normalized floating point space X = dip.im_to_float(X) X *= 255 #(e) Adding Constant to Image Y = X + 75 #(f) Renormalize the image and covert to integer Y = dip.float_to_im(Y/255) #(g) Writing an image to disk dip.im_write(Y, save_link_1) #(h) Square Intenstiy and write image to disk Z = X**2 Z = dip.float_to_im(Z/255) Z = dip.im_write(Z, save_link_2) #(i) Compute FFT of X fX = dip.fft2(X) fX = dip.fftshift(fX) fX = np.log(np.abs(fX)) #(j) Save and show the resulting spectrum dip.imshow(fX) dip.show()
def run_optical_flow(filepath_ind: int, OF_alg: int, param: int=100, display: bool=True): frame_1 = dip.im_read(filepaths[filepath_ind] + 'frame1.png')[:, :, :3] frame_2 = dip.im_read(filepaths[filepath_ind] + 'frame2.png')[:, :, :3] residual = np.abs(frame_1.astype(float) - frame_2.astype(float)) \ .astype(np.uint8) frame_1_gray = dip.rgb2gray(frame_1) frame_2_gray = dip.rgb2gray(frame_2) PSNR_val = dip.PSNR(frame_1_gray, frame_2_gray) if display: # Plot the initial images dip.figure() dip.subplot(1, 3, 1) dip.imshow(frame_1) dip.title('Frame 1', fontsize='x-small') dip.subplot(1, 3, 2) dip.imshow(frame_2) dip.title('Frame 2', fontsize='x-small') dip.subplot(1, 3, 3) dip.imshow(residual) dip.title('Residual - PSNR: {:.2f} dB'.format(PSNR_val), fontsize='x-small') # Convert to grayscale for analysis frame_1 = dip.im_to_float(frame_1_gray) frame_2 = dip.im_to_float(frame_2_gray) start_time = default_timer() # ============================ EDIT THIS PART ============================= mask_x = np.array([[-1, 1], [-1,1]]) mask_y = np.array([[-1, -1], [1,1]]) mask_t_2 = np.array([[-1, -1], [-1,-1]]) mask_t_1 = np.array([[1, 1], [1,1]]) dIx = dip.convolve2d(frame_1, mask_x, mode='same', like_matlab=True) dIy = dip.convolve2d(frame_1, mask_y, mode='same', like_matlab=True) dIt = dip.convolve2d(frame_1, mask_t_1, mode='same', like_matlab=True) + dip.convolve2d(frame_2, mask_t_2, mode='same', like_matlab=True) # ==========!!!!! DO NOT EDIT ANYTHING BELOW THIS !!!!!==================== # Instantiate blank u and v matrices u = np.zeros_like(frame_1) v = np.zeros_like(frame_1) if 0 == OF_alg: print('The optical flow is estimated using Horn-Schuck...') u, v = horn_schuck(u, v, dIx, dIy, dIt, param) elif 1 == OF_alg: print('The optical flow is estimated using Lucas-Kanade...') u, v = lucas_kanade(u, v, dIx, dIy, dIt, param) else: raise ValueError('OF_alg must be either 0 or 1') end_time = default_timer() # Determine run time duration = end_time - start_time clock = [int(duration // 60), int(duration % 60)] print('Flow estimation time was {} minutes and {} seconds' .format(*clock)) # Downsample for better visuals stride = 10 m, n = frame_1.shape x, y = np.meshgrid(range(n), range(m)) x = x.astype('float64') y = y.astype('float64') # Downsampled u and v u_ds = u[::stride, ::stride] v_ds = v[::stride, ::stride] # Coords for downsampled u and v x_ds = x[::stride, ::stride] y_ds = y[::stride, ::stride] # Estimated flow estimated_flow = np.stack((u, v), axis=2) # Read file for ground truth flow ground_truth_flow = read_flow_file(filepaths[filepath_ind] + 'flow1_2.flo') u_gt_orig = ground_truth_flow[:, :, 0] v_gt_orig = ground_truth_flow[:, :, 1] u_gt = np.where(np.isnan(u_gt_orig), 0, u_gt_orig) v_gt = np.where(np.isnan(v_gt_orig), 0, v_gt_orig) # Downsampled u_gt and v_gt u_gt_ds = u_gt[::stride, ::stride] v_gt_ds = v_gt[::stride, ::stride] if display: # Plot the optical flow field dip.figure() dip.subplot(2, 2, 1) dip.imshow(frame_2, 'gray') dip.quiver(x_ds, y_ds, u_ds, v_ds, color='r') dip.title('Estimated', fontsize='x-small') dip.subplot(2, 2, 2) dip.imshow(frame_2, 'gray') dip.quiver(x_ds, y_ds, u_gt_ds, v_gt_ds, color='r') dip.title('Ground truth', fontsize='x-small') # Draw colored velocity flow maps dip.subplot(2, 2, 3) dip.imshow(flow_to_color(estimated_flow)) dip.title('Estimated', fontsize='x-small') dip.subplot(2, 2, 4) dip.imshow(flow_to_color(ground_truth_flow)) dip.title('Ground truth', fontsize='x-small') # Normalization for metric computations normalize = lambda im: (im - np.min(im)) / (np.max(im) - np.min(im)) un = normalize(u) un_gt = normalize(u_gt) un_gt[np.isnan(u_gt_orig)] = 1 vn = normalize(v) vn_gt = normalize(v_gt) vn_gt[np.isnan(v_gt_orig)] = 1 # Error calculations and displays EPE = ((un - un_gt) ** 2 + (vn - vn_gt) ** 2) ** 0.5 AE = np.arccos(((un * un_gt) + (vn * vn_gt) + 1) / (((un + vn + 1) * (un_gt + vn_gt + 1)) ** 0.5)) EPE_nan_ratio = np.sum(np.isnan(EPE)) / EPE.size AE_nan_ratio = np.sum(np.isnan(AE)) / AE.size EPE_inf_ratio = np.sum(np.isinf(EPE)) / EPE.size AE_inf_ratio = np.sum(np.isinf(AE)) / AE.size print('Error nan ratio: EPE={:.2f}, AE={:.2f}' .format(EPE_nan_ratio, AE_nan_ratio)) print('Error inf ratio: EPE={:.2f}, AE={:.2f}' .format(EPE_inf_ratio, AE_inf_ratio)) EPE_avg = np.mean(EPE[~np.isnan(EPE)]) AE_avg = np.mean(AE[~np.isnan(AE)]) print('EPE={:.2f}, AE={:.2f}'.format(EPE_avg, AE_avg)) if display: dip.show() return clock, EPE_avg, AE_avg