pointPolygonTest() throws error for some reason
I am using the following piece of code to check if a point is inside the contour or not:
# form the contour from points
cnt = []
for pose in component.part_outline.outline_poses.poses:
point = (pose.position.x, pose.position.y)
cnt.append(point)
cnt = np.array(cnt)
# get the point to be tested
point = (screw.pose.position.x, screw.pose.position.y)
# run the test
is_inside = cv2.pointPolygonTest(cnt,point, False)
if is_inside > 0:
print("bla bla") # act upon the outcome
and I get the following error for some reason:
"error: OpenCV(3.4.4) /io/opencv/modules/imgproc/src/geometry.cpp:103: error: (-215:Assertion failed) total >= 0 && (depth == CV_32S || depth == CV_32F) in function 'pointPolygonTest'\n\n"
This error message does not tell much to be honest, and I have absolutely no idea why this is happening. I checked the image, it's there. The contour and the point are also there, nothing is null or whatsoever.
P.S: In case if it helps, I tried to print the variable is_inside
and it prints (-1.0) for 4 times, then the error is thrown.
Any idea?
Looks like it needs integer or 32 bits floating point data. Check the type of
pose.position.x
and the type ofscrew.pose.position.x
.There is also a tutorial code here.
(466, 43) (102, 37) (682, 458) (468, 459) (466, 43)
these are the points that I feed into, what's wrong with these anyway?
Okay, turns out one has to use int() to cast each value, as you said. I can accept your answer if you add it.
@Raki. Does your is working or not?
@supra56 Mine works now yes.