Ask Your Question
0

How to create mask from an image in opencv?

asked 2016-06-19 09:27:31 -0600

sakshi gravatar image

updated 2016-06-19 14:45:49 -0600

the mask which i want to create can be of some specific region of the image , For example

image description

this is the image from which i want create mask of the face of cat( i.e including eyes , nose , mouth ) or like if i want to create the mask only for the ears i want to create mask for :

image description

the mask :

image description

how to select the region and create mask??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-06-19 19:51:34 -0600

there is an offical sample create_mask.cpp for doing that.

or you can try the following code that implemented before.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

Mat src,cloneimg;
bool mousedown;
vector<vector<Point> > contours;
vector<Point> pts;

void onMouse( int event, int x, int y, int flags, void* userdata )
{
    Mat img = *((Mat *)userdata);

    if( event == EVENT_LBUTTONDOWN )
    {
        mousedown = true;
        contours.clear();
        pts.clear();
    }

    if( event == EVENT_LBUTTONUP )
    {
        mousedown = false;
        if(pts.size() > 2 )
        {
            Mat mask(img.size(),CV_8UC1);
            mask = 0;
            contours.push_back(pts);
            drawContours(mask,contours,0,Scalar(255),-1);
            Mat masked(img.size(),CV_8UC3,Scalar(255,255,255));
            src.copyTo(masked,mask);
            src.copyTo(cloneimg);
            imshow( "masked", masked );
        }
    }

    if(mousedown)
    {
        if(pts.size() > 2 )
            line(img,Point(x,y),pts[pts.size()-1],Scalar(0,255,0));

        pts.push_back(Point(x,y));

        imshow( "Create Mask", img );
    }
}

int main( int argc, const char** argv )
{
    src = imread(argv[1]);
    if(src.empty())
    {
        return -1;
    }

    namedWindow("Create Mask", WINDOW_AUTOSIZE);
    cloneimg = src.clone();
    setMouseCallback( "Create Mask", onMouse, &cloneimg );
    imshow( "Create Mask", src );

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

is there something in python also? or could u explain me the approach?

sakshi gravatar imagesakshi ( 2016-06-20 07:46:45 -0600 )edit

take a look at this tutorial and this tutorial for basic usage of mouse events. what i did with the code above is recording all the points where mouse moved like a contour points and later with drawContours() function created the mask.

sturkmen gravatar imagesturkmen ( 2016-06-20 20:37:16 -0600 )edit

Is there any better or more detailed explanation about the usage of the offical sample create_mask.cpp ?? I am new in programming and I don't know how to use it!

  1. I load a program in visual studio
  2. Replace code: string input_image = parser.get<string>("@input"); with code string input_image = "myImage.bmp";
  3. then myImage is shown together with red cursor and I am able to select polygon on the image:

http://shrani.si/f/3v/kK/2CLPphAG/myimage.png (link to image)

How can I now get mask of image? What variable is mask stored in? I want to use this mask in further processing...

jok23 gravatar imagejok23 ( 2017-02-08 23:58:37 -0600 )edit

when you finish selection click on right button of mouse

sturkmen gravatar imagesturkmen ( 2017-02-09 06:48:29 -0600 )edit

thank you very much!! Do you know if there is any way to combine this mouse events together with imshow to get interactive pixel values?? For example something like matlabs here:

https://www.mathworks.com/help/images/ref/impixelinfo.html (https://www.mathworks.com/help/images...)

so that pixel values are displayed when you are moveing mouse over image! (Mat format)

jok23 gravatar imagejok23 ( 2017-02-09 08:13:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-19 09:27:31 -0600

Seen: 37,713 times

Last updated: Jun 19 '16