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 fast_multiplicative_restore(self, degraded_image, h_param=35, search_window_size=51): int_image = dip.float_to_im(degraded_image) return dip.im_to_float( cv2.fastNlMeansDenoising(int_image, h=h_param, searchWindowSize=search_window_size))
def param_search_multiplicative_restore(self, degraded_image, original_image): int_image = dip.float_to_im(degraded_image) psnr_max = None best_denoise = None for i in range(1, 25): cur_denoised = dip.im_to_float( cv2.fastNlMeansDenoising(int_image, h=i, searchWindowSize=31)) cur_psnr = dip.PSNR(original_image, cur_denoised) if psnr_max is None or cur_psnr > psnr_max: best_denoise = cur_denoised psnr_max = cur_psnr return best_denoise
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 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 = 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)) 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")
def process(info, grid, odom): resolution = info[3] origin_x = info[0] origin_y = info[1] # origin_z = info[2] size = grid.shape robot_pose_world = odom # robot initial position in world robot_pose_pixel = [] # robot initial position in grid (pixel in image) robot_pose_pixel[0] = (robot_pose_world[0] - origin_x) / resolution robot_pose_pixel[1] = (robot_pose_world[0] - origin_y) / resolution #--------------------------------------------- open cells --------------------- thresh_low = 0 thresh_high = 10 dst =((grid <= thresh_high) & (grid >= thresh_low))*1.0 #threshold # "1" in dst are the open cells # "0" in dst are the unvisited cells and occupied cells # detect contours contours_open = measure.find_contours(dst, 0.5) contours_open_cell = list() for ele in contours_open: for cell in ele: contours_open_cell.append(cell.tolist()) #print(contours_open_cell) #--------------------------------------------- unvisited cells --------------------- thresh_low = -1 thresh_high = -1 dst =((grid <= thresh_high) & (grid >= thresh_low))*1.0 #threshold # "1" in dst are the unvisited cells # "0" in dst are the open cells and occupied cells # find contours contours_unvisited = measure.find_contours(dst, 0.5) contours_unvisited_cell = list() for ele in contours_unvisited: for cell in ele: contours_unvisited_cell.append(cell.tolist()) #print(contours_unvisited_cell) #---------------------------------------------------------------- # frontier detection ! ! ! frontier_cells = [x for x in contours_unvisited_cell if x in contours_open_cell] #print('frontier: ') #print(frontier_cells) # to find the same elements in both lists grid_frontier = np.zeros(size) for ele in frontier_cells: grid_frontier[math.floor(ele[0]), math.floor(ele[1])] = 1 # group them! grid_frontier_img = dip.float_to_im(grid_frontier) conected_frontier, label_num = measure.label(grid_frontier_img, return_num=True, connectivity=2) print("num of frontiers: %d" %label_num) conected_frontier = dip.float_to_im(conected_frontier/label_num) # delete small frontiers #image_label_overlay = label2rgb(conected_frontier, image=grid_frontier_img) #fig, ax = plt.subplots(figsize=(10, 6)) #ax.imshow(image_label_overlay) manh_dist = [] # stores distances cents = [] # stores centers of frontiers for region in regionprops(conected_frontier): # take regions with large enough areas if region.area >= 10: # do not consider small frontier groups # print the centroid of each valid region cen_y = region.centroid[0] # Centroid coordinate tuple (row, col) cen_x = region.centroid[1] # Centroid coordinate tuple (row, col) cents.append([cen_x, cen_y]) manh = abs(cen_x - robot_pose_pixel[0]) + abs(cen_y - robot_pose_pixel[1]) # Manhattan Distance from robot to each frontier center manh_dist.append(manh) #print(region.centroid) # Centroid coordinate tuple (row, col) # draw rectangle around segmented coins #minr, minc, maxr, maxc = region.bbox #rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, # fill=False, edgecolor='red', linewidth=1) #ax.add_patch(rect) #ax.set_axis_off() #plt.tight_layout() #plt.show() next_goal = cents[manh_dist.index(min(manh_dist))] # transform into real world next_goal[0] = next_goal[0]*resolution + origin_x next_goal[1] = next_goal[1]*resolution + origin_y print('next_goal: ', next_goal) return next_goal
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) 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')
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)
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
dir = 'enhance/'; if not os.path.exists(dir): os.makedirs(dir) for mode in modes: for filename in os.listdir(path): file_path = path + filename f = dip.im_read(file_path) #read icmage f = dip.im_to_float(f) f_g = rgb2gray(f) f_edge = dip.transforms.edge_detect(f_g, mode = mode, as_bool=False); edgename = 'edge/' + filename[:-4] + '.png'; if mode is not 'canny': f_edge *= 1/f_edge.max() save_im = dip.float_to_im(f_edge) dip.im_write(save_im, edgename) f[:,:,0] += f_edge f[:, :, 1] += f_edge f[:, :, 2] += f_edge enhance_img = 'enhance/' + filename[:-4] + '.png'; save_im = dip.float_to_im(f) dip.im_write(save_im, enhance_img) ####combine all # # for filename in os.listdir(path): # file_path = path + filename # f = dip.im_read(file_path) # read icmage # f = dip.im_to_float(f)
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 =================================
def multiplicative_clustering_restore(self, degraded_image): block_size = 16 max_h_value = 30 blocks = self.break_image_into_blocks(degraded_image, block_size) variances = self.get_variances_of_blocks(blocks) medians = self.get_statistic_of_blocks(blocks, np.median) means = self.get_means_of_blocks(blocks) maxs = self.get_statistic_of_blocks(blocks, lambda b: np.percentile(b, 55)) mins = self.get_statistic_of_blocks(blocks, lambda b: np.percentile(b, 45)) scaler = preprocessing.StandardScaler() data = [] for i in range(len(blocks)): data.append([ variances[i], np.abs(medians[i] - means[i]), maxs[i] * 1.3, mins[i] * 1.3 ]) scaler.fit(data) output_image = np.zeros( [degraded_image.shape[0], degraded_image.shape[1]]) cluster_image = np.zeros( [degraded_image.shape[0], degraded_image.shape[1]]) h_param_image = np.zeros(degraded_image.shape) ch = ClusteringHandler(data) ch.cluster_data() clustered_labels = ch.labels cluster_centers = np.asarray(ch.cluster_centers) diff_c = cluster_centers[:, 2] - cluster_centers[:, 3] mean_c = cluster_centers[:, 1] median_c = cluster_centers[:, 1] var_centers = cluster_centers[:, 0] average_var = np.mean(var_centers) h_params = np.linspace(max_h_value, max_h_value, ch.num_clusters) window_sizes = [21 for i in range(ch.num_clusters)] count = 0 h_params = [ a for _, a in sorted(zip(var_centers, h_params), reverse=True) ] h_params = var_centers**.5 * diff_c**.55 * 20 * 25 * 23 * 30 * 15 / 30 / 300 for m in range(0, degraded_image.shape[0], block_size): for n in range(0, degraded_image.shape[1], block_size): cur_percentile = clustered_labels[ m // block_size * (degraded_image.shape[0] // block_size) + (n // block_size)] cluster_image[m:m + block_size, n:n + block_size] = cur_percentile / ch.num_clusters h_param_image[m:m + block_size, n:n + block_size] = h_params[cur_percentile] # dip.im_write(dip.float_to_im(h_param_image / np.max(h_param_image)), "./unsmoothed_hparams.jpg") h_param_image = self.blur_borders(h_param_image, cluster_image) # dip.im_write(dip.float_to_im(h_param_image / np.max(h_param_image)), "./smoothed.jpg") h_param_count = 0 all_temp_outputs = [] h_param_list = np.unique(h_param_image.flatten()) processes = [] manager = Manager() return_dict = manager.dict() blocked_pad_size = 32 blocked_image = self.create_surrounding_block_fast( degraded_image, pad_width=blocked_pad_size) process_count = 0 for c in h_param_list: temp_output_image = self.fast_multiplicative_restore( blocked_image, h_param=c, search_window_size=21 )[blocked_pad_size:degraded_image.shape[0] + blocked_pad_size, blocked_pad_size:degraded_image.shape[1] + blocked_pad_size] return_dict[c] = temp_output_image h_param_count += 1 output_image = output_image.flatten() h_param_image = h_param_image.flatten() for cur_param in return_dict.keys(): idx = h_param_image == cur_param return_dict[cur_param] = return_dict[cur_param].flatten() output_image[idx] = return_dict[cur_param][idx] return dip.im_to_float( dip.float_to_im(output_image.reshape( degraded_image.shape))), cluster_image, h_params
#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) edgeMinorX = dip.convolve2d(X, LaplaceKernel, mode='same', boundary='wrap') edgeMildX = dip.convolve2d(X, LaplaceKernel, mode='same', boundary='wrap') edgeSevereX = dip.convolve2d(X, LaplaceKernel, mode='same', boundary='wrap') #PSNR Calculations PSNR_minor = dip.metrics.PSNR(dip.float_to_im(X / 255), dip.float_to_im(edgeMinorX / 255)) PSNR_mild = dip.metrics.PSNR(dip.float_to_im(X / 255), dip.float_to_im(edgeMildX / 255)) PSNR_severe = dip.metrics.PSNR(dip.float_to_im(X / 255), dip.float_to_im(edgeSevereX / 255)) #Displaying PSNR Output print("\n\tPSNR for Laplacian with minor blurring is %.2f \n" % PSNR_minor) print("\tPSNR for Laplacian with mild blurring is %.2f \n" % PSNR_mild) print("\tPSNR for Laplacian with severe blurring is %.2f \n" % PSNR_severe) #Extended Laplacian Filtering extLaplaceKernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float) extEdgeMinorX = dip.convolve2d(X,
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
def save_matrix_as_image_file(self, matrix, image_path): dip.im_write(dip.float_to_im(matrix), image_path)
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 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
def floater(X): return dip.float_to_im(X / 255)