def alternate_rect_attempt(image): orig = image.copy() # <---- RESIZING -----> # image, ratio = functions.standard_resize(image, new_width=100.0) # <---- RESIZING -----> # # edged = functions.colorOps(image) edged = functions.page_edging(image) processed = functions.closed_inversion(edged) boxes = functions.alternateRectMethod(processed) functions.boxes_comparison(image=processed, boxes=boxes) detected = image.copy() rects = [] for box in boxes: # print box detected = cv2.drawContours(detected, [box], 0, (0, 255, 0), 1) rects.append(cv2.minAreaRect(box)) # print "rect in alternate_rect_attempt: " + str(cv2.minAreaRect(box)) # utility.IOU(rects[0], rects[1]) if len(boxes) > 1: # print "got more than 1 box, attempting merge" merged = functions.merge_boxes(boxes) detected = cv2.drawContours(detected, [merged], 0, (255, 0, 0), 2) functions.plot_images( [edged, processed, detected], ["Edge Detection", "Morphological Operations", "Contour Finding"])
def run_net(opt): psnrs = [] cnn.eval() for i, (images, labels) in enumerate(tst_dloader): images, labels = functions.prepare_data( images, labels, pixels_shuffle=opt.pixels_shuffle, use_cuda=use_cuda) outputs = cnn(images) outputs.data = torch.clamp(outputs.data, min=0, max=1) psnr = compute_psnr(outputs, labels, opt) psnrs.append(psnr) if (opt.plot): if (opt.pixels_shuffle): images = functions.resize_lr( images, (outputs.size()[2], outputs.size()[3]), use_cuda) functions.plot_images(torch.cat( (labels.data, images.data, outputs.data), 0), use_cuda=use_cuda) else: functions.plot_images(torch.cat( (labels.data, images.data, outputs.data), 0), use_cuda=use_cuda) return psnrs
def hull_attempt( image ): # these methods seem ripe for command pattern (or strategy?) and/or just using Python's ability to pass methods in parameters orig = image.copy() # <---- RESIZING -----> # image, ratio = functions.standard_resize(image, new_width=100.0) # <---- RESIZING -----> # edged = functions.colorOps(image) # <---- TRIED THIS BUT DIDN'T WORK WELL -----> # # points = hullMethod(processed) # detection = cv2.drawContours(image = image.copy(), contours = points, contourIdx = -1, color = (0, 255, 0), thickness = 1) # for point in points: # final = functions.functions.finalize(orig, point, ratio) # images = [orig, detection, final] # functions.plot_images(images) # <---- SWITCHED TO THE CODE BELOW AND IT WORKS PRETTY WELL. NEED TO FILTER ON SIZE OR SOMETHING, TOO MANY BOXES-----> # processed = functions.closed_inversion(edged) functions.plot_images([edged, processed]) points = functions.hullRectMethod(processed)
def hough_blobbing(image): # doesn't really work. orig = image.copy() # <---- RESIZING -----> # downsized, ratio = functions.standard_resize(image, new_width=200.0) # <---- RESIZING -----> # edged = functions.downsized_text_edging(downsized.copy()) # blank = np.ones(edged.shape[:3], np.uint8) * 255 blank = np.zeros(edged.shape[:3], np.uint8) lines, drawn = hough.prob_hough(edged, blank) detected = downsized.copy() # boxes = functions.alternateRectMethod(drawn) boxes = functions.all_boxes(drawn) for box in boxes: detected = cv2.drawContours(detected, [box], 0, (0, 255, 0), 3) kernel = np.ones((3, 3), np.uint8) # original 9x9 dilated = cv2.dilate(drawn, kernel, iterations=5) # original was 5 iterations dilated = functions.closed_inversion(dilated) detected2 = downsized.copy() # boxes = functions.alternateRectMethod(dilated) boxes = functions.all_boxes(dilated) for box in boxes: detected2 = cv2.drawContours(detected2, [box], 0, (0, 255, 0), 3) functions.plot_images( [downsized, edged, drawn, dilated, detected, detected2])
def edging_comparisons(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gaussian = cv2.GaussianBlur(gray.copy(), (5, 5), 0) laplacian = cv2.Laplacian(gaussian,cv2.CV_64F) sobelx = cv2.Sobel(gaussian,cv2.CV_64F,1,0,ksize=5) sobely = cv2.Sobel(gaussian,cv2.CV_64F,0,1,ksize=5) canny = cv2.Canny(gaussian, threshold1 = 75, threshold2 = 200) # was 0, 50...not sure what these numbers mean abs_lap64f = np.absolute(laplacian) lap_8u = np.uint8(abs_lap64f) abs_sobel64f = np.absolute(sobelx) sobx_8u = np.uint8(abs_sobel64f) abs_sobel64f = np.absolute(sobely) soby_8u = np.uint8(abs_sobel64f) lap_thresh = cv2.adaptiveThreshold(src = lap_8u, maxValue = 25, adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ thresholdType = cv2.THRESH_BINARY, blockSize = 11, C = 2) sobx_thresh = cv2.adaptiveThreshold(src = sobx_8u, maxValue = 25, adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ thresholdType = cv2.THRESH_BINARY, blockSize = 11, C = 2) soby_thresh = cv2.adaptiveThreshold(src = soby_8u, maxValue = 25, adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ thresholdType = cv2.THRESH_BINARY, blockSize = 11, C = 2) canny_thresh = cv2.adaptiveThreshold(src = canny, maxValue = 25, adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ thresholdType = cv2.THRESH_BINARY, blockSize = 11, C = 2) titles = ["laplacian", "sobelx", "sobely", "canny"] functions.plot_images([laplacian, sobelx, sobely, canny], titles) titles = ["laplacian", "sobelx", "sobely", "canny_tresh"] functions.plot_images([lap_thresh, sobx_thresh, soby_thresh, canny_thresh], titles)
def downsized_canny_detection(image): downsized, ratio = functions.standard_resize(image, new_width = 100.0) edged, titles = canny(downsized) originals = utility.image_array(downsized, array_length = len(edged)) # detected = functions.process_several(images = edged, function = demo.vanilla_boxing) detected = functions.draw_several(images = edged, drawing_images = originals, function = demo.vanilla_box_drawing) functions.plot_images(edged, titles) functions.plot_images(detected,titles)
def standard_hough_rotation(theta, drawing_image): # rotation_angle = ((math.cos(theta) * 180) / math.pi) * -1 # rotation_angle = (90 - math.degrees(theta)) * -1 rotation_angle = (90 - math.degrees(theta)) * -1 rotated_image = functions.rotate(drawing_image, rotation_angle) print "most common theta is: " + str(theta) print "rotation angle is: " + str(rotation_angle) functions.plot_images([image, drawing_image, rotated_image], ["input image", "Std. Hough", "Rotated"])
def downsized_canny_dilated(image): downsized, ratio = functions.standard_resize(image) edged, titles = canny(downsized) originals = utility.image_array(downsized, array_length = len(edged)) kernel = np.ones((5,5),np.uint8) # original was 15x15. 5x5 is working well right now. dilated = functions.process_several(images = edged, function = cv2.dilate, kernel = kernel) detected = functions.draw_several(images = dilated, drawing_images = originals, function = demo.vanilla_box_drawing) functions.plot_images(dilated, titles) functions.plot_images(detected,titles)
def blur_comparison(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # gray = image.copy() bilateral = cv2.bilateralFilter(gray.copy(), 9, 75, 75) gaussian = cv2.GaussianBlur(gray.copy(), (5, 5), 0) bilateral_edged = cv2.Canny(bilateral, threshold1 = 75, threshold2 = 200) # was 0, 50...not sure what these numbers mean gaussian_edged = cv2.Canny(gaussian, threshold1 = 75, threshold2 = 200) # was 0, 50...not sure what these numbers mean titles = ["gray", "bilateral", "gaussian"] functions.plot_images([gray, bilateral_edged, gaussian_edged], titles)
def downsized_canny_CI(image): downsized, ratio = functions.standard_resize(image, new_width = 250.0) # edged, titles = canny_thresh(downsized) edged, titles = canny(downsized) originals = utility.image_array(downsized, array_length = len(edged)) closed_inverted = functions.process_several(images = edged, function = functions.closed_inversion) # detected = functions.process_several(images = closed_inverted, function = demo.vanilla_boxing) detected = functions.draw_several(images = closed_inverted, drawing_images = originals, function = demo.vanilla_box_drawing) functions.plot_images(edged,titles) # functions.plot_images(closed_inverted, titles) functions.plot_images(detected,titles)
def original_demo(image): orig = image.copy() # <---- RESIZING -----> # image, ratio = functions.standard_resize(image, new_width=100.0) # <---- RESIZING -----> # processed = functions.colorOps(image) points = functions.contour_method(processed) detection = cv2.drawContours(image.copy(), [points], -1, (0, 255, 0), 2) final = functions.finalize(orig, points, ratio) images = [orig, detection, final] functions.plot_images(images)
def text_region_method2(image): orig = image.copy() edged = functions.text_edging(orig.copy()) kernel = np.ones((9, 9), np.uint8) # original 9x9 dilated = cv2.dilate( edged, kernel, iterations=7) # original was 5 iterations. This works a little better? points = functions.minRectMethod(dilated) detection = cv2.drawContours(image.copy(), [points], -1, (0, 255, 0), 2) final = functions.finalize(orig.copy(), points) functions.plot_images( [orig, edged, dilated, detection, final], ["Original", "Edged", "Dilated", "Detection", "Final"])
def pipeline(image): orig = image.copy() downsized, ratio = functions.standard_resize(image, new_width=100.0) edged = functions.small_page_edging(downsized) processed = functions.closed_inversion(edged) boxes = functions.box_generation(processed) filtered_boxes = functions.box_filtration(boxes) detected = downsized.copy() rects = [] if not filtered_boxes: # print "FOUND NO BOXES; TRYING DIFFERENT CANNY" # edged = functions.text_edging(orig.copy()) # edged = functions.downsized_text_edging(downsized.copy()) edged = functions.smaller_page_edging(downsized) # rotated = hough.prob_hough_rotation(edged, orig.copy()) # detected = rotated processed = functions.closed_inversion(edged) boxes = functions.box_generation(processed) filtered_boxes = functions.box_filtration(boxes) final_box = functions.merge_boxes(filtered_boxes) if final_box: # final_box = final_box * ratio final_box = box[:, :] * ratio final_box = np.round(small_box) final_box = small_box.astype(int) warped = functions.perspective_transform(orig.copy(), final_box, ratio=ratio) lined = hough.standard_hough(warped) else: print("in demo pipeline") else: for box in boxes: # print box detected = cv2.drawContours(detected, [box], 0, (0, 255, 0), 1) rects.append(cv2.minAreaRect(box)) # print "rect in alternate_rect_attempt: " + str(cv2.minAreaRect(box)) if len(boxes) > 1: # print "got more than 1 box, attempting merge" merged = functions.merge_boxes(boxes) detected = cv2.drawContours(detected, [merged], 0, (255, 0, 0), 2) functions.plot_images( [edged, processed, detected], ["Edge Detection", "Morphological Operations", "Contour Finding"]) return detected
def hough_downsized(originals, images): downsized = functions.process_several(originals, function=reshape.standard_resize) processed = functions.process_several(images, function=functions.text_edging) downsized_processed = functions.process_several( processed, function=reshape.standard_resize, new_width=200.0, return_ratio=False) # rotated_images = functions.draw_several(downsized_processed, drawing_images = downsized_processed, function = prob_hough_rotation) rotated_images = functions.draw_several2( downsized_processed, drawing_images=downsized_processed, function=prob_hough) functions.plot_images(rotated_images) return rotated_images
def canny_comparison(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) canny = cv2.Canny(blurred, threshold1 = 0, threshold2 = 40, apertureSize=3) l2_canny = cv2.Canny(blurred, threshold1 = 0, threshold2 = 80, apertureSize=3 , L2gradient=False) # l2_canny = cv2.Canny(blurred, threshold1 = 0, threshold2 = 80, apertureSize=3) canny2 = cv2.Canny(blurred, threshold1 = 0, threshold2 = 100, apertureSize=3) l2_canny2 = cv2.Canny(blurred, threshold1 = 0, threshold2 = 160, apertureSize=3 , L2gradient=False) canny3 = cv2.Canny(blurred, threshold1 = 75, threshold2 = 200, apertureSize=3, L2gradient=False) canny4 = cv2.Canny(blurred, threshold1 = 0, threshold2 = 200, apertureSize=3) kernel = np.ones((5,5),np.uint8) # original was 15x15. 5x5 is working well right now. dilated_canny = cv2.dilate(canny, kernel, iterations=1) edged = [gray, canny, l2_canny, canny2, l2_canny2, canny3, canny4] dilated = functions.process_several(images = edged, function = cv2.dilate, kernel = kernel) # dilated_l2 = cv2.dilate(l2_canny, kernel, iterations=1) # functions.plot_images([gray, dilated_canny, dilated_l2, canny2, l2_canny2]) functions.plot_images(dilated)
def text_blobbing(image): orig = image.copy() small_orig = functions.standard_resize(image, new_width=100.0, return_ratio=False) start = time.clock() # downsized, ratio = functions.standard_resize(image, new_width = 100.0) edged = functions.text_edging(orig.copy()) # downsized, ratio = functions.standard_resize(edged, new_width = 200.0) # edged = functions.downsized_text_edging(downsized) # dilated = functions.closed_inversion(downsized) # kernel = np.ones((3,3),np.uint8) # was 9x9 # dilated = cv2.dilate(edged, kernel, iterations=3) kernel = np.ones((13, 13), np.uint8) # original 9x9 dilated = cv2.dilate(edged, kernel, iterations=7) # erosion = cv2.erode(dilated, kernel, iterations=2) # DOESN'T WORK ON VIDEO # functions.plot_images([dilated]) # boxes = functions.alternateRectMethod(edged) boxes = functions.box_generation(dilated) # boxes = functions.pureRectMethod(dilated) # WAS USING THIS ON VIDEOS WORKING FAIRLY WELL # boxes = functions.pureRectMethod(erosion) # detected = orig.copy() box = [] detected = small_orig.copy() if not boxes: # print "NO BOXES FOUND" final = detected else: detected = cv2.drawContours(orig, boxes, 0, (0, 255, 0), 3) box = boxes[0] final = functions.perspective_transform(orig.copy(), box) # for box in boxes: # detected = cv2.drawContours(detected,[box],0,(0,255,0),1) # box = boxes[0] # final = functions.perspective_transform(orig.copy(), box) end = time.clock() # print "Frame took " + (str (end-start)) + " time to process" functions.plot_images([orig, dilated, detected, final], ["orig", "dilated", "text_region_method", "final"])
def run_net(plot_images=False): psnrs = [] cnn.eval() for i, (images, labels) in enumerate(tst_loader): images, labels = functions.prepare_data(images, labels, use_cuda=use_cuda) outputs = cnn(images) cleans = images - outputs # res cleans = cleans.clamp(min=0, max=1) psnr = compute_psnr(cleans, labels) psnrs.append(psnr) if (plot_images): functions.plot_images(torch.cat( (labels.data, images.data, cleans.data), 0), use_cuda, num_cols=3) return psnrs
def prob_hough_rotation(image, drawing_image): lines, drawn_image = prob_hough(image, drawing_image) radians = dict() for line in lines: pt1, pt2 = line x1, y1 = pt1 x2, y2 = pt2 radian = round(calculate_radians((x1, y1), (x2, y2)), 1) if radian in radians.keys(): radians[radian] = radians[radian] + 1 else: radians[radian] = 1 freq_radian = max(radians, key=radians.get) # rotation_angle = (90 - math.degrees(freq_radian)) * -1 rotation_angle = math.degrees(freq_radian) print radians print "Most frequent radians is: " + str( freq_radian) + "Rotation angle is: " + str(rotation_angle) rotated_image = functions.rotate(drawn_image.copy(), rotation_angle) functions.plot_images([image, drawn_image, rotated_image], ["Input image", "HoughLinesP", "Rotated"]) return rotated_image
def occlusion_demo(image): orig = image.copy() # <---- RESIZING -----> # image, ratio = functions.standard_resize(image, new_width=100.0) # <---- RESIZING -----> # edged = functions.colorOps(image) processed = functions.closed_inversion(edged) points = functions.minRectMethod(processed) imutils.negative_coords(points, processed.shape[1], processed.shape[0]) # points = functions.minRectMethod(edged) detection = cv2.drawContours(image.copy(), [points], -1, (0, 255, 0), 2) final = functions.finalize(orig.copy(), points, ratio) # cv2.imwrite('processed/final.jpg', final) functions.plot_images([orig, edged, processed, detection, final], [ "Original", "Edge Detection", "Morpohological Operations", "Contour Finding", "Perspective Transform" ])
def downsized_text_blobbing(image): orig = image.copy() # <---- RESIZING -----> # image, ratio = functions.standard_resize(image, new_width=150.0) # <---- RESIZING -----> # # edged = functions.text_edging(orig.copy()) # edged = functions.text_edging(image.copy()) edged = functions.downsized_text_edging(image.copy()) kernel = np.ones((3, 3), np.uint8) # original 9x9 dilated = cv2.dilate(edged, kernel, iterations=3) # original was 5 iterations points = functions.box_generation(dilated) # print "# printing points in text_region_method2" + str(points) points = points[0] * ratio # print "# printing points in text_region_method2" + str(points) # detection = cv2.drawContours(image.copy(), [points], -1, (0, 255, 0), 2) # final = functions.finalize(orig.copy(), points) final = functions.perspective_transform(orig.copy(), points) functions.plot_images([orig, edged, dilated, final], ["Original", "Edged", "Dilated", "Final"])
def IOU_test(): rect = ((50.5, 52.0), (95.0, 102.0), -60.0) rect2 = ((50.0, 56.5), (82.0, 79.0), 5.0) rect = ((72.67567443847656, 47.554054260253906), (75.82424545288086, 50.249412536621094), -49.462323188781738) rect2 = ((53.00882339477539, 49.114704132080078), (26.536989212036133, 37.829050064086914), -4.39870548248291) # rect = ((50.5, 52.0), (95.0, 102.0), 5.0) # rect2 = ((50.0, 56.5), (82.0, 79.0), 5.0) # box1 = cv2.boxPoints(rect) # box2 = cv2.boxPoints(rect2) # print("box1 before int: " + str(box1)) # print("box2 before int: " + str(box2)) # box1 = np.int0(box1) # box2 = np.int0(box2) # tup1 = ((676, 227), (1073, 214), (1168, 761), (672, 791)) # tup2 = ((668, 221), (1093, 216), (1185, 778), (675, 807)) # box1 = np.array(list(tup1)) # box2 = np.array(list(tup2)) # box1 = np.int0(box1) # box2 = np.int0(box2) tup1 = ((543, 799), (573, 280), (972, 271), (1034, 773)) tup2 = ((547, 785), (579, 282), (955, 271), (1018, 755)) box1 = np.array(list(tup1)) box2 = np.array(list(tup2)) box1 = np.int32(box1) box2 = np.int32(box2) print(box1) print(box2) rect_conversion = cv2.minAreaRect(box2) print("rect conversion is " + str(rect_conversion)) print("box1 is " + str(box1)) print("box2 is " + str(box2)) iou1 = IOURect(box1) iou2 = IOURect(box2) start = time.clock() interior1 = iou1.interior_point_set() interior2 = iou2.interior_point_set() intersection = set.intersection(interior1, interior2) union = set.union(interior1, interior2) end = time.clock() print("IOU CALCULATION TOOK " + str(end - start) + " seconds") intersect_size = len(intersection) union_size = len(union) print(intersect_size) print(union_size) IOU = float(intersect_size) / float(union_size) print("IOU is " + str(IOU)) image_file = "iou_test.png" image_file = "../data/pics/demo/IMAG0603.jpg" image_file = '../frame18.jpg' image_file = '../frame38.jpg' image = cv2.imread(image_file) # image, _ = functions.standard_resize(image) orig = image.copy() detected = cv2.drawContours(orig, [box1], 0, (0, 255, 0), 5) detected = cv2.drawContours(orig, [box2], 0, (0, 255, 0), 5) inter = image.copy() for point in intersection: inter = cv2.circle(inter, point, 5, (0, 0, 255), -1) print(box1) print(box2) unioned = image.copy() # for point in interior1: for point in union: unioned = cv2.circle(unioned, point, 5, (255, 0, 0), -1) functions.plot_images([detected, inter, unioned])
def standard_hough(image, drawing_image): lines = [] count = dict() dst = np.ones(image.shape[:3], np.uint8) # dst = np.ones((h,w), np.uint8) # hough_lines = cv2.HoughLines(image, rho = 1, theta = 3.14/180, threshold = 75) # this works pretty well for normal "edged" input (assuming no illustrations in image) # hough_lines = cv2.HoughLines(image, rho = 1, theta = 3.14/180, threshold = 950) # need much higher threshold for "dilated" input, because is so clear # hough_lines = cv2.HoughLines(image, rho = 1, theta = 3.14/180, threshold = 40) # when using downsized image for corner finding, need very low threshold. This was working pretty well, but not on videos I guess hough_lines = cv2.HoughLines( image, rho=1, theta=3.14 / 180, threshold=30 ) # when using downsized image for corner finding, need very low threshold if hough_lines is None: print("NO LINES FOUND") else: print("number of lines found " + str(hough_lines.shape)) for hough_line in hough_lines: for rho, theta in hough_line: # print "rho: " + str(rho) + ". theta: " + str(theta) if theta in count.keys(): count[theta] = count[theta] + 1 else: count[theta] = 1 a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000 * (-b)) # all of these multiples were 1000 y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) cv2.line(drawing_image, (x1, y1), (x2, y2), (255, 0, 0), 1) cv2.line(dst, (x1, y1), (x2, y2), (255, 0, 0), 1) line = ((x1, y1), (x2, y2)) lines.append(line) # print count # for c in sorted(count, key=count.get, reverse=True): # print c, count[c] # if count: # theta = max(count, key=count.get) # print "found theta " + str(theta) # standard_hough_rotation(theta, drawing_image) # return lines, drawing_image if len(lines) > 30: print("found too many lines in standard hough") return drawing_image '''<------------COMMENTED THIS OUT 4/18 TRYING SOMETHING ELSE ----------------->''' # corner_count = find_corners(lines) # print("Found the following corners from find_corners in standard_hough: ") # print(corner_count) # corners = set() # # for group, point_set in corner_count.iteritems(): # for group, point_set in corner_count.items(): # points = np.vstack(list(point_set)) # x_average = int(np.mean(points[:, 0])) # y_average = int(np.mean(points[:, 1])) # corners.add((x_average,y_average)) # print(corners) '''<------------4/18 TRYING THIS ----------------->''' corners = all_corners(lines) # print("Ended up with " + str(len(corners)) + " after in-group merging in standard_hough") print("Found " + str(len(corners)) + " from all_corners") # corners = cv2.goodFeaturesToTrack(dst, 20, 0.01, 15) # drawing_image = cv2.cvtColor(drawing_image, cv2.COLOR_BGR2GRAY) # drawing_image = np.float32(drawing_image) # corners = cv2.cornerHarris(drawing_image,2,3,0.04) # functions.plot_images([corners]) # corners = np.int0(corners) if corners: # for my corner method # if type(corners) == np.ndarray: for corner in corners: x, y = corner print(corner) # x, y = corner.ravel() cv2.circle(drawing_image, (x, y), 2, (0, 0, 255), -1) # print corners # detected = image.copy() # boxes = functions.alternateRectMethod(detected) # h,w = image.shape[:2] # blank = np.ones((h,w,3), np.uint8) # if boxes: # for box in boxes: # detected = cv2.drawContours(blank,[box],0,(0,255,0),1) functions.plot_images( [image, dst, drawing_image], titles=["Canny edge detection", "Hough lines", "Hough corners"]) # functions.plot_images([image, drawing_image, dst, detected]) return drawing_image
def hough_cornering_draw(image, orig, crop_box): # added original for plotting original = image.copy( ) # necessary? Need to walk through these methods and see what they change downsized, ratio = reshape.standard_resize(image, new_width=100.0) # edged = edging.page_edging(downsized, thresh1=75, thresh2=200) # good edged = edging.new_page_edging(downsized, thresh1=0, thresh2=160) # edged = edging.auto_edging(downsized, sigma=0.33) # not great # edged = functions.orig_page_edging(downsized) # edged = functions.page_edging(downsized, thresh1=0, thresh2=120) # edged = functions.page_edging(original, thresh1=0, thresh2=120) # edged = functions.orig_page_edging(original) lines = standard_hough_lines(edged) lined = draw_lines(downsized.copy(), lines) corners = np.zeros(shape=(4, 2)) if len(lines) < 70: # corners = hough_corners(lines) # COMMENTED OUT 4/18 # print("FEWER than 30 lines...finding CORNERS") corners = all_corners(lines) # TRYING THIS 4/18 # print("Here are the corners in hough cornering") # print(corners) else: print("TOO MANY HOUGH LINES AT " + str(len(lines))) success = False filtered_corners = np.zeros(shape=(4, 2)) upsized_corners = np.zeros(shape=(4, 2)) upsized_filtered_corners = np.zeros(shape=(4, 2)) if len(corners) >= 4: # filtered_corners, success = corner_filtration(corner_array) # old method, didn't work. image_area = downsized.shape[0] * downsized.shape[1] filtered_corners, success = corner_filter(corners, image_area=image_area) # print("CORNERS after corner filtration") # print(filtered_corners) '''Compute bounding box size from corners. If too small, success = False. O OR add a check in the filtration method that ensures the range of all axes is reasonable Reason: came across a case where there were 4 points really close together that the logic interpreted as valid because it is using the global range defined in the method from the corners there ''' # print("corners before upsizeing") # print(corners) # print(type(corners)) upsized_corners = corners * ratio upsized_filtered_corners = filtered_corners * ratio upsized_corners = np.int0(upsized_corners) upsized_filtered_corners = np.int0(upsized_filtered_corners) xmin, xmax, ymin, ymax = boxing.max_points(crop_box) # print("UPSIZED CORNERS") # print(upsized_corners) # upsized_corners[:, 0] += xmin # upsized_corners[:, 1] += ymin # xmin, xmax, ymin, ymax = boxing.max_points(upsized_filtered_corners) # upsized_filtered_corners[:, 0] += xmin # upsized_filtered_corners[:, 1] += ymin upsized_corners[:, 0] = np.add(upsized_corners[:, 0], xmin, out=upsized_corners[:, 0], casting="unsafe") upsized_corners[:, 1] = np.add(upsized_corners[:, 1], ymin, out=upsized_corners[:, 1], casting="unsafe") upsized_filtered_corners[:, 0] = np.add(upsized_filtered_corners[:, 0], xmin, out=upsized_filtered_corners[:, 0], casting="unsafe") upsized_filtered_corners[:, 1] = np.add(upsized_filtered_corners[:, 1], ymin, out=upsized_filtered_corners[:, 1], casting="unsafe") # upsized_corners = tuple(map(tuple, upsized_corners)) # upsized_filtered_corners = tuple(map(tuple, upsized_filtered_corners)) corners = np.int32(corners) corners = tuple(map(tuple, corners)) filtered_corners = np.int32(filtered_corners) filtered_corners = tuple(map(tuple, filtered_corners)) upsized_corners = np.int32(upsized_corners) upsized_filtered_corners = np.int32(upsized_filtered_corners) upsized_corners = tuple(map(tuple, upsized_corners)) upsized_filtered_corners = tuple(map(tuple, upsized_filtered_corners)) cornered = draw_corners(downsized, corners) # cornered = draw_corners(original, upsized_corners) # cornered = draw_corners(orig, upsized_corners) # was here if success: cornered = draw_corners(cornered, filtered_corners, color=(255, 0, 0)) # cornered = draw_corners(cornered, upsized_filtered_corners, color=(255,0,0)) # was here # warped = reshape.perspective_transform(image=original, points=filtered_corners, ratio=ratio) warped = reshape.perspective_transform(image=orig, points=upsized_filtered_corners) threshed = coloring.thresholding(warped) # functions.plot_images([orig, lined, cornered, threshed], titles=['Original', 'Hough lines', 'Corners', 'Transform']) functions.plot_images([orig, image, edged, lined, cornered, threshed], titles=[ 'Original', 'Cropped', 'Canny', 'Hough lines', 'Corners', 'Transform' ]) # was here # functions.plot_images([original, edged, lined, cornered, threshed], titles=['Original', 'edged', 'Hough lines', 'Corners', 'Transform']) else: # functions.plot_images([orig, image, edged, lined], titles=['Original', 'Cropped', 'Edged', 'Lined']) # was here functions.plot_images([orig, downsized, edged, lined], titles=['Original', 'Cropped', 'Edged', 'Lined']) # was here # print('hello dolly') '''This works when 4 corners are passed in. Need to figure out how to filter down to 4 corners or skip it if no corners found.''' # functions.plot_images([image, edged, lined, cornered], titles=['image', 'edged', 'lined', 'cornered']) # functions.plot_images([image, lined, cornered, warped]) return upsized_filtered_corners, success
def detection(image): original = image.copy() downsized, ratio = functions.standard_resize(image, new_width=100.0) # downsized = functions.shadow_removal(image=downsized) downsized = functions.gamma_correction(image=downsized, correction=2) # downs = functions.masking_attempt(image=original) merged, boxes, edged, closed_invert = boxes_from_edged( downsized, edging_function=functions.orig_page_edging) # merged, boxes, edged, closed_invert = boxes_from_edged(image=downsized, edging_function = functions.page_edging, thresh1=75, thresh2=220) functions.plot_images([edged, closed_invert], titles=["First pass", "First pass"]) detected = image.copy() print("type of boxes : " + str(type(boxes))) # if not type(boxes) == np.ndarray or not boxes: if not type(merged) == np.ndarray: print("GOT NO BOXES, TRYING SMALL PAGE EDGING") # merged, boxes, edged, closed_invert = boxes_from_edged(downsized, edging_function = functions.small_page_edging) merged, boxes, edged, closed_invert = boxes_from_edged( downsized, edging_function=functions.page_edging, thresh1=0, thresh2=160) functions.plot_images([edged, closed_invert], titles=["Second pass", "Second pass"]) # if not type(boxes) == np.ndarray or not boxes: if not type(merged) == np.ndarray: print("GOT NO BOXES, TRYING SMALLER!! PAGE EDGING") # merged, boxes, edged, closed_invert = boxes_from_edged(downsized, edging_function = functions.smaller_page_edging) merged, boxes, edged, closed_invert = boxes_from_edged( downsized, edging_function=functions.page_edging, thresh1=0, thresh2=120) functions.plot_images([edged, closed_invert], titles=["Third pass", "Third pass"]) # if type(boxes) == np.ndarray or boxes: if type(merged) == np.ndarray: print("DRAWING THE BOX") # detected = cv2.drawContours(downsized.copy(), contours = boxes, contourIdx = -1, color = (0,255,0), thickness = 1) # if passing in a list and want to draw more than 1 detected = cv2.drawContours( downsized.copy(), contours=[merged], contourIdx=-1, color=(0, 255, 0), thickness=1) # if just drawing one (using merge boxes) # all_boxes = cv2.drawContours(downsized.copy(), contours = boxes, contourIdx = -1, color = (255,0,0), thickness = 1) # if just drawing one (using merge boxes) all_boxes = cv2.drawContours( downsized.copy(), contours=boxes, contourIdx=-1, color=(0, 255, 0), thickness=1) # if just drawing one (using merge boxes) else: print("FOUND NO BOXES AT ALL!!!!") functions.plot_images( [original, edged, closed_invert, detected], titles=["Original", "Edged", "Closed inversion", "Detected"]) return # warped = functions.perspective_transform(original, boxes, ratio) box = merged.reshape(4, 2) * ratio box = np.int0(box) cropped = functions.box_crop(original.copy(), box) downsize_cropped = functions.standard_resize(image=cropped, new_width=100.0, return_ratio=False) '''Was the idea here to potentially take a second pass at this now-cropped image? Just with Hough, or with same approach? Or use prob_hough_rotation?''' blob_cropped = functions.text_blobbing(downsize_cropped.copy()) edge_cropped = functions.text_edging(downsize_cropped.copy()) edge_cropped2 = functions.downsized_text_edging(downsize_cropped.copy()) print("edged cropped size is " + str(edge_cropped.shape)) # lines = hough.standard_hough_lines(edged.copy()) # corners = hough.hough_corners(lines) # cornered = hough.draw_corners(downsized.copy(), corners) # functions.plot_images([original, edged, closed_invert, all_boxes, detected, cropped, edge_cropped, edge_cropped2], titles = ["original", "Edged", "closed_invert", "all boxes", "detected", "cropped", "edge_cropped", "edge_cropped2"]) functions.plot_images( [original, closed_invert, all_boxes, cropped], titles=["Original", "Close + Invert", "All boxes", "Cropped"])
def text_regions(image): vis = image.copy() # image = cv2.imread('../service_form.png', 0) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # mser = cv2.MSER() # was working but now isnt? version change? mser = cv2.MSER_create() # trying this # regions = mser.detect(image, None) regions = mser.detectRegions(image, None) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv2.polylines(vis, hulls, 1, (0, 255, 0)) mask = np.zeros((image.shape[0], image.shape[1], 1), dtype=np.uint8) for contour in hulls: cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1) #this is used to find only text regions, remaining are ignored text_only = cv2.bitwise_and(image, image, mask=mask) # text_only = functions.standard_resize(text_only) # text_only = cv2.bitwise_not(src = text_only.copy()) return text_only if __name__ == '__main__': image = cv2.imread(sys.argv[1]) text_only = text_regions(image) functions.plot_images([text_only])
def hough_cornering(image, orig): # added original for plotting original = image.copy( ) # necessary? Need to walk through these methods and see what they change downsized, ratio = reshape.standard_resize(image, new_width=100.0) # edged = edging.orig_page_edging(downsized) # bad edged = edging.page_edging(downsized, thresh1=0, thresh2=160) # good # edged = edging.auto_edging(downsized, sigma=0.5) # not great lines = standard_hough_lines(edged) lined = draw_lines(downsized.copy(), lines) # functions.plot_image(edged) corners = set() if len(lines) < 70: # corners = hough_corners(lines) # COMMENTED OUT 4/18 print("FEWER than 30 lines...finding CORNERS") corners = all_corners(lines) # TRYING THIS 4/18 print("Here are the corners in hough cornering") print(corners) else: print("TOO MANY HOUGH LINES AT " + str(len(lines))) success = False if len(corners) >= 4: # filtered_corners, success = corner_filtration(corner_array) # old method, didn't work. image_area = downsized.shape[0] * downsized.shape[1] filtered_corners, success = corner_filter(corners, image_area=image_area) print("CORNERS after corner filtration") print(filtered_corners) '''Compute bounding box size from corners. If too small, success = False. O OR add a check in the filtration method that ensures the range of all axes is reasonable Reason: came across a case where there were 4 points really close together that the logic interpreted as valid because it is using the global range defined in the method from the corners there ''' upsized_corners = corners * ratio upsized_filtered_corners = filtered_corners * ratio upsized_corners = np.int0(upsized_corners) upsized_filtered_corners = np.int0(upsized_filtered_corners) upsized_corners = tuple(map(tuple, upsized_corners)) upsized_filtered_corners = tuple(map(tuple, upsized_filtered_corners)) # cornered = draw_corners(downsized, corners) cornered = draw_corners(original, upsized_corners) if success: # cornered = draw_corners(cornered, filtered_corners, color=(255,0,0)) cornered = draw_corners(cornered, upsized_filtered_corners, color=(255, 0, 0)) warped = reshape.perspective_transform(image=original, points=filtered_corners, ratio=ratio) threshed = coloring.thresholding(warped) # functions.plot_images([image, lined, cornered, threshed], titles=['Original', 'Hough lines', 'Corners', 'Transform']) functions.plot_images([orig, image, edged, lined, cornered, threshed], titles=[ 'Original', 'Cropped', 'Canny', 'Hough lines', 'Corners', 'Transform' ]) # functions.plot_images([original, edged, lined, cornered, threshed], titles=['Original', 'edged', 'Hough lines', 'Corners', 'Transform']) else: functions.plot_images([image, edged, lined, cornered], titles=['image', 'edged', 'lined', 'cornered'])
# files = utility.filenames_at_path("/media/thor/LEXAR/sampleDataset/input_sample", ".jpg") # hough threshold of 30 is best here # files = utility.filenames_at_path("/home/thor/code/sr_project/pics/forms", ".jpg") originals, images = utility.image_reading(files[:20]) for image in images: detection(image.copy()) # text_region_method(image.copy()) # downsized_text_blobbing(image.copy()) originals, images = utility.image_reading(files) # results = functions.process_several(images, function = pipeline) # results = functions.process_several(images, function = downsized_text_blobbing) results = functions.process_several(images, function=text_region_method) functions.plot_images(results, files) # text_region_method2(image) # text_region_method(image) # downsized_text_blobbing(image) # canny_comparison(image) # dilation_canny(image) # rotate(image, 20) # occlusion_demo(image) # original_demo(image) hull_attempt(image) # alternate_rect_attempt(image) # prob_hough(image)
def prob_hough(image): orig = image.copy() edged = functions.text_edging(image) rotated = hough.prob_hough_rotation(edged, orig) functions.plot_images([orig, edged, rotated], ["Original", "Edged", "Rotated"])
closedEdges = cv2.morphologyEx( bds_edged, cv2.MORPH_CLOSE, kernel=np.ones((5, 11)) ) # this potentially helps close Canny edges that are close but not quite touching closedInvert = cv2.bitwise_not(src=closedEdges.copy()) titles = [ "orig", "dilate_edged", "border->dilate->shrink", "closed_invert", "dilate->shrink->border" ] images = [orig, dilate_edged, bds_edged, closedInvert, dsb_edged] # images = [blurred, opening, dilation, dilate_edged] # it looks like 'opening has a postive effect on text readability # titles = ["Orig", "open", "dilation", "dilate_edged"] functions.plot_images(images, titles) edges = cv2.Canny( blurred, 0, CANNY, apertureSize=3 ) # this is the original from this process. Below is what's working in other conditions # edges = cv2.Canny(blurred, threshold1 = 75, threshold2 = 200) # was 0, 50...not sure what these numbers mean blackhat = cv2.morphologyEx(edges, cv2.MORPH_BLACKHAT, kernel) tophat = cv2.morphologyEx(edges, cv2.MORPH_TOPHAT, kernel) gradient = cv2.morphologyEx( dilation, cv2.MORPH_GRADIENT, kernel ) # this looks good!! (input original image or the canny edged photo) # gradient = cv2.morphologyEx(dilate_edged, cv2.MORPH_GRADIENT, kernel) # gradient = cv2.morphologyEx(edges, cv2.MORPH_GRADIENT, kernel) height, width = gradient.shape[:2]
blurred, threshold1=75, threshold2=200) # was 0, 50...not sure what these numbers mean closedEdges = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel=np.ones( (5, 11))) # added this 11/2/17. Trying to work with document occlusion. # mask = np.ones(closedEdges.shape,np.uint8) closed2 = closedEdges.copy() # cv2.bitwise_and(closed2,closed2,mask) cv2.bitwise_not(closedEdges, closed2) images = [image, edged] images = [edged, closedEdges] images = [closedEdges, closed2] functions.plot_images(images) # find the contours in the edged image, keeping only the # largest ones, and initialize the screen contour #(contours, _) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # original line ('too many values to unpack') ims, contours, hierarchy = cv2.findContours( edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # modified line with CHAIN_APPROX_NONE # ims, contours, hierarchy = cv2.findContours(closedEdges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # trying CHAIN_APPROX_SIMPLE...DOES THE SAME THING # ims, contours, hierarchy = cv2.findContours(closed2.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # trying CHAIN_APPROX_SIMPLE...DOES THE SAME THING contours = sorted( contours, key=cv2.contourArea, reverse=True)[:5] # added the [:5] per the kickass scanner example # get approximate contour