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 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 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 detection(image): downsized = functions.standard_resize() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(blurred, threshold1 = 0, threshold2 = 140, apertureSize=3, L2gradient=True) closed_inversion = functions.closed_inversion(edged) boxes = functions.alternateRectMethod(closed_inversion) return boxes
def boxes_from_edged(image, edging_function, **edging_args): edged = edging_function( image, **edging_args ) # perform edge detection on the input image according to the passed in edging function closed_invert = functions.closed_inversion( edged ) # perform closed inversion (morphological closing + color inversion) on the edged image boxes = functions.box_generation( closed_invert ) # generate bounding boxes from the closed inversion output boxes = functions.box_filtration(boxes, image.shape[1], image.shape[0]) # filter bounding boxes merged = functions.merge_boxes(boxes) return merged, boxes, edged, closed_invert
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" ])
HOUGH = 25 image = cv2.imread(sys.argv[1]) orig = image.copy() # <---- RESIZING -----> # height, width = image.shape[:2] new_width = 100.0 scaling_factor = new_width / width ratio = 1 / scaling_factor # image = imutils.resize_new(image, scaling_factor = scaling_factor) # <---- RESIZING -----> # # <---- FUNCTIONS.PY PROCESS -----> # edged = functions.colorOps(image) closed_inversion = functions.closed_inversion(edged) images = [edged, closed_inversion] titles = ["edged", "closed_inversion"] # functions.plot_images(images, titles) # <---- FUNCTIONS.PY PROCESS -----> # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) bordered = cv2.copyMakeBorder( blurred, 15, 15, 15, 15, cv2.BORDER_CONSTANT, value=[ 0, 0, 0 ]) # added this 11/2/17. Trying to work with document occlusion. # cv2.GaussianBlur(gray, (3,3), 0, gray) # this is the original from this process # this is to recognize white on white
import numpy as np import cv2 import functions import sys if __name__ == '__main__': img = cv2.imread(sys.argv[1]) Z = img.reshape((-1, 3)) # convert to np.float32 Z = np.float32(Z) # define criteria, number of clusters(K) and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) K = 2 ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) # Now convert back into uint8, and make original image center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((img.shape)) res2 = functions.colorOps(res2) res2 = functions.closed_inversion(res2) functions.plot_images([res2])