作者:云思逸 | 来源:互联网 | 2023-10-12 20:28
pointPolygonTest(InputArraycontour,输入的轮廓Point2fpt,测试点boolmeasureDist是否返回距离值,如果是f
pointPolygonTest(
InputArray contour,// 输入的轮廓
Point2f pt, // 测试点
bool measureDist // 是否返回距离值,如果是false,1表示在内面,0表示在边界上,-1表示在外部,true返回实际距离 )
返回数据是double类型
#include
#include
#include using namespace std;
using namespace cv;
int main(int argc, char** argv) {const int r &#61; 100;Mat src &#61; Mat::zeros(r * 4, r * 4, CV_8UC1);vector vert(6);vert[0] &#61; Point(3 * r / 2, static_cast(1.34*r)); vert[1] &#61; Point(1 * r, 2 * r);vert[2] &#61; Point(3 * r / 2, static_cast(2.866*r)); vert[3] &#61; Point(5 * r / 2, static_cast(2.866*r));vert[4] &#61; Point(3 * r, 2 * r); vert[5] &#61; Point(5 * r / 2, static_cast(1.34*r));for (int i &#61; 0; i <6; i&#43;&#43;) {line(src, vert[i], vert[(i &#43; 1) % 6], Scalar(255), 3, 8, 0);}vector> contours;vector hierachy;Mat csrc;src.copyTo(csrc);findContours(csrc, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));Mat raw_dist &#61; Mat::zeros(csrc.size(), CV_32FC1);for (int row &#61; 0; row (col), static_cast(row)), true);//注意这里是col和row我也不知道Whyraw_dist.at(row, col) &#61; static_cast(dist);}}double minValue, maxValue;minMaxLoc(raw_dist, &minValue, &maxValue, 0, 0, Mat());Mat drawImg &#61; Mat::zeros(src.size(), CV_8UC3);for (int row &#61; 0; row (row, col);if (dist > 0) {drawImg.at(row, col)[0] &#61; (uchar)(abs(1.0 - (dist / maxValue)) * 255);}else if (dist <0) {drawImg.at(row, col)[2] &#61; (uchar)(abs(1.0 - (dist / minValue)) * 255);} else {drawImg.at(row, col)[0] &#61; (uchar)(abs(255 - dist));drawImg.at(row, col)[1] &#61; (uchar)(abs(255 - dist));drawImg.at(row, col)[2] &#61; (uchar)(abs(255 - dist));}}}const char* output_win &#61; "point polygon test demo";char input_win[] &#61; "input image";namedWindow(input_win, CV_WINDOW_AUTOSIZE);namedWindow(output_win, CV_WINDOW_AUTOSIZE);imshow(input_win, src);imshow(output_win, drawImg);waitKey(0);return 0;
}