I am new in python and I created anaconda environment and running given code but it's showing error
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# our approximated contour should have four points
if len(approx) == 4:
screenCnt = approx
break
# show the contour (outline) of the piece of paper
#print("STEP 2: Find contours of paper")
cv2.drawContours(orig, [screenCnt], -1, (0, 255, 0), 2)
# apply the four point transform to obtain a top-down
# view of the original image
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
Error is
NameError Traceback (most recent call last) in 122 # show the contour (outline) of the piece of paper 123 #print("STEP 2: Find contours of paper") --> 124 cv2.drawContours(orig, [screenCnt], -1, (0, 255, 0), 2) 125 126 # apply the four point transform to obtain a top-down
NameError: name 'screenCnt' is not defined
but in code I already defined screenCnt. initially, this code was running properly but now it's not working or it's show error after every alternate execution. (it run properly the first time and when restart kernel giving same error).
or if i set screenCnt = 0 or screenCnt = None it giving error
error Traceback (most recent call last) in 123 # show the contour (outline) of the piece of paper 124 #print("STEP 2: Find contours of paper") --> 125 cv2.drawContours(orig, [screenCnt], -1, (0, 255, 0), 2) 126 127 # apply the four point transform to obtain a top-down
error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2509: error: (-215:Assertion failed) npoints > 0 in function 'cv::drawContours'
looking at your code, probably the statement screenCnt = approx
is never hit during the execution. Try initializing screenCnt
to some default value.
Looks like the screenCnt variables in lines 81 and 83 are not of the same. Or either you are trying to access the screenCnt which is defined locally within the scope of the if statement.
Obviously, neither 0
nor none
meets the data type required by the second parameter of cv2.drawcontours, The specific details of the function can be seen here https://docs.opencv.org/4.x/d4/d73/tutorial_py_contours_begin.html
Analyze the data type. cnts
is a set containing multiple contours. Each contour taken out during the cycle is defined as cnt
. cnt
is a point set defined by multiple two-tuples, so we can initialize it like this screenCnt = [[0,0], [255,0], [255,255], [0,255]]
to initialize with a contour.
It is showing not defined because the program could not detect 4 definite points in the image. Try to place the paper on a dark background.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.