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 hist25(image): dim = image.shape x = np.reshape(image, (dim[0] * dim[1], 1)) dip.hist(x) dip.grid() dip.title('Histogram') dip.xlabel('Pixel value') dip.ylabel('Pixel frequency')
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 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 hist(image, plot): dim = image.shape M = dim[0] N = dim[1] res = np.zeros(256) for i in range(0, M): for j in range(0, N): res[image[i, j]] += 1 if plot: dip.bar(np.arange(256), res, width=1) dip.grid() dip.title('Histogram') dip.xlabel('Pixel value') dip.ylabel('Pixel frequency') return res
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 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()
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') dip.subplot(2, 4, 6) dip.imshow(ssim_image_poisson, 'gray')
im = dip.im_read('lena.png') im = dip.float_to_im(im, 8) # STEP 2: Converting to YCrCb # ============================ EDIT THIS PART ================================= im = dip.rgb2ycbcr(im) # HINT: Look into dip.rgb2ycbcr # STEP 3: Keep only the luminance channel # ============================ EDIT THIS PART ================================= im = im[:, :, 0] # ===================!!!!! DO NOT EDIT THIS PART !!!!!========================= dip.figure() dip.subplot(2, 2, 1) dip.imshow(im, 'gray') dip.title('Grayscale image') # STEP 4: Calculating the image entropy # ============================ EDIT THIS PART ================================= H = dip.entropy(im) # ===================!!!!! DO NOT EDIT THIS PART !!!!!========================= print("Entropy of the grayscale image = {:.2f} bits/pixel".format(H)) # STEP 5: Coding of the original image # ============================ EDIT THIS PART ================================= im_vec = im.reshape(-1) im_encoded, stream_length, symbol_code_dict, symbol_prob_dict = dip.huffman_encode( im_vec) row, col = im.shape im_bit_rate = stream_length / (row * col) # ===================!!!!! DO NOT EDIT THIS PART !!!!!=========================
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) dip.imshow(edgeMildX, 'gray') dip.title('Laplacian on Mild Blurring', fontsize='x-small')
image_size_distribution = np.zeros((y_max + 100, x_max + 100)) for filename in os.listdir(directory): im = dip.im_read(directory + filename) dim = im.shape image_size_distribution[dim[0]][dim[1]] += 1 image_size_distribution = image_size_distribution / np.amax( image_size_distribution) dip.contour(dip.float_to_im(image_size_distribution, 8)) dip.grid() dip.xlabel('image width') dip.ylabel('image height') dip.title('Image size distribution') ########################### # CONVERTING TO GRAYSCALE # ########################### for directory in dirs: for filename in os.listdir(directory): out = Image.open(directory + filename).convert('L') filename_without_extension = os.path.splitext(filename)[0] if directory == dir_no: out.save("../grayscale_data/no/" + filename_without_extension + ".png") elif directory == dir_yes: out.save("../grayscale_data/yes/" + filename_without_extension + ".png")
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
# 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) dip.imshow(H3, 'gray') dip.title('Spectrum of Interpolated image')
#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') dip.colorbar() dip.title('k = 1')
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') dip.title('Mild Distortion') dip.subplot(1, 3, 3)
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') dip.show()
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 TEENY = 0.0000000000001 sigmaImage = np.zeros(g.shape)
# ============================ EDIT THIS PART ================================= im = dip.image_io.im_read(img_path) # STEP 2: Converting to YCrCb # ============================ EDIT THIS PART ================================= im = dip.utilities.rgb2ycbcr(im) # HINT: Look into dip.rgb2ycbcr # STEP 3: Keep only the luminance channel # ============================ EDIT THIS PART ================================= im = im[:,:,0] # ===================!!!!! DO NOT EDIT THIS PART !!!!!========================= dip.figure() dip.subplot(2, 2, 1) dip.imshow(im, 'gray') dip.title('Grayscale image', fontsize='x-small') # STEP 4: Calculating the image entropy # ============================ EDIT THIS PART ================================= H = dip.metrics.entropy(im) # ===================!!!!! DO NOT EDIT THIS PART !!!!!========================= print("Entropy of the grayscale image = {:.2f} bits/pixel".format(H)) # STEP 5: Coding of the original image # ============================ EDIT THIS PART ================================= byte_seq, _,_,_ = dip.coding.huffman_encode(im.flatten()) l,b = im.shape im_bit_rate = (len(byte_seq)*8.0)/float(l*b) # ===================!!!!! DO NOT EDIT THIS PART !!!!!========================= print("Bit rate of the original image = {:.2f} bits/pixel"