Add example of finding rectangles

This commit is contained in:
Matt McWilliams 2024-10-09 09:18:29 -04:00
parent 3b23fc9578
commit 23e7f40fc1
1 changed files with 28 additions and 0 deletions

28
notes/find_rectangles.py Normal file
View File

@ -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()