Hi everyone,
i am working to recognize if i am framing a document, calculating a rectangle plus adding other algorithms that do not concern opencv.
Everything works fine if I am on surfaces with a dark background.
If I am on a surface with a white background or similar, I cannot generate the rectangle correctly.
This is the test image pat.jpg - Google Drive
This is my actual code
frame = cv2.imread('pat.jpg')
MORPH = 9
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(frame, (5, 5), 0)
thresh = cv2.Canny(blur, 0, 84)
#kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (MORPH, MORPH))
#thresh = cv2.dilate(thresh, kernel, iterations=7)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = sorted(cnts, key=lambda x: cv2.contourArea(x), reverse=True)
#loop over the contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# if our approximated contour has four points, then we
# can assume that we have found our screen
if (len(approx) == 4 and cv2.contourArea(approx) >= 1000
and cv2.isContourConvex(approx)):
screenCnt = c
cv2.drawContours(frame, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", frame)
cv2.waitKey(0)
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()