Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/ffilldemo.cpp
16337 views
1
#include "opencv2/imgproc.hpp"
2
#include "opencv2/imgcodecs.hpp"
3
#include "opencv2/videoio.hpp"
4
#include "opencv2/highgui.hpp"
5
6
#include <iostream>
7
8
using namespace cv;
9
using namespace std;
10
11
static void help()
12
{
13
cout << "\nThis program demonstrated the floodFill() function\n"
14
"Call:\n"
15
"./ffilldemo [image_name -- Default: ../data/fruits.jpg]\n" << endl;
16
17
cout << "Hot keys: \n"
18
"\tESC - quit the program\n"
19
"\tc - switch color/grayscale mode\n"
20
"\tm - switch mask mode\n"
21
"\tr - restore the original image\n"
22
"\ts - use null-range floodfill\n"
23
"\tf - use gradient floodfill with fixed(absolute) range\n"
24
"\tg - use gradient floodfill with floating(relative) range\n"
25
"\t4 - use 4-connectivity mode\n"
26
"\t8 - use 8-connectivity mode\n" << endl;
27
}
28
29
Mat image0, image, gray, mask;
30
int ffillMode = 1;
31
int loDiff = 20, upDiff = 20;
32
int connectivity = 4;
33
int isColor = true;
34
bool useMask = false;
35
int newMaskVal = 255;
36
37
static void onMouse( int event, int x, int y, int, void* )
38
{
39
if( event != EVENT_LBUTTONDOWN )
40
return;
41
42
Point seed = Point(x,y);
43
int lo = ffillMode == 0 ? 0 : loDiff;
44
int up = ffillMode == 0 ? 0 : upDiff;
45
int flags = connectivity + (newMaskVal << 8) +
46
(ffillMode == 1 ? FLOODFILL_FIXED_RANGE : 0);
47
int b = (unsigned)theRNG() & 255;
48
int g = (unsigned)theRNG() & 255;
49
int r = (unsigned)theRNG() & 255;
50
Rect ccomp;
51
52
Scalar newVal = isColor ? Scalar(b, g, r) : Scalar(r*0.299 + g*0.587 + b*0.114);
53
Mat dst = isColor ? image : gray;
54
int area;
55
56
if( useMask )
57
{
58
threshold(mask, mask, 1, 128, THRESH_BINARY);
59
area = floodFill(dst, mask, seed, newVal, &ccomp, Scalar(lo, lo, lo),
60
Scalar(up, up, up), flags);
61
imshow( "mask", mask );
62
}
63
else
64
{
65
area = floodFill(dst, seed, newVal, &ccomp, Scalar(lo, lo, lo),
66
Scalar(up, up, up), flags);
67
}
68
69
imshow("image", dst);
70
cout << area << " pixels were repainted\n";
71
}
72
73
74
int main( int argc, char** argv )
75
{
76
cv::CommandLineParser parser (argc, argv,
77
"{help h | | show help message}{@image|../data/fruits.jpg| input image}"
78
);
79
if (parser.has("help"))
80
{
81
parser.printMessage();
82
return 0;
83
}
84
string filename = parser.get<string>("@image");
85
image0 = imread(filename, 1);
86
87
if( image0.empty() )
88
{
89
cout << "Image empty\n";
90
parser.printMessage();
91
return 0;
92
}
93
help();
94
image0.copyTo(image);
95
cvtColor(image0, gray, COLOR_BGR2GRAY);
96
mask.create(image0.rows+2, image0.cols+2, CV_8UC1);
97
98
namedWindow( "image", 0 );
99
createTrackbar( "lo_diff", "image", &loDiff, 255, 0 );
100
createTrackbar( "up_diff", "image", &upDiff, 255, 0 );
101
102
setMouseCallback( "image", onMouse, 0 );
103
104
for(;;)
105
{
106
imshow("image", isColor ? image : gray);
107
108
char c = (char)waitKey(0);
109
if( c == 27 )
110
{
111
cout << "Exiting ...\n";
112
break;
113
}
114
switch( c )
115
{
116
case 'c':
117
if( isColor )
118
{
119
cout << "Grayscale mode is set\n";
120
cvtColor(image0, gray, COLOR_BGR2GRAY);
121
mask = Scalar::all(0);
122
isColor = false;
123
}
124
else
125
{
126
cout << "Color mode is set\n";
127
image0.copyTo(image);
128
mask = Scalar::all(0);
129
isColor = true;
130
}
131
break;
132
case 'm':
133
if( useMask )
134
{
135
destroyWindow( "mask" );
136
useMask = false;
137
}
138
else
139
{
140
namedWindow( "mask", 0 );
141
mask = Scalar::all(0);
142
imshow("mask", mask);
143
useMask = true;
144
}
145
break;
146
case 'r':
147
cout << "Original image is restored\n";
148
image0.copyTo(image);
149
cvtColor(image, gray, COLOR_BGR2GRAY);
150
mask = Scalar::all(0);
151
break;
152
case 's':
153
cout << "Simple floodfill mode is set\n";
154
ffillMode = 0;
155
break;
156
case 'f':
157
cout << "Fixed Range floodfill mode is set\n";
158
ffillMode = 1;
159
break;
160
case 'g':
161
cout << "Gradient (floating range) floodfill mode is set\n";
162
ffillMode = 2;
163
break;
164
case '4':
165
cout << "4-connectivity mode is set\n";
166
connectivity = 4;
167
break;
168
case '8':
169
cout << "8-connectivity mode is set\n";
170
connectivity = 8;
171
break;
172
}
173
}
174
175
return 0;
176
}
177
178