Domanda

I'm trying to obtain a ROI from an image using VC++ and OpenCV. I managed to display an image, get the coordinates of a point when I click on it, store these coordinates in a vector and draw lines between these points on my image. Here is my code:

//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);

    }
}

What I would like to obtain is a vector that contains the coordinates of all the points of the contour and ultimately a bianary matrix the size of my image that contains 0 if the pixel is not in the contour, else 1. Thanks for your help.

È stato utile?

Soluzione

Make single element vector< vector< Point> > and then use drawContours with CV_FILLED. Then you will have binary matrix you wanted.

I currently don't have IDE but code will be like following

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top