判断opencv自定义多边形内是否含有某个点

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

判断opencv自定义多边形内是否含有某个点
#ifndef BOXEXTRACTOR_H
#define BOXEXTRACTOR_H
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
using namespace std;

class MouseHelper4OpenCV {
public:
    MouseHelper4OpenCV();
    bool checkPointInPolygon(cv::Point point);
    vector<vector<cv::Point>> contours;
    vector<cv::Vec4i> hierarchy;
    cv::Mat MouseDraw(const std::string& windowName, cv::Mat img);
    struct handlerT{
        bool isDrawing;
        cv::Point curPoint;
        cv::Mat image;
        std::vector<cv::Point2f> vert;
        handlerT(): isDrawing(false) {};
    }params;
private:
    static void mouseHandler(int event, int x, int y, int flags, void *param);
    void opencv_mouse_callback( int event, int x, int y, int , void *param );
};
#endif // BOXEXTRACTOR_H
#include "mousedraw.h"

MouseHelper4OpenCV::MouseHelper4OpenCV()
{
}

bool MouseHelper4OpenCV::checkPointInPolygon(cv::Point point)
{
    for(size_t i = 0; i<contours.size(); i++)
    {
        if (cv::pointPolygonTest(contours[i], point, false) > 0)return true;
    }
    return false;
}

void MouseHelper4OpenCV::mouseHandler(int event, int x, int y, int flags, void *param){
    MouseHelper4OpenCV *self =static_cast<MouseHelper4OpenCV*>(param);
    self->opencv_mouse_callback(event,x,y,flags,param);
}

void MouseHelper4OpenCV::opencv_mouse_callback( int event, int x, int y, int , void *param ){
    handlerT * data = (handlerT*)param;
    switch( event ){
    case cv::EVENT_MOUSEMOVE:
        break;
    case cv::EVENT_LBUTTONDOWN:
        data->isDrawing = true;
        break;
    case cv::EVENT_LBUTTONUP:
        data->isDrawing = false;
        data->curPoint.x = x;
        data->curPoint.y = y;
        data->vert.push_back (data->curPoint);
        break;
    }
}

cv::Mat MouseHelper4OpenCV::MouseDraw(const std::string& windowName, cv::Mat img){
    int key=0;
    cv::imshow(windowName,img);
    printf("DRAW AN DIGIT and then press SPACE/BACKSPACE/ENTER button!\n");
    params.image=img.clone();
    cv::setMouseCallback( windowName, mouseHandler, (void *)&params );
    while(!(key==32 || key==27 || key==13)){
        for( int j = 0; j < params.vert.size (); j++ )
        {
            cv::circle ( params.image, params.vert[j],2, cv::Scalar( 255 ), 3, 8 );
        }
        cv::imshow(windowName,params.image);
        key=cv::waitKey(1);
    }
    cv::Mat src = cv::Mat::zeros( cv::Size(params.image.cols, params.image.rows ),CV_8UC1 );
    for( int j = 0; j < params.vert.size (); j++ )
    {
        cv::line( src, params.vert[j],  params.vert[(j+1)%params.vert.size ()], cv::Scalar( 255 ), 4, 8 );
        cv::line( params.image, params.vert[j],  params.vert[(j+1)%params.vert.size ()], cv::Scalar( 255 ), 4, 8 );
    }
    cv::findContours(src,contours,hierarchy,cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
    return params.image;
}
/**
 * @function pointPolygonTest_demo.cpp
 * @brief Demo code to use the pointPolygonTest function...fairly easy
 * @author OpenCV team
 */

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "mousedraw.h"

using namespace cv;
using namespace std;

/**
 * @function main
 */
int main( void )
{

    MouseHelper4OpenCV helper;

    helper.MouseDraw ("test",imread("d://0000.jpg")); 
    if(helper.checkPointInPolygon (cv::Point(100,100))){
        printf("!!!!! in !!!!!\n");
    }else{
        printf("!!!!! not in !!!!!\n");
    }
    printf("Done\n");

    return(0);
}