Ask Your Question
0

How to use pointPolygonTest function for a polygon

asked 2020-08-15 16:19:32 -0600

Ajay gravatar image

updated 2020-08-16 02:14:36 -0600

berak gravatar image

I am trying to run polygonpoint test function for a polygon. My code looks something like below, does it only meant for coutours ?

import ast
import cv2
import numpy as np
point1 = (25, 50)
t = "201.94,191.31;158.20,343.59;520.55,361.55;469.79,175.70" # closed polygon 
pts = [*map(ast.literal_eval, t.split(';'))]
pts = np.array(pts)
cv2.pointPolygonTest(pts, point1, False)

I am getting below error

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-454-bfdc7e835e9b> in <module>
----> 1 cv2.pointPolygonTest(pts, point1, False)

error: OpenCV(4.3.0) /io/opencv/modules/imgproc/src/geometry.cpp:103: error: (-215:Assertion failed) total >= 0 && (depth == CV_32S || depth == CV_32F) in function 'pointPolygonTest'
edit retag flag offensive close merge delete

Comments

1

well, if it complains about the type, why don't you go and check it ?

berak gravatar imageberak ( 2020-08-15 20:55:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-08-16 02:02:14 -0600

berak gravatar image

opencv, being a c++ library, is far more pesky about datatypes, than your usual python code, so when in doubt, you have to CHECK !

see:

>>> pts = [*map(ast.literal_eval, t.split(';'))]
>>> pts = np.array(pts)
>>> pts.dtype
dtype('float64')     ## aaaaaaaaarghhh !

so, it needs a conversion !

>>> pts = np.array(pts, dtype=np.float32)
>>> pts.dtype
dtype('float32')
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-08-15 16:19:32 -0600

Seen: 875 times

Last updated: Aug 16 '20