def problem2(): """Solution ot part 2 of ps1.""" # Load the input image. src = cv2.imread('./input/ps1-input0.png') # Compute the edge image using canny edge method. edge_img = cv2.Canny(src, 100, 200) # Compute the Hough line transformation. H = hough.hough_lines_acc(edge_img) # Perform a histogram equalization to enchance the image. H_ench = hough.enchance_acc(H) # Save the result. cv2.imwrite('./output/ps1-2-a-1.png', H_ench) # Find peaks in accumulator array H. peaks = hough.hough_peaks(H, edge_img.shape, 150, 0) # Convert enchanced Hough image from gray to color to draw the peaks. H_ench = cv2.cvtColor(H_ench, cv2.COLOR_GRAY2RGB) # For each peak, draw a red dot in Hough accumulator array. H_peak = H_ench.copy() for param, pixel in peaks: cv2.circle(H_peak, (pixel[1], pixel[0]), 2, (0, 0, 255), -1) # Save the result. cv2.imwrite('./output/ps1-2-b-1.png', H_peak) # Draw the lines in the original image according to peaks List. line_img = hough.hough_lines_draw(src, peaks) # Save the result. cv2.imwrite('./output/ps1-2-c-1.png', line_img) # Uncoment the next 5 lines for display perposes. cv2.imshow('Hough Accumulator', H_ench) cv2.imshow('Hough Accumulator peaks', H_peak) cv2.imshow('line image', line_img) cv2.waitKey(0) cv2.destroyAllWindows()
def problem3(): """Solution ot part 3 of ps1.""" # Load the input image. src = cv2.imread('./input/ps1-input0-noise.png') # Remove noise with gaussian filter. src_smooth = cv2.GaussianBlur(src, (19, 19), 5) # Save smouthed image. cv2.imwrite('./output/ps1-3-a-1.png', src_smooth) # Compute the edge images using canny edge method. edge_img = cv2.Canny(src, 100, 200) edge_img_smoothed = cv2.Canny(src_smooth, 0, 50) # Save the original and smoothed images. cv2.imwrite('./output/ps1-3-b-1.png', edge_img) cv2.imwrite('./output/ps1-3-b-2.png', edge_img_smoothed) # Compute the Hough line transformation. H = hough.hough_lines_acc(edge_img_smoothed) # Perform a histogram equalization to enchance the image. H_ench = hough.enchance_acc(H) # Find peaks in accumulator array H. peaks = hough.hough_peaks(H, edge_img.shape, 140, 0) # Convert enchanced Hough image from gray to color to draw the peaks. H_ench = cv2.cvtColor(H_ench, cv2.COLOR_GRAY2RGB) # For each peak, draw a red dot in Hough accumulator array. H_peak = H_ench.copy() for param, pixel in peaks: cv2.circle(H_peak, (pixel[1], pixel[0]), 2, (0, 0, 255), -1) # Save the result. cv2.imwrite('./output/ps1-3-c-1.png', H_peak) # Draw the lines in the original image according to peaks List. line_img = hough.hough_lines_draw(src, peaks) # Save the result. cv2.imwrite('./output/ps1-3-c-2.png', line_img)
def problem6(): """Solution to part 5 of ps1.""" # Load image. src = cv2.imread('./input/ps1-input2.png') # Create a grayscale image from original. src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # smooth image to find edges. smoothed = cv2.GaussianBlur(src_gray, (9, 9), 1) # extract edges using canny edge algorithm. edge_img = auto_canny(smoothed, 2) H = hough.hough_lines_acc(edge_img, ) # Find peak points in Hough accumulator. peaks = hough.hough_peaks(H, edge_img.shape, 140) # perform histogram equalizaton to Hough accumulator to diplay it. H_ench = hough.enchance_acc(H) # Convert enchanced Hough image from gray to color to draw the peaks. H_ench = cv2.cvtColor(H_ench, cv2.COLOR_GRAY2RGB) src_gray = cv2.cvtColor(src_gray, cv2.COLOR_GRAY2RGB) # For each peak, draw a red dot in Hough accumulator array. H_peak = H_ench.copy() # Delete lines with 0 d. peaks = [(param, pixel) for param, pixel in peaks if param[0] > 1] for param, pixel in peaks: cv2.circle(H_peak, (pixel[1], pixel[0]), 1, (0, 0, 255), -1) # Draw found lines in the original image. line_img = hough.hough_lines_draw(src_gray, peaks) # Save the result. cv2.imwrite('./test/ps1-4-a-1.png', smoothed) cv2.imwrite('./test/ps1-4-b-1.png', edge_img) cv2.imwrite('./test/ps1-4-c-1.png', H_peak) cv2.imwrite('./test/ps1-4-c-2.png', line_img) cv2.waitKey(0)
# img_dir = np.arctan2(img_sobely,img_sobelx) # plt.imshow(img_sobelx, cmap = 'gray', interpolation = 'bicubic') # plt.xticks([]), plt.yticks([]) # plt.show() img_edges = cv2.Canny(img, 100, 200) cv2.imwrite(os.path.join('output', 'ps1-1-a-1.png'), img_edges) # Problem 2: Hough Line H, theta, rho = hough.hough_lines_acc(img_edges) H_norm = (H/np.amax(H)) * 255 cv2.imwrite(os.path.join('output', 'ps1-2-a-1.png'), H_norm) H1 = H_norm.copy() peaks = hough.hough_peaks(H1, 8, nHoodSize = (50,50)) for x,y in peaks: cv2.circle(H1, (int(x), int(y)), 10, (255,0,0), 1) cv2.imwrite(os.path.join('output', 'ps1-2-b-1.png'), H1) hough.hough_lines_draw(img, 'ps1-2-c-1.png', peaks, rho, theta) # Problem 3 img = cv2.imread(os.path.join('input', 'ps1-input0-noise.png'), 0) img_clean = cv2.GaussianBlur(img,(15,15),1) cv2.imwrite(os.path.join('output', 'ps1-3-a-1.png'), img_clean) img_edges1 = cv2.Canny(img, 200, 400) cv2.imwrite(os.path.join('output', 'ps1-3-b-1.png'), img_edges1) img_edges2 = cv2.Canny(img_clean, 200, 400)