• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * create_mask.cpp
3 *
4 * Author:
5 * Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
6 *
7 * This tutorial demonstrates how to make mask image (black and white).
8 * The program takes as input a source image and ouputs its corresponding
9 * mask image.
10 */
11 
12 #include "opencv2/imgproc.hpp"
13 #include "opencv2/imgcodecs.hpp"
14 #include "opencv2/highgui.hpp"
15 #include "opencv2/core.hpp"
16 #include <iostream>
17 #include <stdlib.h>
18 
19 using namespace std;
20 using namespace cv;
21 
22 Mat img0, img1, res1, final;
23 
24 Point point;
25 int drag = 0;
26 
27 int numpts = 100;
28 Point* pts = new Point[100];
29 
30 int var = 0;
31 int flag = 0;
32 int flag1 = 0;
33 
34 int minx,miny,maxx,maxy,lenx,leny;
35 
36 int channel;
37 
38 void mouseHandler(int, int, int, int, void*);
39 
mouseHandler(int event,int x,int y,int,void *)40 void mouseHandler(int event, int x, int y, int, void*)
41 {
42 
43     if (event == EVENT_LBUTTONDOWN && !drag)
44     {
45         if(flag1 == 0)
46         {
47             if(var==0)
48                 img1 = img0.clone();
49             point = Point(x, y);
50             circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);
51             pts[var] = point;
52             var++;
53             drag  = 1;
54             if(var>1)
55                 line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
56 
57             imshow("Source", img1);
58         }
59     }
60 
61     if (event == EVENT_LBUTTONUP && drag)
62     {
63         imshow("Source", img1);
64 
65         drag = 0;
66     }
67     if (event == EVENT_RBUTTONDOWN)
68     {
69         flag1 = 1;
70         img1 = img0.clone();
71         for(int i = var; i < numpts ; i++)
72             pts[i] = point;
73 
74         if(var!=0)
75         {
76             const Point* pts3[1] = {&pts[0]};
77             polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
78         }
79 
80         for(int i=0;i<var;i++)
81         {
82             minx = min(minx,pts[i].x);
83             maxx = max(maxx,pts[i].x);
84             miny = min(miny,pts[i].y);
85             maxy = max(maxy,pts[i].y);
86         }
87         lenx = maxx - minx;
88         leny = maxy - miny;
89 
90         imshow("Source", img1);
91     }
92 
93     if (event == EVENT_RBUTTONUP)
94     {
95         flag = var;
96 
97         final = Mat::zeros(img0.size(),CV_8UC3);
98         res1 = Mat::zeros(img0.size(),CV_8UC1);
99         const Point* pts4[1] = {&pts[0]};
100 
101         fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
102         bitwise_and(img0, img0, final,res1);
103         imshow("mask",res1);
104         imwrite("mask.png",res1);
105 
106         imshow("Source", img1);
107 
108     }
109     if (event == EVENT_MBUTTONDOWN)
110     {
111         for(int i = 0; i < numpts ; i++)
112         {
113             pts[i].x=0;
114             pts[i].y=0;
115         }
116         var = 0;
117         flag1 = 0;
118         minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
119         imshow("Source", img0);
120         drag = 0;
121     }
122 }
123 
main(int argc,char ** argv)124 int main(int argc, char **argv)
125 {
126 
127     if(argc != 2)
128     {
129         cout << "usage: " << argv[0] << " <input_image>" << endl;
130         exit(1);
131     }
132 
133     Mat src = imread(argv[1]);
134 
135     minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
136 
137     img0 = src;
138 
139     channel = img0.channels();
140 
141     res1 = Mat::zeros(img0.size(),CV_8UC1);
142     final = Mat::zeros(img0.size(),CV_8UC3);
143     //////////// source image ///////////////////
144 
145     namedWindow("Source", 1);
146     setMouseCallback("Source", mouseHandler, NULL);
147     imshow("Source", img0);
148     waitKey(0);
149 
150     img0.release();
151     img1.release();
152 }
153