点和点集(轮廓)关系

#include
#include
#include
#include

using namespace cv;
using namespace std;

/*
	double pointPolygonTest(InputArray contour,Point2f pt,bool measureDist);
	contour:	输入点集
	pt     :	坐标点
	measureDist:是否计算坐标点到轮廓的距离

	当measureDist 为false时,函数返回+1,0,-1,分别对应内,上,外
	当measureDist 为true时,函数返回距离,正负代表位置。
*/
int main()
{
	vector contour;
	contour.push_back(Point(0, 0));
	contour.push_back(Point(50, 30));
	contour.push_back(Point(100, 100));
	contour.push_back(Point(100, 0));

	Mat img = Mat::zeros(Size(130, 130), CV_8UC1);
	int num = contour.size();//点的数量
	for (int i = 0; i <  num - 1; i++)
	{
		//用直线依次连接轮廓中相邻的点
		line(img, contour[i], contour[i + 1], Scalar(255), 2);
	}
	line(img, contour[num - 1], contour[0], Scalar(255), 2);
	//标注点的位置
	circle(img, Point2f(80, 40), 3, Scalar(255), CV_FILLED);
	circle(img, Point2f(50, 0), 3, Scalar(255), CV_FILLED);
	circle(img, Point2f(40, 80), 3, Scalar(255), CV_FILLED);
	//点在内
	double dist1 = pointPolygonTest(contour, Point2f(80, 40), true);
	cout << "dist1:" << dist1 << endl;
	//点在上
	double dist2 = pointPolygonTest(contour, Point2f(50, 0), true);
	cout << "dist2:" << dist2 << endl;
	//点在外
	double dist3 = pointPolygonTest(contour, Point2f(40, 80), true);
	cout << "dist3:" << dist3 << endl;
	//显示点集围成的轮廓和三个标注点
	imshow("轮廓", img);
	waitKey(0);
	return 0;

}
import cv2
import numpy as np

contour=np.array([[0,0],[50,30],[100,100],[100,0]],np.float32)

dist1=cv2.pointPolygonTest(contour,(80,40),False);
dist2=cv2.pointPolygonTest(contour,(50,0),False);
dist3=cv2.pointPolygonTest(contour,(40,80),False);

print(dist1,dist2,dist3)


dist1=cv2.pointPolygonTest(contour,(80,40),True);
dist2=cv2.pointPolygonTest(contour,(50,0),True);
dist3=cv2.pointPolygonTest(contour,(40,80),True);

print(dist1,dist2,dist3)

 

你可能感兴趣的:(图像处理,OpenCV,C++)