Reputation: 3375
I want to find rectangles in the figures.
I can find them when outside of the boxes are not interrupted with writings with the code below. However when the box is interrupted with handwriting I cannot find it, that is the part that I need help.
The code:
import cv2
import numpy as np
import matplotlib.pyplot as pp
image = cv2.imread('original_image.jpeg')
blur = cv2.pyrMeanShiftFiltering(image, 11, 21)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.015 * peri, True)
if len(approx) == 4:
print(approx)
x,y,w,h = cv2.boundingRect(approx)
if(w > 10 and h > 10):
print(x,y,w,h)
cv2.rectangle(image,(x,y),(x+w,y+h),(36,255,12),2)
cv2.imshow('thresh', thresh)
cv2.waitKey()
cv2.imshow('image', image)
Original Image:
Interrupted Image with handwriting:
Boxes found on original image:
How can I find the box in the interrupted picture?
Upvotes: 3
Views: 210
Reputation: 3421
You can use my answer which is a solution to find all the checkboxes from a given form to get the boxes. https://stackoverflow.com/a/62801071/9605907 The steps are basically like this.
cv2.connectedComponentsWithStats
I tried running the code with your input image and this was the result I got,
You can get the code from this link.
Upvotes: 3