def main(): ######## PART (a): EDIT HERE ######## img = img = dip.im_read("WiseonRocks_noise_1.png") # img = dip.im_to_float(img) # img = img*255 print("The shape of IMG is %s \n" % str(img.shape)) ######## PART (b): EDIT HERE ######## noise_estimation(img) ######## PART (c)-(e): EDIT HERE ######## SIZE = 100 print("Image 1") flatRegion = img[0:SIZE, 100:(100 + SIZE)] notFlatRegion = img[250:(250 + SIZE), 150:(150 + SIZE)] noise_estimation(flatRegion) noise_estimation(notFlatRegion) print("Image 2") img = img = dip.im_read("WiseonRocks_noise_2.png") flatRegion = img[0:SIZE, 100:(100 + SIZE)] noise_estimation(img) notFlatRegion = img[250:(250 + SIZE), 150:(150 + SIZE)] noise_estimation(flatRegion) noise_estimation(notFlatRegion) print("Image 3") img = img = dip.im_read("WiseonRocks_noise_3.png") flatRegion = img[0:SIZE, 100:(100 + SIZE)] noise_estimation(img) notFlatRegion = img[250:(250 + SIZE), 150:(150 + SIZE)] noise_estimation(flatRegion) noise_estimation(notFlatRegion) print("Image 4") img = img = dip.im_read("WiseonRocks_noise_4.png") flatRegion = img[0:SIZE, 100:(100 + SIZE)] noise_estimation(img) notFlatRegion = img[250:(250 + SIZE), 150:(150 + SIZE)] noise_estimation(flatRegion) noise_estimation(notFlatRegion) print("Image 5") img = img = dip.im_read("WiseonRocks_noise_5.png") flatRegion = img[0:SIZE, 100:(100 + SIZE)] noise_estimation(img) notFlatRegion = img[250:(250 + SIZE), 150:(150 + SIZE)] noise_estimation(flatRegion) noise_estimation(notFlatRegion)
def load_image(fpath) -> np.ndarray: im = dip.im_read(fpath) #im = dip.im_to_float(im) img_rgb = np.empty((im.shape[0], im.shape[1], 3), dtype='uint8') if im.ndim == 2: # Conv grayscale to RGB (quick approach) img_rgb = np.dstack([im]*3) else: img_rgb[:, :, :] = im return img_rgb
def encodeVlad(kmeans, im_path, xi, yi, scale, stride, blkRadii, numClusters): img = dip.im_read(im_path) if np.shape(img) != (200, 200): img = dip.resize(img, (200, 200)) # Resizes each image to be the same size features = computeGradientDMD(img, xi, yi, scale, stride, blkRadii) classes = kmeans.predict(features.T) #classes = np.array([kmeans.predict(features[:,i]) for i in range(np.shape(features)[1])]) numSamp = np.shape(xi)[1] descr = np.zeros((len(kmeans.cluster_centers_) * numSamp * 5, )) for i in range(numClusters): center = kmeans.cluster_centers_[i] center = np.reshape(center, (np.shape(center)[0], 1)) inds = np.where(classes == i)[0] centermat = np.repeat(center, np.shape(inds)[0], axis=1) descr[i * numSamp * 5:(i + 1) * numSamp * 5] = np.sum( centermat - features[:, inds], axis=1) return descr
def trainEncoder(images, numDescr, numClusters, xi, yi, scale, stride, blkRadii): numImages = len(images) numDescrPerImg = int(np.ceil(numDescr / numImages)) descrs = np.zeros((np.shape(xi)[1] * 5, numDescrPerImg * numImages)) for i in range(numImages): print("Training on : " + images[i]) # Prints name of image im = dip.im_read(images[i]) if np.shape(im) != (200, 200): im = dip.resize( im, (200, 200)) # Resizes each image to be the same size features = computeGradientDMD(im, xi, yi, scale, stride, blkRadii) subset = np.random.choice(np.shape(features)[1], size=int(numDescrPerImg), replace=False) ''' if(i == numImages - 1): numRemaining = np.shape(descrs[:, i*numDescrPerImg : (i+1)*numDescrPerImg])[1] subset = subset[:numRemaining] ''' descrs[:, i * numDescrPerImg:(i + 1) * numDescrPerImg] = features[:, subset] print("Finished descriptors") # Kmeans learning to find cluster centers kmeans = KMeans(n_clusters=numClusters, random_state=0, max_iter=1, algorithm='full').fit(descrs.T) # Returns Kmeans object containing clusters and assignments return kmeans
def open_image_file_as_matrix(self, image_path): im = dip.im_read(image_path) float_im = dip.im_to_float(im) gray_im = self.rgb_to_gray(float_im) return gray_im
training_vlad = np.empty((80,5*block_pairs*k)) pair = np.array([2,2,3,3,4,4]) correct_label = np.ones(16)*2 correct_label = np.concatenate((correct_label, np.ones(16)*3)) correct_label = np.concatenate((correct_label, np.ones(16)*1)) correct_label = np.concatenate((correct_label, np.zeros(16))) while i < 3: patch_size = 9 while patch_size < 16: block_size = 2 while block_size < 6: directory = r'D:/GT/FALL2020/IMG/Project/Train/Class0' count0 = 0 for filename in os.listdir(directory): img = dip.im_read('D:/GT/FALL2020/IMG/Project/Train/Class0/'+filename) bigd = sing_bigd(img, block_size, block_pairs, patch_size, 0.05) training_vlad[count0] = vlad_encoder(bigd,k) # print(training_vlad[count0]) count0 += 1 label = np.zeros(count0) directory = r'D:/GT/FALL2020/IMG/Project/Train/Class1' count1 = 0 for filename in os.listdir(directory): img = dip.im_read('D:/GT/FALL2020/IMG/Project/Train/Class1/'+filename) bigd = sing_bigd(img, block_size, block_pairs, patch_size, 0.05) training_vlad[count0+count1] = vlad_encoder(bigd,k) # print(training_vlad[count0+count1]) count1 += 1 label = np.concatenate((label, np.ones(count1)))
dip.imshow(images[i]) dip.show() 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 iqa_results(im1, im2): print('The MSE is: {:.2f}'.format( dip.MSE(cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY), cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)))) print('The PSNR is: {:.2f}'.format( dip.PSNR(cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY), cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY), np.amax(cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY))))) if __name__ == "__main__": original = dip.im_read('images/original.png') result = dip.im_read('temp_images/result.png') display_images([original, result], ['Original image', 'Result image']) display_diff(original, result) iqa_results(original, result)
import dippykit as dip import numpy as np #Resolution Guide #4CIF Resolution = 704 x 480 #CIF Resolution = 352 x 240 X = dip.im_read("coatOfArms.png") X = dip.im_to_float(X) X = X * 255 #Filter SIZE = 25 filterSize = (SIZE, SIZE) minor = 5**2 mild = 25**2 severe = 100**2 minorWindow = dip.window_2d(filterSize, window_type='gaussian', variance=minor) mildWindow = dip.window_2d(filterSize, window_type='gaussian', variance=mild) severeWindow = dip.window_2d(filterSize, window_type='gaussian', variance=severe) #Output of the Filter minorX = dip.convolve2d(X, minorWindow, mode='same', boundary='wrap') mildX = dip.convolve2d(X, mildWindow, mode='same', boundary='wrap') severeX = dip.convolve2d(X, severeWindow, mode='same', boundary='wrap') #Laplacian Filtering LaplaceKernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]], dtype=np.float)
if tmp2.shape[1] < col: error_col = col - tmp2.shape[1] tmp2 = np.pad(tmp2, ((0, 0), (error_col, error_col), (0, 0)), 'constant', constant_values=(0, 0)) tmp3 = tmp2[int(tmp2.shape[0] / 2 - 1 - row / 2 + 1):int(tmp2.shape[0] / 2 + row / 2), int(tmp2.shape[1] / 2 - 1 - col / 2 + 1):int(tmp2.shape[1] / 2 + col / 2), :] tmp4 = np.clip(tmp3, 0, 1) return tmp4 #example filename = 'images/airplane.jpg' im = dip.im_read(filename) #downSampling down_IMG = zoomAndshrink(im, 2.5, 1.0 / 1.7, 27.5 / 180.0 * np.pi) dip.figure(1) dip.imshow(down_IMG) #upSampling up_IMG = zoomAndshrink_inv(down_IMG, 0.4, 1.7, -27.5 / 180.0 * np.pi, im.shape) dip.figure(2) dip.imshow(up_IMG) #show difference dip.figure(3) dip.imshow(im - up_IMG) #show PSNR
# -*- coding: utf-8 -*- import dippykit as dip from math import exp import numpy as np # Proof of concept low pass filter dim_filter = 800 h = np.zeros((dim_filter, dim_filter)) for u in range(dim_filter): for v in range(dim_filter): h[u][v] = exp(-(u + v) / (dim_filter * 0.3)) # 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')
## Define Down Sampling Matrix D = 8 Down_Sampled_Matrix = np.array([[D, 0], [0, D]]) # Generate Training Set Training_images = np.zeros([100, 64, 64]) Training_labels = np.zeros([100, 1]) if mode == 0: for i in range(50): path1 = "Tumor_Images/" + str(i + 1) + ".png" path2 = "Non_Tumor_Images/" + str(i + 1) + ".png" Training_images[i, :, :] = dip.resample( dip.im_to_float(dip.im_read(path1)), Down_Sampled_Matrix) Training_images[i + 50, :, :] = dip.resample( dip.im_to_float(dip.im_read(path2)), Down_Sampled_Matrix) Training_labels[i] = 1 Training_labels[i + 50] = 0 elif mode == 1: for i in range(50): path1 = "Tumor_Images/" + str(i + 1) + ".png" path2 = "Non_Tumor_Images/" + str(i + 1) + ".png" X1 = SkullAndShape(cv2.imread(path1)) X2 = SkullAndShape(cv2.imread(path1))
import dippykit as dip import numpy as np from skimage.restoration import denoise_bilateral as bilateral from skimage.filters import gaussian #Loading the image and assigning to f f = dip.im_read("cameraman.tif") f = dip.im_to_float(f) #Assigning the noise to g SIGMA = 20 / 255 g = dip.image_noise(f, mode='gaussian', mean=0, var=(SIGMA**2)) #Finding the Gaussian Noise LEN, WIDTH = g.shape SIZE = 6 sigma = 1 / 255 TEENY = 0.0000000000001 gaussianImage = np.zeros(g.shape) # gaussianImage = gaussian(g,\ # sigma=1.0,\ # mode='wrap',\ # multichannel=False) for n in range(LEN): for m in range(WIDTH): coeffSum = 0 outputSum = 0 for k in range(SIZE): for l in range(SIZE): xIndex = n - k yIndex = m - l
import dippykit as dip import numpy as np import cv2 # STEP 1: Loading the image # ============================ EDIT THIS PART ================================= 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 =================================
#Reconstruct the image and return it X_rec = np.zeros((row_new, col_new)) index = 0 for i in range(row_block): for j in range(col_block): tmp = F_app[:, index] X_rec[i * B:(i + 1) * B, j * B:(j + 1) * B] = tmp.reshape(B, B, order='F') index = index + 1 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')
import dippykit as dip import numpy as np # Part (a) I1 = dip.im_read('/home/harshbhate/Pictures/lena.png') # Specify your image here I1 = dip.im_to_float(I1) #Converting image to float I1 *= 255 #Normalizing # Part (b) # Take the Fourier transform of the image H1 = dip.fft2(I1) # Fourier transform of I1 H1 = dip.fftshift(H1) H1 = np.log(np.abs(H1)) print ("Shape of I1:"+str(I1.shape)) print("Shape of H1"+str(H1.shape)) # Part (c) # Downsample the image by 2 in both directions (and take its Fourier transform) x_scaling = 2 y_scaling = 2 sampling_matrix = np.array([[x_scaling, 0],[0, y_scaling]]) I2 = dip.sampling.resample(I1, sampling_matrix) # Downsampled I1 H2 = dip.fft2(I2) # Fourier transform of I2 # 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)
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
import dippykit as dip import numpy as np f = dip.im_read('cameraman.tif') f = dip.float_to_im(f, 8) g = dip.image_noise(f, 'gaussian', var=(20**2)) def filter(h, gext: np.ndarray, m: int, n: int, L: int): nominator = 0 denominator = 0 for k in range(-L, L + 1): for l in range(-L, L + 1): nominator = nominator + h(k, l) * gext[n + L - k, m + L - l] denominator = denominator + h(k, l) result = nominator / denominator return result #Gaussian Filter sigma1 = 1 L = 3 h1 = lambda k, l: np.exp(-(k * k + l * l) / (2 * sigma1 * sigma1)) gext = np.pad(g, (L, L), mode='symmetric') output1 = np.zeros(g.shape) for m in range(output1.shape[1]): for n in range(output1.shape[0]): output1[n, m] = filter(h1, gext, m, n, L) print('MSE1') print(dip.MSE(f, output1)) #Range (or Sigma) Filter
from PIL import Image x_max = 0 y_max = 0 dir_no = "../original_data/no/" dir_yes = "../original_data/yes/" dirs = [dir_no, dir_yes] ########################### # IMAGE SIZE DISTRIBUTION # ########################### for directory in dirs: for filename in os.listdir(directory): im = dip.im_read(directory + filename) dim = im.shape x_max = max(x_max, dim[1]) y_max = max(y_max, dim[0]) 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))
import dippykit as dip import numpy as np import matplotlib.pyplot as plt import os path = "/Users/chuchu/Dropbox/gt_exp/ddb1_fundusimages/" # Change the path to where you store the images modes = [2, 3, 4] for m in range(len(modes)): dir = 'upsample_img/'+str(modes[m]); if not os.path.exists(dir): os.makedirs(dir) for m in range(len(modes)): for filename in os.listdir(path): mode = modes[m] file_path = path + filename f = dip.im_read(file_path) # read icmage f = dip.im_to_float(f) M = np.array([[1/mode, 0], [0, 1/mode]]) f_up_0 = dip.sampling.resample(f[:,:,0], M, interp= 'bilinear') f_up_1 = dip.sampling.resample(f[:, :, 1], M, interp='bilinear') f_up_2 = dip.sampling.resample(f[:, :, 2], M, interp='bilinear') h, l = f_up_0.shape f_up = np.zeros((h, l, 3)) f_up[:,:,0] = f_up_0 f_up[:, :, 1] = f_up_1 f_up[:, :, 2] = f_up_2 upsample_img = 'upsample_img/' + str(mode)+ '/' +filename[:-4] + '_up' + str(mode) + '.jpg' save_im = dip.float_to_im(f_up) dip.im_write(save_im, upsample_img)
def imageToBoard(path): # Read the image and extract the grayscale and hue channels image = dip.im_read(path) image = np.rot90(image[:, :, 0:3], 3) hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) hue = hsv[:, :, 0] image = dip.rgb2gray(image) # Scale down the image so that the smallest dimension is 1000 pixels x, y = image.shape if min(x, y) > 1000: scale = 1000.0 / min(x, y) newSize = (int(scale * y), int(scale * x)) image = dip.resize(image, newSize) hue = dip.resize(hue, newSize) # Detect the straight lines and draw them (dilated) on a blank canvas image2 = np.zeros(image.shape, dtype=np.uint8) lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(image)[0] for line in lines: for (x1, y1, x2, y2) in line: # length = abs(np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)) cv2.line(image2, (x1, y1), (x2, y2), 255, 3) # dip.figure("Image") # dip.imshow(image, 'gray') # dip.figure("Test Lines") # dip.imshow(image2, 'gray') # dip.show() # Find the largest blob in the image to find the board Labeled, numobj = ndImage.label(image2) lastSum = 0 displayImage = None for item in range(1, numobj + 1): newImage = (Labeled == item) newSum = newImage.sum() if newSum > lastSum: displayImage = newImage lastSum = newSum # Find the four corners of the image. # The corners are defined as the maxima of the four functions: # (x + y), (X - x + y), (x + Y - y), and (X - x + Y - y) # This assumes the image is taken roughly square with the image boundaries, but it can vary somewhat cornerBR = (0, 0) sumBR = 0 cornerTR = (0, 0) sumTR = 0 cornerBL = (0, 0) sumBL = 0 cornerTL = (0, 0) sumTL = 0 imagey, imagex = displayImage.shape for x in range(0, imagex): for y in range(0, imagey): if displayImage[y][x] != 0: temp = x + y if temp > sumBR: sumBR = temp cornerBR = (x, y) temp = x + imagey - y if temp > sumTR: sumTR = temp cornerTR = (x, y) temp = imagex - x + imagey - y if temp > sumTL: sumTL = temp cornerTL = (x, y) temp = imagex - x + y if temp > sumBL: sumBL = temp cornerBL = (x, y) # Estimate the transformation that would put the board corners on the image corners dest = np.array([cornerTL, cornerBL, cornerBR, cornerTR]) scale = 1000 # 15 * 15 * 20 src = np.array([[0, 0], [0, scale], [scale, scale], [scale, 0]]) tform3 = tf.ProjectiveTransform() tform3.estimate(src, dest) warped = tf.warp(image, tform3, output_shape=[scale, scale]) hue = tf.warp(hue, tform3, output_shape=[scale, scale]) warped = warped[7 : -7, 7 : -7] hue = hue[7 : -7, 7 : -7] # dip.figure("warped") # dip.imshow(warped, 'gray') # dip.figure("hue") # dip.imshow(hue, 'gray') # dip.show() # Do line detection again to try to fine the best grid lines, particularly on the board borders warped = dip.float_to_im(warped) gridLines = np.zeros(warped.shape, dtype=np.uint8) lsd = cv2.createLineSegmentDetector(_refine=cv2.LSD_REFINE_ADV) lines = lsd.detect(warped)[0] for line in lines: for (x1, y1, x2, y2) in line: cv2.line(gridLines, (x1, y1), (x2, y2), 255, 3) # dip.figure("warped") # dip.imshow(warped, 'gray') # dip.figure("gridLines") # dip.imshow(gridLines, 'gray') # dip.show() # Determine the actual rows/cols that start with pixels dimX, dimY = warped.shape topRow = 0 botRow = dimY - 1 leftCol = 0 rightCol = dimX - 1 while np.amax(gridLines[:, leftCol]) == 0: leftCol += 1 while np.amax(gridLines[:, rightCol]) == 0: rightCol -= 1 while np.amax(gridLines[topRow]) == 0: topRow += 1 while np.amax(gridLines[botRow]) == 0: botRow -= 1 lineTop = (topRow, topRow) lineBot = (botRow, botRow) lineLeft = (leftCol, leftCol) lineRight = (rightCol, rightCol) bestScoreTop = 0 bestScoreBot = 0 bestScoreLeft = 0 bestScoreRight = 0 # Within a small range from the border, determine the lines that best describe the image borders # They are scored by which one has the most overlap with the canvas with the image lines canvas = np.zeros(warped.shape, dtype=np.uint8) thickness = 13 for i in range(6, 50): for j in range(6, 50): # Top Row x1 = 0 y1 = topRow + i x2 = dimX - 1 y2 = topRow + j canvas.fill(0) cv2.line(canvas, (x1, y1), (x2, y2), 255, thickness) score = np.count_nonzero(np.logical_and(canvas > 0, gridLines)) if score > bestScoreTop: lineTop = (y1, y2) bestScoreTop = score # Bottom Row x1 = 0 y1 = botRow - i x2 = dimX - 1 y2 = botRow - j canvas.fill(0) cv2.line(canvas, (x1, y1), (x2, y2), 255, thickness) score = np.count_nonzero(np.logical_and(canvas > 0, gridLines)) if score > bestScoreBot: lineBot = (y1, y2) bestScoreBot = score # Left Column x1 = leftCol + i y1 = 0 x2 = leftCol + j y2 = dimY - 1 canvas.fill(0) cv2.line(canvas, (x1, y1), (x2, y2), 255, thickness) score = np.count_nonzero(np.logical_and(canvas > 0, gridLines)) if score > bestScoreLeft: lineLeft = (x1, x2) bestScoreLeft = score # Right Column x1 = rightCol - i y1 = 0 x2 = rightCol - j y2 = dimY - 1 canvas.fill(0) cv2.line(canvas, (x1, y1), (x2, y2), 255, thickness) score = np.count_nonzero(np.logical_and(canvas > 0, gridLines)) if score > bestScoreRight: lineRight = (x1, x2) bestScoreRight = score xDiff0 = (lineBot[0] - lineTop[0]) / 15.0 xDiff1 = (lineBot[1] - lineTop[1]) / 15.0 yDiff0 = (lineRight[0] - lineLeft[0]) / 15.0 yDiff1 = (lineRight[1] - lineLeft[1]) / 15.0 # cv2.line(warped, (0, lineTop[0]), (dimY - 1, lineTop[1]), 0, 3) # cv2.line(warped, (0, lineBot[0]), (dimY - 1, lineBot[1]), 0, 3) # cv2.line(warped, (lineLeft[0], 0), (lineLeft[1], dimX - 1), 0, 3) # cv2.line(warped, (lineRight[0], 0), (lineRight[1], dimX - 1), 0, 3) # # for i in range(1, 15): # y1 = int(lineTop[0] + i * xDiff0) # y2 = int(lineTop[1] + i * xDiff1) # cv2.line(warped, (0, y1), (dimY - 1, y2), 0, 3) # # x1 = int(lineLeft[0] + i * yDiff0) # x2 = int(lineLeft[1] + i * yDiff1) # cv2.line(warped, (x1, 0), (x2, dimX - 1), 0, 3) # # dip.figure("Lined image") # dip.imshow(warped, 'gray') # dip.show() # Now go through each of the 225 (15 * 15) cells grid = [] for i in range(0, 15): grid.append([]) for j in range(0, 15): # Calculate the four corners of the current grid square amt_i = [i / 15.0, (i + 1) / 15.0] amt_i_inv = [1.0 - i / 15.0, 1.0 - (i + 1) / 15.0] tl_y = int((lineTop[0] + (i + 0) * xDiff0) * amt_i_inv[0] + (lineTop[1] + (i + 0) * xDiff1) * amt_i[0]) tr_y = int((lineTop[0] + (i + 0) * xDiff0) * amt_i_inv[1] + (lineTop[1] + (i + 0) * xDiff1) * amt_i[1]) bl_y = int((lineTop[0] + (i + 1) * xDiff0) * amt_i_inv[0] + (lineTop[1] + (i + 1) * xDiff1) * amt_i[0]) br_y = int((lineTop[0] + (i + 1) * xDiff0) * amt_i_inv[1] + (lineTop[1] + (i + 1) * xDiff1) * amt_i[1]) amt_j = [j / 15.0, (j + 1) / 15.0] amt_j_inv = [1.0 - j / 15.0, 1.0 - (j + 1) / 15.0] tl_x = int((lineLeft[0] + (j + 0) * yDiff0) * amt_j_inv[0] + (lineLeft[1] + (j + 0) * yDiff1) * amt_j[0]) bl_x = int((lineLeft[0] + (j + 0) * yDiff0) * amt_j_inv[1] + (lineLeft[1] + (j + 0) * yDiff1) * amt_j[1]) tr_x = int((lineLeft[0] + (j + 1) * yDiff0) * amt_j_inv[0] + (lineLeft[1] + (j + 1) * yDiff1) * amt_j[0]) br_x = int((lineLeft[0] + (j + 1) * yDiff0) * amt_j_inv[1] + (lineLeft[1] + (j + 1) * yDiff1) * amt_j[1]) scale = 80 pad = 10 # Warp the image so that the grid square becomes the center of the image with some padding on all sides dest = np.array([[pad, pad], [pad, scale + pad], [scale + pad, scale + pad], [scale + pad, pad]]) src = np.array([[tl_x, tl_y], [bl_x, bl_y], [br_x, br_y], [tr_x, tr_y]]) tform = tf.ProjectiveTransform() tform.estimate(dest, src) total = scale + 2 * pad output = tf.warp(warped, tform, output_shape=[total, total]) outputHue = tf.warp(hue, tform, output_shape=[total, total]) # Output hue doesn't use any of the extra padding because it wants the values from the middle of the tile outputHue = outputHue[2 * pad : -2 * pad, 2 * pad : -2 * pad] # Perform a simple image threshold to determine any text on the tile outputBinary = np.logical_not(output < 0.55) Labeled, numobj = ndImage.label(outputBinary) closestBlob = None distance = 20 for item in range(1, numobj + 1): blob = (Labeled != item) x, y = output.shape for a in range(0, x): for b in range(0, y): if blob[a, b] == 0: dist = np.sqrt((a - 50) ** 2 + (b - 50) ** 2) tot = np.sum(blob) # If the current blob is within a set distance from the middle of the image, # and the total count doesn't indicate a false tile or a blank tile if dist < distance and 9000 < tot and tot < 9950: distance = dist closestBlob = blob text = "?" # If a blob was detected if closestBlob is not None: closestBlob = closestBlob.astype(np.uint8) * 255 # Perform OCR text = pytesseract.image_to_string(closestBlob, config='--oem 0 -c tessedit_char_whitelist=ABCDEFGHIJLKMNOPQRSTUVWXYZ|01l --psm 10') # Just a precaution to fix any ambiguity with 0s and Os text = text.replace("0", "O") # Correct the I tile, as a straight line doesn't easily count with vanilla Tesseract if text in ['', '|', 'l', '1']: text = "I" # If no letter detected and the median hue & grayscale values indicate a blank tile med = np.median(outputHue) if text == "?" and (med > 0.6 or med < 0.01) and np.median(output) < 0.3: text = '_' grid[-1].append(text) return grid
import dippykit as dip import numpy as np def scale(img: np.ndarray): for i in range(img.shape[0]): for j in range(img.shape[1]): if img[i, j] > 1: img[i, j] = 1 if img[i, j] < 0: img[i, j] = 0 return img img = dip.im_read('barbara.png') Gaussian = 1 / 16 * np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) img1 = dip.convolve2d(img, Gaussian, mode='same') img2 = dip.convolve2d(img1, Gaussian, mode='same') img3 = dip.convolve2d(img2, Gaussian, mode='same') Lap_kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) ELap_kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) Lap1 = dip.convolve2d(img1, Lap_kernel, mode='same') Lap2 = dip.convolve2d(img2, Lap_kernel, mode='same') Lap3 = dip.convolve2d(img3, Lap_kernel, mode='same') ELap1 = dip.convolve2d(img1, ELap_kernel, mode='same') ELap2 = dip.convolve2d(img2, ELap_kernel, mode='same') ELap3 = dip.convolve2d(img3, ELap_kernel, mode='same')
Date: August 24, 2018 GTID: 903424029 Problem Set: 1 Problem Number: 2 """ import dippykit as dip import numpy as np picture_link = "/home/harshbhate/Pictures/cameraman.tif" save_link_1 = "/home/harshbhate/Pictures/cameraman_add.tif" save_link_2 = "/home/harshbhate/Pictures/cameraman_square.tif" save_link_3 = "/home/harshbhate/Pictures/cameraman_fourier.tif" #(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
## Change the path to where you want to save the resulting images path_mix = "/Users/chuchu/Dropbox/gt_exp/set2/image_mix/" ## o_c_e = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0.5, 0.25, 0.25), (0.25, 0.5, 0.25), (0.25, 0.25, 0.5), (0.33, 0.33, 0.33)] dir = path_mix if not os.path.exists(dir): os.makedirs(dir) for event in range(3, len(o_c_e)): dir = path_mix + 'event_' + str(o_c_e[event][0]) + '_' + str( o_c_e[event][1]) + '_' + str(o_c_e[event][2]) if not os.path.exists(dir): os.makedirs(dir) for filename in os.listdir(path_orgin): path_image_origin = path_orgin + filename path_image_contrast = path_contrast + filename path_image_edge = path_edge + filename im_origin = dip.im_read(path_image_origin) # read icmage im_contrast = dip.im_read(path_image_contrast) # read icmage im_edge = dip.im_read(path_image_edge) # read icmage im_mix = (im_origin * o_c_e[event][0] + im_contrast * o_c_e[event][1] + im_edge * o_c_e[event][2]).astype(np.uint8) save_name = dir + '/' + filename[:-4] + '.png' dip.im_write(im_mix, save_name)
def open_image(file): three_layer_image = dip.im_to_float(dip.im_read(file)) return np.dot(three_layer_image[..., :3], [0.2989, 0.5870, 0.1140])
import numpy as np import dippykit as dip # Read the image # ============================ EDIT THIS PART ============================= im = dip.im_read("ch06_ssim/barbara.png") # Add noise to the original image to create new images # ============================ 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')