# How to use pointPolygonTest function for a polygon

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 close merge delete

1

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

Sort by » oldest newest most voted

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')

more

Official site

GitHub

Wiki

Documentation