【OpenCV图像处理】1.32 点多边形测试

1. 相关理论

概念介绍 - 点多边形测试

  • 测试一个点是否在给定的多边形内部,边缘或者外部

API介绍 cv::pointPolygonTest

pointPolygonTest(
	InputArray  contour,// 输入的轮廓
	Point2f  pt, // 测试点
	bool  measureDist // 是否返回距离值,如果是false,1表示在内面,0表示在边界上,-1表示在外部,true返回实际距离
)

返回数据是double类型

2. 代码 & 效果展示

  • 演示代码 - 步骤

    • 构建一张400x400大小的图片, Mat::Zero(400, 400, CV_8UC1)
    • 画上一个六边形的闭合区域line
    • 发现轮廓
    • 对图像中所有像素点做点 多边形测试,得到距离,归一化后显示。
  • 代码:

#include 
#include 
#include 

using namespace std;
using namespace cv;

#ifndef P32
#define P32 32
#endif

int main() {
    std::string path = "../circle.JPG";
    cv::Mat img = cv::imread(path, 5);

    string str_input = "input image";
    string str_output = "output image";

    if (img.empty()) {
        std::cout << "open file failed" << std::endl;
        return -1;
    }

    namedWindow(str_input, WINDOW_AUTOSIZE);
    namedWindow(str_output, WINDOW_AUTOSIZE);
    imshow(str_input, img);
    
#if P32 //点多边形测试
    const int r = 100;
    Mat src = Mat::zeros(r *4, r*4, CV_8UC1);

    vector<Point2f> vert(6);
    vert[0] = Point(3*r/2, static_cast<int>(1.34*r));
    vert[1] = Point(1*r, 2*r);
    vert[2] = Point(3*r/2, static_cast<int>(2.866*r));
    vert[3] = Point(5*r/2, static_cast<int>(2.866*r));
    vert[4] = Point(3*r, 2*r);
    vert[5] = Point(5*r/2, static_cast<int>(1.34*r));

    for (int i = 0; i < 6; ++i) {
        line(src, vert[i], vert[(i+1)%6], Scalar(255), 3, 8, 0);
    }

    vector<vector<Point>> contours;
    vector<Vec4i> hierachy;

    Mat csrc;
    src.copyTo(csrc);
    findContours(csrc, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0));
    Mat raw_dist = Mat::zeros(csrc.size(), CV_32FC1);

    for( int row = 0; row < raw_dist.rows; row++)
    {
        for( int col = 0; col < raw_dist.cols; col++)
        {
            double dist = pointPolygonTest(contours[0], \
                    Point2f(static_cast<float >(col), \
                    static_cast<float>(row)), true);
            raw_dist.at<float>(row, col) = static_cast<float >(dist);
        }
    }

    double minValue, maxValue;
    minMaxLoc(raw_dist, &minValue, &maxValue, 0, 0, Mat());
    Mat drawImg = Mat::zeros(src.size(), CV_8UC3);
    for (int row = 0; row < drawImg.rows; row++) {
        for (int col = 0; col < drawImg.cols; col++) {
            float dist = raw_dist.at<float>(row, col);
            if (dist > 0) {
                drawImg.at<Vec3b>(row, col)[0] = (uchar)(abs(1.0 - (dist / maxValue)) * 255);
            }
            else if (dist < 0) {
                drawImg.at<Vec3b>(row, col)[2] = (uchar)(abs(1.0 - (dist / minValue)) * 255);
            } else {
                drawImg.at<Vec3b>(row, col)[0] = (uchar)(abs(255 - dist));
                drawImg.at<Vec3b>(row, col)[1] = (uchar)(abs(255 - dist));
                drawImg.at<Vec3b>(row, col)[2] = (uchar)(abs(255 - dist));
            }
        }
    }

    const char* output_win = "point polygon test demo";
    char input_win[] = "input image";
    namedWindow(input_win, WINDOW_AUTOSIZE);
    namedWindow(output_win, WINDOW_AUTOSIZE);

    imshow(input_win, src);
    imshow(output_win, drawImg);
#endif

    cv::waitKey(0);
    cv::destroyAllWindows();
    return 0;
}

效果展示:
【OpenCV图像处理】1.32 点多边形测试_第1张图片

你可能感兴趣的:(OpenCV图像处理基础)