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 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 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()
dip.float_to_im(extEdgeMinorX / 255)) PSNR_mild = dip.metrics.PSNR(dip.float_to_im(X / 255), dip.float_to_im(extEdgeMildX / 255)) PSNR_severe = dip.metrics.PSNR(dip.float_to_im(X / 255), dip.float_to_im(extEdgeSevereX / 255)) #Displaying PSNR Output print("\n\tPSNR for extended Laplacian with minor blurring is %.2f" % PSNR_minor) print("\n\tPSNR for extended Laplacian with mild blurring is %.2f" % PSNR_mild) print("\n\tPSNR for extended Laplacian with severe blurring is %.2f" % PSNR_severe) #Image Output dip.figure() dip.subplot(3, 4, 1) dip.imshow(X, 'gray') dip.title('Original Image', fontsize='x-small') dip.subplot(3, 4, 2) dip.imshow(minorX, 'gray') dip.title('Minor Blurring', fontsize='x-small') dip.subplot(3, 4, 3) dip.imshow(mildX, 'gray') dip.title('Mild Blurring', fontsize='x-small') dip.subplot(3, 4, 4) dip.imshow(severeX, 'gray') dip.title('Severe Blurring', fontsize='x-small') dip.subplot(3, 4, 5) dip.imshow(edgeMinorX, 'gray') dip.title('Laplacian on Minor Blurring', fontsize='x-small') dip.subplot(3, 4, 6)
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
# Part (d) # Pad the downsampled image's spectrum (H2) with zeros and then take its # inverse Fourier transform H3 = np.pad(H2,(128, 128), 'constant', constant_values = (0,0)) # Zero-padded H2 I3 = np.abs(dip.ifft2(H3)) # Interpolated image I3 = I3/(np.amax(I3))*255 #Normalizing #Converting everything back to int and normalizing I1 = dip.float_to_im(I1/255) I2 = dip.float_to_im(I2/255) I3 = dip.float_to_im(I3/255) H2 = dip.fftshift(H2) H2 = np.log(np.abs(H2)) H3 = np.pad(H2,(128, 128), 'constant', constant_values = (0,0)) # Plotting dip.figure() dip.subplot(2, 3, 1) dip.imshow(I1, 'gray') dip.title('Original Image') dip.subplot(2, 3, 2) dip.imshow(I2, 'gray') dip.title('Downsampled Image') dip.subplot(2, 3, 3) dip.imshow(I3, 'gray') dip.title('Interpolated image') dip.subplot(2, 3, 4) dip.imshow(H1, 'gray') dip.title('Spectrum of Original Image') dip.subplot(2, 3, 5) dip.imshow(H2, 'gray') dip.title('Spectrum of Downsampled Image') dip.subplot(2, 3, 6)
sharpen3 = scale(img3 - Lap3) print('sharpen3') print(dip.PSNR(img, sharpen3)) Esharpen1 = scale(img1 - ELap1) print('Esharpen1') print(dip.PSNR(img, Esharpen1)) Esharpen2 = scale(img2 - ELap2) print('Esharpen2') print(dip.PSNR(img, Esharpen2)) Esharpen3 = scale(img3 - ELap3) print('Esharpen3') print(dip.PSNR(img, Esharpen3)) dip.figure(1) dip.subplot(1, 3, 1) dip.imshow(img1, 'gray') dip.title('Minor Distortion') dip.subplot(1, 3, 2) dip.imshow(img2, 'gray') dip.title('Mild Distortion') dip.subplot(1, 3, 3) dip.imshow(img3, 'gray') dip.title('Severe Distortion') dip.figure(2) dip.subplot(1, 3, 1) dip.imshow(Lap1, 'gray') dip.title('Minor Distortion') dip.subplot(1, 3, 2) dip.imshow(Lap2, 'gray')
# Loading image im = dip.im_read('images/UW_400.png') im = dip.im_to_float(im) if 2 < im.ndim: im = np.mean(im, axis=2) F = dip.fft2(im) print(h * F) #Plot results #Original spectra dip.figure(1) dip.subplot(2, 2, 1) dip.imshow(im, 'gray') dip.title('Original Image') dip.subplot(2, 2, 2) dip.imshow(np.real(dip.ifft2(F * h)), 'gray') dip.title('Modified image') dip.subplot(2, 2, 3) dip.imshow(abs(np.log(dip.fftshift(F))), 'gray') dip.title('Original spectra') dip.subplot(2, 2, 4) dip.imshow(abs(np.log(dip.fftshift(F) * h)), 'gray') dip.title('Modified spectra')
for k in range(SIZE): for l in range(SIZE): xIndex = n - k yIndex = m - l if (xIndex < 0): xIndex = LEN + xIndex if (yIndex < 0): yIndex = WIDTH + yIndex coeff = (k**2 + l**2) / (2 * (sigma**2)) h_kl = np.exp(-coeff) coeffSum = coeffSum + h_kl outputSum = outputSum + h_kl * g[xIndex, yIndex] gaussianImage[n, m] = (outputSum + TEENY) / (coeffSum + TEENY) #Displaying the Images dip.figure() dip.subplot(1, 5, 1) dip.imshow(f, 'gray') dip.title('Original Image', fontsize='x-small') dip.subplot(1, 5, 2) dip.imshow(g, 'gray') dip.title('Noised Image', fontsize='x-small') dip.subplot(1, 5, 3) dip.imshow(gaussianImage, 'gray') dip.title('Gaussian Filtering', fontsize='x-small') #Printing the MSE mseGaussian = dip.metrics.MSE(f * 255, gaussianImage * 255) print("\n\tThe MSE for Gaussian Transform is %.2f.\n" % mseGaussian) #Computing the Sigma Filter SIZE = 6 p = 40 / 255
X_rec = X_rec[0:row, 0:col] return X_rec #X = dip.im_read('airplane_downsample_gray_square.jpg') X = dip.im_read('brussels_gray_square.jpg') X_hat_1 = KLT_blocks(X, 1, 10) X_hat_12 = KLT_blocks(X, 12, 10) X_hat_24 = KLT_blocks(X, 24, 10) diff_1 = X - X_hat_1 diff_12 = X - X_hat_12 diff_24 = X - X_hat_24 dip.figure(1) dip.subplot(2, 2, 1) dip.imshow(X, 'gray') dip.title('Original') dip.subplot(2, 2, 2) dip.imshow(X_hat_1, 'gray') dip.title('k = 1') dip.subplot(2, 2, 3) dip.imshow(X_hat_12, 'gray') dip.title('k = 12') dip.subplot(2, 2, 4) dip.imshow(X_hat_24, 'gray') dip.title('k = 24') dip.figure(2) dip.subplot(1, 3, 1) dip.imshow(diff_1, 'gray')
# ============================ EDIT THIS PART ============================= im_gaussian = dip.image_noise(im, "gaussian") im_poisson = dip.image_noise(im, "poisson") im_salt_pepper = dip.image_noise(im, "s&p") im_speckle = dip.image_noise(im, "speckle") # Compute the SSIM values and images # ============================ EDIT THIS PART ============================= mssim_gaussian, ssim_image_gaussian = dip.metrics.SSIM(im, im_gaussian) mssim_poisson, ssim_image_poisson = dip.metrics.SSIM(im, im_poisson) mssim_salt_pepper, ssim_image_salt_pepper = dip.metrics.SSIM( im, im_salt_pepper) mssim_speckle, ssim_image_speckle = dip.metrics.SSIM(im, im_speckle) dip.figure() dip.subplot(2, 4, 1) dip.imshow(im_gaussian, 'gray') dip.title('Distortion type: gaussian', fontsize='x-small') dip.subplot(2, 4, 2) dip.imshow(im_poisson, 'gray') dip.title('Distortion type: poisson', fontsize='x-small') dip.subplot(2, 4, 3) dip.imshow(im_salt_pepper, 'gray') dip.title('Distortion type: salt & pepper', fontsize='x-small') dip.subplot(2, 4, 4) dip.imshow(im_speckle, 'gray') dip.title('Distortion type: speckle', fontsize='x-small') dip.subplot(2, 4, 5) dip.imshow(ssim_image_gaussian, 'gray') dip.title('SSIM Map; MSSIM={0:.2f}'.format(mssim_gaussian), fontsize='x-small')