如何对多边形使用polygonPointTest函数?

问题描述

我正在尝试为多边形运行多边形点测试功能。我的代码如下所示,它仅适用于coutour吗?

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)

我遇到错误

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-454-bfdc7e835e9b> in <module>
----> 1 cv2.pointpolygonTest(pts,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'

解决方法

我正在使用不兼容类型的np array,将其转换为np.int32之后,我可以获得所需的输出。

import ast
import cv2
import numpy as np
point1 = (25,40)
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,np.int32)
cv2.pointPolygonTest(pts,point1,False)