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)
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)
bin_image = Task0.Binarization(image, filename) filename = filename.split('/')[-1] # Task 1 height, width = bin_image.shape filename = path + "box_" + filename top, bottom, left, right = Task1.BoundingBox(bin_image, height, width) bounding_box_image = cv2.rectangle(bin_image, (top, left), (bottom, right), (0,255,0), 3) cv2.imwrite(filename, bounding_box_image) B = (left, right, top, bottom) filename = filename.split('/')[-1] # Task 2 filename = path + "cen_" + filename cx, cy = Task2.FindCentroid(bin_image, 0, bin_image.shape[1], 0, bin_image.shape[0]) centroid_image = cv2.circle(bounding_box_image, (cy, cx), 10, 200, -1) cv2.imwrite(filename, centroid_image) C = (cx, cy) filename = filename.split('/')[-1] # Task 3 filename = path + "seg_" + filename top_left, bottom_left, top_right, bottom_right, segmented_image = Task3.DivideBoundingBox(centroid_image, top, bottom, left, right, cx, cy) cv2.imwrite(filename, segmented_image) filename = filename.split('/')[-1] CleaningDirectories("../Text/") image = SplittingRecursive(bin_image, left, right, top, bottom, 0) cv2.imshow("image", image)
# Task 0 filename = "bin_" + filename bin_image = Task0.Binarization(image, filename) # cv2.imshow("Binarization", bin_image) # Task 1 width, height = bin_image.shape filename = "box_" + filename 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")