Skip to content

Commit ae800a5

Browse files
authored
Create Exercises_13_9.cpp
1 parent cc93cde commit ae800a5

File tree

1 file changed

+356
-0
lines changed

1 file changed

+356
-0
lines changed

Exercises_13_9.cpp

Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
2+
//Exercises_13_9.cpp Exercise 9 at end of Chapter 13
3+
/************************************************************************/
4+
/* 9. Write an image-processing function to interactively remove people from an
5+
image. Use cv::grabCut() to segment the person, and then use cv::inpaint()
6+
to fill in the resulting hole (recall that we learned about cv::inpaint() in the
7+
previous chapter). */
8+
/************************************************************************/
9+
10+
11+
#include "opencv2/imgcodecs.hpp"
12+
#include "opencv2/highgui.hpp"
13+
#include "opencv2/imgproc.hpp"
14+
#include "opencv2/photo.hpp"
15+
16+
#include <iostream>
17+
18+
using namespace std;
19+
using namespace cv;
20+
21+
static void help()
22+
{
23+
cout << "\nThis program demonstrates GrabCut segmentation -- select an object in a region\n"
24+
"and then grabcut will attempt to segment it out.\n"
25+
"Call:\n"
26+
"./grabcut <image_name>\n"
27+
"\nSelect a rectangular area around the object you want to segment\n" <<
28+
"\nHot keys: \n"
29+
"\tESC - quit the program\n"
30+
"\tr - restore the original image\n"
31+
"\tn - next iteration\n"
32+
"\n"
33+
"\tleft mouse button - set rectangle\n"
34+
"\n"
35+
"\tCTRL+left mouse button - set GC_BGD pixels\n"
36+
"\tSHIFT+left mouse button - set GC_FGD pixels\n"
37+
"\n"
38+
"\tCTRL+right mouse button - set GC_PR_BGD pixels\n"
39+
"\tSHIFT+right mouse button - set GC_PR_FGD pixels\n" << endl;
40+
}
41+
42+
const Scalar RED = Scalar(0,0,255);
43+
const Scalar PINK = Scalar(230,130,255);
44+
const Scalar BLUE = Scalar(255,0,0);
45+
const Scalar LIGHTBLUE = Scalar(255,255,160);
46+
const Scalar GREEN = Scalar(0,255,0);
47+
48+
const int BGD_KEY = EVENT_FLAG_CTRLKEY;
49+
const int FGD_KEY = EVENT_FLAG_SHIFTKEY;
50+
51+
static void getBinMask( const Mat& comMask, Mat& binMask )
52+
{
53+
if( comMask.empty() || comMask.type()!=CV_8UC1 )
54+
CV_Error( Error::StsBadArg, "comMask is empty or has incorrect type (not CV_8UC1)" );
55+
if( binMask.empty() || binMask.rows!=comMask.rows || binMask.cols!=comMask.cols )
56+
binMask.create( comMask.size(), CV_8UC1 );
57+
binMask = comMask & 1;
58+
}
59+
60+
class GCApplication
61+
{
62+
public:
63+
enum{ NOT_SET = 0, IN_PROCESS = 1, SET = 2 };
64+
static const int radius = 2;
65+
static const int thickness = -1;
66+
67+
void reset();
68+
void setImageAndWinName( const Mat& _image, const string& _winName );
69+
void showImage() const;
70+
void mouseClick( int event, int x, int y, int flags, void* param );
71+
int nextIter();
72+
int getIterCount() const { return iterCount; }
73+
private:
74+
void setRectInMask();
75+
void setLblsInMask( int flags, Point p, bool isPr );
76+
77+
const string* winName;
78+
const Mat* image;
79+
Mat mask;
80+
Mat bgdModel, fgdModel;
81+
82+
uchar rectState, lblsState, prLblsState;
83+
bool isInitialized;
84+
85+
Rect rect;
86+
vector<Point> fgdPxls, bgdPxls, prFgdPxls, prBgdPxls;
87+
int iterCount;
88+
};
89+
90+
void GCApplication::reset()
91+
{
92+
if( !mask.empty() )
93+
mask.setTo(Scalar::all(GC_BGD));
94+
bgdPxls.clear(); fgdPxls.clear();
95+
prBgdPxls.clear(); prFgdPxls.clear();
96+
97+
isInitialized = false;
98+
rectState = NOT_SET;
99+
lblsState = NOT_SET;
100+
prLblsState = NOT_SET;
101+
iterCount = 0;
102+
}
103+
104+
void GCApplication::setImageAndWinName( const Mat& _image, const string& _winName )
105+
{
106+
if( _image.empty() || _winName.empty() )
107+
return;
108+
image = &_image;
109+
winName = &_winName;
110+
mask.create( image->size(), CV_8UC1);
111+
reset();
112+
}
113+
114+
void GCApplication::showImage() const
115+
{
116+
if( image->empty() || winName->empty() )
117+
return;
118+
119+
Mat res;
120+
Mat binMask;
121+
if( !isInitialized )
122+
image->copyTo( res );
123+
else
124+
{
125+
getBinMask( mask, binMask );
126+
image->copyTo( res, binMask );
127+
// mask and source code
128+
Mat src = *image;
129+
Mat mask = binMask;
130+
//inpaint
131+
Mat inpainted;
132+
inpaint(src, mask, inpainted, 3, CV_INPAINT_TELEA);
133+
imshow("inpainted result",inpainted);
134+
}
135+
136+
vector<Point>::const_iterator it;
137+
for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
138+
circle( res, *it, radius, BLUE, thickness );
139+
for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
140+
circle( res, *it, radius, RED, thickness );
141+
for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
142+
circle( res, *it, radius, LIGHTBLUE, thickness );
143+
for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
144+
circle( res, *it, radius, PINK, thickness );
145+
146+
if( rectState == IN_PROCESS || rectState == SET )
147+
rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
148+
149+
imshow( *winName, res );
150+
}
151+
152+
void GCApplication::setRectInMask()
153+
{
154+
CV_Assert( !mask.empty() );
155+
mask.setTo( GC_BGD );
156+
rect.x = max(0, rect.x);
157+
rect.y = max(0, rect.y);
158+
rect.width = min(rect.width, image->cols-rect.x);
159+
rect.height = min(rect.height, image->rows-rect.y);
160+
(mask(rect)).setTo( Scalar(GC_PR_FGD) );
161+
}
162+
163+
void GCApplication::setLblsInMask( int flags, Point p, bool isPr )
164+
{
165+
vector<Point> *bpxls, *fpxls;
166+
uchar bvalue, fvalue;
167+
if( !isPr )
168+
{
169+
bpxls = &bgdPxls;
170+
fpxls = &fgdPxls;
171+
bvalue = GC_BGD;
172+
fvalue = GC_FGD;
173+
}
174+
else
175+
{
176+
bpxls = &prBgdPxls;
177+
fpxls = &prFgdPxls;
178+
bvalue = GC_PR_BGD;
179+
fvalue = GC_PR_FGD;
180+
}
181+
if( flags & BGD_KEY )
182+
{
183+
bpxls->push_back(p);
184+
circle( mask, p, radius, bvalue, thickness );
185+
}
186+
if( flags & FGD_KEY )
187+
{
188+
fpxls->push_back(p);
189+
circle( mask, p, radius, fvalue, thickness );
190+
}
191+
}
192+
193+
void GCApplication::mouseClick( int event, int x, int y, int flags, void* )
194+
{
195+
// TODO add bad args check
196+
switch( event )
197+
{
198+
case EVENT_LBUTTONDOWN: // set rect or GC_BGD(GC_FGD) labels
199+
{
200+
bool isb = (flags & BGD_KEY) != 0,
201+
isf = (flags & FGD_KEY) != 0;
202+
if( rectState == NOT_SET && !isb && !isf )
203+
{
204+
rectState = IN_PROCESS;
205+
rect = Rect( x, y, 1, 1 );
206+
}
207+
if ( (isb || isf) && rectState == SET )
208+
lblsState = IN_PROCESS;
209+
}
210+
break;
211+
case EVENT_RBUTTONDOWN: // set GC_PR_BGD(GC_PR_FGD) labels
212+
{
213+
bool isb = (flags & BGD_KEY) != 0,
214+
isf = (flags & FGD_KEY) != 0;
215+
if ( (isb || isf) && rectState == SET )
216+
prLblsState = IN_PROCESS;
217+
}
218+
break;
219+
case EVENT_LBUTTONUP:
220+
if( rectState == IN_PROCESS )
221+
{
222+
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
223+
rectState = SET;
224+
setRectInMask();
225+
CV_Assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
226+
showImage();
227+
}
228+
if( lblsState == IN_PROCESS )
229+
{
230+
setLblsInMask(flags, Point(x,y), false);
231+
lblsState = SET;
232+
showImage();
233+
}
234+
break;
235+
case EVENT_RBUTTONUP:
236+
if( prLblsState == IN_PROCESS )
237+
{
238+
setLblsInMask(flags, Point(x,y), true);
239+
prLblsState = SET;
240+
showImage();
241+
}
242+
break;
243+
case EVENT_MOUSEMOVE:
244+
if( rectState == IN_PROCESS )
245+
{
246+
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
247+
CV_Assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
248+
showImage();
249+
}
250+
else if( lblsState == IN_PROCESS )
251+
{
252+
setLblsInMask(flags, Point(x,y), false);
253+
showImage();
254+
}
255+
else if( prLblsState == IN_PROCESS )
256+
{
257+
setLblsInMask(flags, Point(x,y), true);
258+
showImage();
259+
}
260+
break;
261+
}
262+
}
263+
264+
int GCApplication::nextIter()
265+
{
266+
if( isInitialized )
267+
grabCut( *image, mask, rect, bgdModel, fgdModel, 1 );
268+
else
269+
{
270+
if( rectState != SET )
271+
return iterCount;
272+
273+
if( lblsState == SET || prLblsState == SET )
274+
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_MASK );
275+
else
276+
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT );
277+
278+
isInitialized = true;
279+
}
280+
iterCount++;
281+
282+
bgdPxls.clear(); fgdPxls.clear();
283+
prBgdPxls.clear(); prFgdPxls.clear();
284+
285+
return iterCount;
286+
}
287+
288+
GCApplication gcapp;
289+
290+
static void on_mouse( int event, int x, int y, int flags, void* param )
291+
{
292+
gcapp.mouseClick( event, x, y, flags, param );
293+
}
294+
295+
int main( int argc, char** argv )
296+
{
297+
cv::CommandLineParser parser(argc, argv, "{help h||}{@input||}");
298+
if (parser.has("help"))
299+
{
300+
help();
301+
return 0;
302+
}
303+
string filename = parser.get<string>("@input");
304+
if( filename.empty() )
305+
{
306+
cout << "\nDurn, empty filename" << endl;
307+
return 1;
308+
}
309+
Mat image = imread( filename, 1 );
310+
if( image.empty() )
311+
{
312+
cout << "\n Durn, couldn't read image filename " << filename << endl;
313+
return 1;
314+
}
315+
316+
help();
317+
318+
const string winName = "image";
319+
namedWindow( winName, WINDOW_AUTOSIZE );
320+
setMouseCallback( winName, on_mouse, 0 );
321+
322+
gcapp.setImageAndWinName( image, winName );
323+
gcapp.showImage();
324+
325+
for(;;)
326+
{
327+
char c = (char)waitKey(0);
328+
switch( c )
329+
{
330+
case '\x1b':
331+
cout << "Exiting ..." << endl;
332+
goto exit_main;
333+
case 'r':
334+
cout << endl;
335+
gcapp.reset();
336+
gcapp.showImage();
337+
break;
338+
case 'n':
339+
int iterCount = gcapp.getIterCount();
340+
cout << "<" << iterCount << "... ";
341+
int newIterCount = gcapp.nextIter();
342+
if( newIterCount > iterCount )
343+
{
344+
gcapp.showImage();
345+
cout << iterCount << ">" << endl;
346+
}
347+
else
348+
cout << "rect must be determined>" << endl;
349+
break;
350+
}
351+
}
352+
353+
exit_main:
354+
destroyWindow( winName );
355+
return 0;
356+
}

0 commit comments

Comments
 (0)