def SplittingRecursive(image, left, right, top, bottom, depth=0): cx, cy = Task2.FindCentroid(image, left, right, top, bottom) # print("(", top, "\t", left, ")\t(", bottom, "\t", right, ")\t", cx, "\t", cy, "\tDepth: ", depth) if depth < 3: SplittingRecursive(image, left, cy, top, cx, depth + 1) # Top Left SplittingRecursive(image, cy, right, top, cx, depth + 1) # Bottom Left SplittingRecursive(image, left, cy, cx, bottom, depth + 1) # Top Right SplittingRecursive(image, cy, right, cx, bottom, depth + 1) # Bottom Right else: t = Task4.B2W_Transitions(image, left, right, top, bottom) r = Task5.AspectRatio(left, right, top, bottom) filePath = "../Text/" # If Path Does not exists; Create it if not os.path.exists(filePath): os.makedirs(filePath + "Transitions/") os.makedirs(filePath + "Ratios/") os.makedirs(filePath + "Centroids/") TransitionsFile = open(filePath + "Transitions/" + "signature.txt", "a") TransitionsFile.write(str(t) + "\n") TransitionsFile.close() RatiosFile = open(filePath + "Ratios/" + "signature.txt", "a") RatiosFile.write(str(r) + "\n") RatiosFile.close() CentroidsFile = open(filePath + "Centroids/" + "signature.txt", "a") CentroidsFile.write(str(cx) + "," + str(cy) + "\n") CentroidsFile.close() return cv2.rectangle(bin_image, (top, left), (bottom, right), (0,255,0), 1)
def SplittingRecursive(image, left, right, top, bottom, depth=0): cx, cy, n = Task2.FindCentroid(image, left, right, top, bottom) if depth < 3: SplittingRecursive(image, left, cx, top, cy, depth + 1) # Top Left SplittingRecursive(image, cx, right, top, cy, depth + 1) # Bottom Left SplittingRecursive(image, left, cx, cy, bottom, depth + 1) # Top Right SplittingRecursive(image, cx, right, cy, bottom, depth + 1) # Bottom Right else: rectangles.append([(left, top), (right, bottom)]) transitions.append(Task4.B2W_Transitions(image[top:bottom, left:right])) ratios.append(Task5.AspectRatio(left, right, top, bottom)) # Task 6 size = (bottom - top) * (right - left) blacks = Task6.blackPixels(image, left, right, top, bottom) try: normalized.append(size / blacks) except: normalized.append(0) cx, cy, n = Task2.FindCentroid(image[top:bottom, left:right], 0, right - left, 0, bottom - top) centroids.append((cx, cy)) # Task 7 angle = math.degrees( math.acos( (bottom - top - cy) / (math.sqrt((right - left - cx)**2 + (bottom - top - cy)**2)))) angles.append(angle)
top, bottom, left, right, bounding_box_image = Task1.BoundingBox(width, height, bin_image, filename) B = (left, right, top, bottom) # cv2.imshow("Bounding Box", bounding_box_image) # Task 2 filename = "cen_" + filename centroid_image, cx, cy = Task2.FindCentroid(width, height, bin_image, bounding_box_image, filename) C = (cx, cy) # cv2.imshow("Centroid", centroid_image) # Task 3 cx = int(cx) cy = int(cy) filename = "seg_" + filename top_left, bottom_left, top_right, bottom_right, segmented_image = Task3.DivideBoundingBox(centroid_image, top, bottom, left, right, cx, cy, filename) cv2.imshow("Top Left", top_left) cv2.imshow("Bottom Left", bottom_left) cv2.imshow("Top Right", top_right) cv2.imshow("Bottom Right", bottom_right) print("\nNumber of Black to White Transitions") # Task 4 TL = Task4.B2W_Transitions(top_left, top_left.shape[0], top_left.shape[1], "Top Left") BL = Task4.B2W_Transitions(bottom_left, bottom_left.shape[0], bottom_left.shape[1], "Bottom Left") TR = Task4.B2W_Transitions(top_right, top_right.shape[0], top_right.shape[1], "Top Right") BR = Task4.B2W_Transitions(bottom_right, bottom_right.shape[0], bottom_right.shape[1], "Bottom Right") T = (TL, TR, BL, BR) cv2.waitKey(0)