rectangle, Contour detection with python3, opencv3

Multi tool use
Multi tool use


rectangle, Contour detection with python3, opencv3



I want to detect paper sheet from image. i applied medianBlur, Canny , dilate, threshold, etc. algorithms to find.i am able to find sheet but don't know how to crop rectangle and apply transformation



answer sheet





This my code


import numpy as np
import cv2

image = cv2.imread('im_1.jpg')
image = cv2.resize(image, (800, 600))
draw = np.zeros_like(image)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
thresh = cv2.erode(thresh, kernel, iterations=4)
thresh = cv2.dilate(thresh, kernel, iterations=4)
im, cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

max_area = -1
max_c = 0
for i in range(len(cnts)):
contour = cnts[i]
area = cv2.contourArea(contour)
if (area > max_area):
max_area = area
max_c = i

contour = cnts[max_c]


rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)



cv2.drawContours(image, [box],-1, (0, 255, 0), 2)

cv2.imshow('Sheet', image)

cv2.waitKey(0)
cv2.destroyAllWindows()



result of code:
result







unfortunately, image is not directly visible
– Abdulaziz G'aybullayev
Jul 1 at 18:05





Doing this would be far tougher and error-prone in comparison to conventional systems.
– Ubdus Samad
Jul 1 at 19:08




1 Answer
1



There are some minor flaws in your approach. The following code will help. I have mentioned the changes made as well.



Code:


import numpy as np
import cv2

image = cv2.imread('C:/Users/Jackson/Desktop/score.jpg')

#--- Resized the image to half its dimension maintaining the aspect ratio ---
image = cv2.resize(image, (0, 0), fx = 0.5, fy = 0.5)
draw = np.zeros_like(image)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

#--- I found the inverse binary image, because contours are found for objects in white. Since the border of the page is in black you have to invert the binary image. This is where it went wrong.
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)

#--- I did not perform any morphological operation ---
im, cnts, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

max_area = -1
max_c = 0
for i in range(len(cnts)):
contour = cnts[i]
area = cv2.contourArea(contour)
if (area > max_area):
max_area = area
max_c = i

contour = cnts[max_c]


rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)

cv2.drawContours(image, [box],-1, (0, 255, 0), 2)

cv2.imshow('Sheet', image)
cv2.waitKey(0)
cv2.destroyAllWindows()



Result:



enter image description here





Thank you bro for your help
– Abdulaziz G'aybullayev
Jul 3 at 6:39






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

XZeA3MnMDTul5U356fvPk r
H7,65U0z FvT 7RxicUriTYfGI53PDgF,Xf L M6spyJa5u9pW,WAlt1

Popular posts from this blog

Rothschild family

Cinema of Italy