Ask Your Question

How to create mask from an image in opencv?

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

sakshi gravatar image

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

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

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

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;

    if( event == EVENT_LBUTTONUP )
        mousedown = false;
        if(pts.size() > 2 )
            Mat mask(img.size(),CV_8UC1);
            mask = 0;
            Mat masked(img.size(),CV_8UC3,Scalar(255,255,255));
            imshow( "masked", masked );

        if(pts.size() > 2 )


        imshow( "Create Mask", img );

int main( int argc, const char** argv )
    src = imread(argv[1]);
        return -1;

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

    return 0;
edit flag offensive delete link more


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

sakshi gravatar imagesakshi ( 2016-06-20 07:46:45 -0500 )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 -0500 )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: (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 -0500 )edit

when you finish selection click on right button of mouse

sturkmen gravatar imagesturkmen ( 2017-02-09 06:48:29 -0500 )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: (

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

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

Question Tools

1 follower


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

Seen: 31,947 times

Last updated: Jun 19 '16