2

VC++ と OpenCV を使用して画像から ROI を取得しようとしています。画像を表示し、クリックすると点の座標を取得し、これらの座標をベクトルに保存し、画像上のこれらの点の間に線を引くことができました。これが私のコードです:

//Includes
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

static int app;
static vector<Point2f> cont(6);
static Mat img = imread("C:/img.jpg",0);

void on_mouse(int, int, int, int, void* );

int main() 

{
app = 0;
namedWindow("myWindow", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("myWindow", on_mouse, 0);
imshow("myWindow", img);
waitKey(0);
}

void on_mouse(int evt, int x, int y, int flags, void* param)
{

if(evt == CV_EVENT_LBUTTONDOWN)
{
    Point pt(x,y);
    if(app<6)
        {
            cont[app]=pt;
            app++;
        }


    cout<<"Coordonnees du point pt : "<<x<<","<<y<<endl;
    for (int i=0; i<6;i++)
    {cout<<cont[i]<<endl;}
}
 if(evt == CV_EVENT_RBUTTONDOWN)
{
    for (int j=0;j<5;j++)
        {
            line(img,cont[(j)],cont[(j+1)],CV_RGB(255,0,0),2);
        }
    line(img,cont[(5)],cont[(0)],CV_RGB(255,0,0),2);
    imshow("myWindow", img);

    }
}

私が取得したいのは、輪郭のすべての点の座標を含むベクトルと、最終的にピクセルが輪郭にない場合は 0、それ以外の場合は 1 を含む画像のサイズの 2 値行列です。助けてくれてありがとう。

4

1 に答える 1

1

単一の要素vector< vector< Point> >を作成してから、CV_FILLED で drawContours を使用します。次に、必要なバイナリマトリックスが得られます。

私は現在IDEを持っていませんが、コードは次のようになります

vector< vector< Point> > contours;
contours.push_back(cont);//your cont
Mat output(img.rows,img.cols,CV_8UC1);//your img
drawContours(output, contours, 0, Scalar(1), CV_FILLED);//now you have binary image
于 2012-10-12T01:59:27.330 に答える