#include <iostream>
static void help(char** argv)
{
cout << "\n此程式演示了 floodFill() 函式\n"
"呼叫:\n"
<< argv[0]
<< " [image_name -- 預設: fruits.jpg]\n" << endl;
cout << "熱鍵: \n"
"\tESC - 退出程式\n"
"\tc - 切換顏色/灰度模式\n"
"\tm - 切換掩碼模式\n"
"\tr - 恢復原始影像\n"
"\ts - 使用空範圍 floodfill\n"
"\tf - 使用固定(絕對)範圍的梯度 floodfill\n"
"\tg - 使用浮動(相對)範圍的梯度 floodfill\n"
"\t4 - 使用 4-連通模式\n"
"\t8 - 使用 8-連通模式\n" << endl;
}
Mat image0, image, gray, mask;
int ffillMode = 1;
int loDiff = 20, upDiff = 20;
int connectivity = 4;
int isColor = true;
bool useMask = false;
int newMaskVal = 255;
static void onMouse( int event, int x, int y, int, void* )
{
if( event != EVENT_LBUTTONDOWN )
return;
int lo = ffillMode == 0 ? 0 : loDiff;
int up = ffillMode == 0 ? 0 : upDiff;
int flags = connectivity + (newMaskVal << 8) +
(ffillMode == 1 ? FLOODFILL_FIXED_RANGE : 0);
int b = (unsigned)theRNG() & 255;
int g = (unsigned)theRNG() & 255;
int r = (unsigned)theRNG() & 255;
Mat dst = isColor ? image : gray;
int area;
if( useMask )
{
threshold(mask, mask, 1, 128, THRESH_BINARY);
area = floodFill(dst, mask, seed, newVal, &ccomp,
Scalar(lo, lo, lo),
imshow( "mask", mask );
}
else
{
}
cout << area << " 畫素被重繪\n";
}
int main(
int argc,
char** argv )
{
"{help h | | 顯示幫助資訊}{@image|fruits.jpg| 輸入影像}"
);
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string filename = parser.get<string>("@image");
image0 =
imread(samples::findFile(filename), 1);
if( image0.empty() )
{
cout << "影像為空\n";
parser.printMessage();
return 0;
}
help(argv);
image0.copyTo(image);
for(;;)
{
imshow(
"image", isColor ? image : gray);
if( c == 27 )
{
cout << "正在退出 ...\n";
break;
}
switch( c )
{
case 'c'
if( isColor )
{
cout << "灰度模式已設定\n";
isColor = false;
}
else
{
cout << "顏色模式已設定\n";
image0.copyTo(image);
isColor = true;
}
break;
case 'm'
if( useMask )
{
useMask = false;
}
else
{
useMask = true;
}
break;
case 'r'
cout << "原始影像已恢復\n";
image0.copyTo(image);
break;
case 's'
cout << "簡單 floodfill 模式已設定\n";
ffillMode = 0;
break;
case 'f'
cout << "固定範圍 floodfill 模式已設定\n";
ffillMode = 1;
break;
case 'g'
cout << "梯度(浮動範圍)floodfill 模式已設定\n";
ffillMode = 2;
break;
case '4'
cout << "4-連通模式已設定\n";
connectivity = 4;
break;
case '8'
cout << "8-連通模式已設定\n";
connectivity = 8;
break;
}
}
return 0;
}
如果陣列沒有元素,則返回 true。
int64_t int64
2D 矩形的模板類。
定義 types.hpp:444
#define CV_8UC1
定義 interface.h:88
GMat mask(const GMat &src, const GMat &mask)
將掩碼應用於矩陣。
void imshow(const String &winname, InputArray mat)
在指定視窗中顯示影像。
int waitKey(int delay=0)
等待按鍵按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
建立視窗。
void destroyWindow(const String &winname)
銷燬指定視窗。
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
為指定視窗設定滑鼠處理程式。
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
建立滑動條並將其附加到指定視窗。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
從檔案載入影像。
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
將影像從一個顏色空間轉換為另一個顏色空間。
int floodFill(InputOutputArray image, InputOutputArray mask, Point seedPoint, Scalar newVal, Rect *rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4)
用給定顏色填充連通分量。
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3