如何从OPENCV的多边形绘制中提取图像(基本上是MAT)?

如何从OPENCV的多边形绘制中提取图像(基本上是MAT)?,opencv,image-processing,Opencv,Image Processing,我正在编写一个简单的opencv程序来提取图像,并从自己绘制的策略中获取图像矩阵。下面的代码给出了一个使用给定的几个点绘制多边形的示例,当我以红色绘制完多边形时,我想使用FindContentours从图片中提取唯一的多边形,并从该轮廓获取矩阵 #include "stdafx.h" #include "opencv2/highgui/highgui.hpp" #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.h

我正在编写一个简单的opencv程序来提取图像,并从自己绘制的策略中获取图像矩阵。下面的代码给出了一个使用给定的几个点绘制多边形的示例,当我以红色绘制完多边形时,我想使用FindContentours从图片中提取唯一的多边形,并从该轮廓获取矩阵

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
Mat grey_img;
Mat img_resized;
bool start = 0;
cv::Point lastPoint =  Point(-1,-1);
vector<Point> parkspoint;
void mouseMode2(int event, int x, int y, void* param);
void mouseHandler(int event, int x, int y, int flags, void* param);

void mouseHandler(int event, int x, int y, int flags, void* param){
    mouseMode2(event,x,y,param);
}

void mouseMode2(int event, int x, int y, void* param){
    //Boolean select_flag = CV_EVENT_FLAG_SHIFTKEY; 
    if (event == CV_EVENT_LBUTTONDOWN && start)
    {
        cout<<"select one point  "<<x<<"   "<< y<<endl;
        cv::Point point = cv::Point(x, y);

        cout<<"last point   x:"<<lastPoint.x <<"    y:"<<lastPoint.y<<endl; 
        //  add point
        if(lastPoint.x!=-1 && lastPoint.y!=-1){
           cv::line(img_resized, lastPoint, point, CV_RGB(255, 0, 0), 1, 1, 0);
           cv::imshow("parking", img_resized);
        }
        cout<<"add point to array"<<endl;
        lastPoint =  Point(x,y);        
        parkspoint.push_back(point);
    }

    if (event == CV_EVENT_MOUSEMOVE && start){
        cv::Point point = cv::Point(x, y);
        if(lastPoint.x!=-1 && lastPoint.y!=-1)
        {
            cv::Mat img1 = img_resized.clone();
            cv::line(img1, lastPoint, point, CV_RGB(255, 0, 0), 1, 1, 0);
            cv::imshow("parking", img1);
        }
    }

    if (event == CV_EVENT_RBUTTONUP )
    {
        cout<<"end selecting"<<endl;
        start = 0;
        if(lastPoint.x!=-1 && lastPoint.y!=-1){
            cv::line(img_resized, lastPoint, parkspoint[0], CV_RGB(255, 0, 0), 1, 1, 0);
            cv::imshow("parking", img_resized);
        }
        for(int i=0;i<parkspoint.size();i++){
            cout<<"show points "<<i<<"     "<< parkspoint[i].x<<":"<< parkspoint[i].y <<endl;
        }

        std::vector<std::vector<cv::Point> > contours;
        cv::imshow("parking", img_resized);

        cv::findContours(img_resized,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
        cout<<"find  how many coutours  : "<< contours.size()<< endl;
        for(int i = 0;i <contours.size();i++){
           cv::drawContours(grey_img,contours,i,cv::Scalar(255,0,0),1);
        }
        cv::imshow("parking2", grey_img);
    }

    if (event == CV_EVENT_RBUTTONDOWN )
    {
        cout<<"start selecting"<<endl;
        start = 1;
    }
}

int main(int argc, char* argv[])
{
    Mat img_raw = imread("D:/car parking/bb.jpg", 1); // load as color image

    resize(img_raw, img_resized, Size(64,128) );
    grey_img = img_resized.clone();

    cout << "raw img dimensions: " << img_raw.cols << " width x " << img_raw.rows << "height" << endl;
    cout << "img dimensions: " << img_resized.cols << " width x " << img_resized.rows << "height" << endl; 

    cv::cvtColor(img_resized,img_resized,CV_BGR2GRAY);
   namedWindow("parking",CV_WINDOW_NORMAL);
   imshow("parking",img_resized);
   namedWindow("parking2",CV_WINDOW_NORMAL);
   imshow("parking2",grey_img);
   cv::setMouseCallback("parking",mouseHandler,0);
   waitKey(0);
   return 0;
}
#包括“stdafx.h”
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/core/core.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
使用名称空间cv;
使用名称空间std;
Mat grey_img;
调整了材料的尺寸;
bool start=0;
cv::Point lastPoint=点(-1,-1);
矢量parkspoint;
void mouseMode2(int事件、int x、int y、void*param);
void鼠标句柄(int事件、int x、int y、int标志、void*参数);
void鼠标句柄(int事件、int x、int y、int标志、void*参数){
mouseMode2(事件,x,y,参数);
}
void mouseMode2(int事件、int x、int y、void*param){
//布尔选择标志=CV事件标志移位键;
if(event==CV_event_LBUTTONDOWN&start)
{

为什么不只是得到你已经画的点的形状,然后裁剪呢?在这个例子中,我画了一个矩形,但实际上,它可以是矩形以外的任何形状。我可能误解了你在提取中的意思,但是如果你想裁剪零件,这些只能是矩形。我只想裁剪零件,通过链接,零件可以是任何形状这些点。可以有至少3点,或最多8点