From 23e7f40fc12fd1a63b5974cb12fda0f2127a48c1 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Wed, 9 Oct 2024 09:18:29 -0400 Subject: [PATCH] Add example of finding rectangles --- notes/find_rectangles.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 notes/find_rectangles.py diff --git a/notes/find_rectangles.py b/notes/find_rectangles.py new file mode 100644 index 0000000..71b12f5 --- /dev/null +++ b/notes/find_rectangles.py @@ -0,0 +1,28 @@ +import cv2 + +img = cv2.imread('demo-hand-written.png') +gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) + +thresh_inv = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1] + +# Blur the image +blur = cv2.GaussianBlur(thresh_inv,(1,1),0) + +thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] + +# find contours +contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0] + +mask = np.ones(img.shape[:2], dtype="uint8") * 255 +for c in contours: + # get the bounding rect + x, y, w, h = cv2.boundingRect(c) + if w*h>1000: + cv2.rectangle(mask, (x, y), (x+w, y+h), (0, 0, 255), -1) + +res_final = cv2.bitwise_and(img, img, mask=cv2.bitwise_not(mask)) + +cv2.imshow("boxes", mask) +cv2.imshow("final image", res_final) +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file