【OpenCV】使用floodfill()實現PhotoShop魔棒功能

pamxy發表於2013-05-29

轉自:http://blog.csdn.net/xiaowei_cqu/article/details/8987387

在OpenCV中看到一個很有意思的函式:floodfill()

使用給定顏色填充一個聯通的區域

  1. C++: int floodFill(InputOutputArray image, Point seedPoint,   
  2. Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(),   
  3. Scalar upDiff=Scalar(), int flags=4 )  
C++: int floodFill(InputOutputArray image, Point seedPoint, 
Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), 
Scalar upDiff=Scalar(), int flags=4 )


一個簡單的例子:

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

#include <iostream>

using namespace cv;
using namespace std;


//floodfill()
//Fills a connected component with the given color.

static void help()
{
	cout << "\nThis program demonstrated the floodFill() function\n"
		"Call:\n"
		"./ffilldemo [image_name -- Default: fruits.jpg]\n" << endl;

	cout << "Hot keys: \n"
		"\tESC - quit the program\n"
		"\tc - switch color/grayscale mode\n"
		"\tm - switch mask mode\n"
		"\tr - restore the original image\n"
		"\ts - use null-range floodfill\n"
		"\tf - use gradient floodfill with fixed(absolute) range\n"
		"\tg - use gradient floodfill with floating(relative) range\n"
		"\t4 - use 4-connectivity mode\n"
		"\t8 - use 8-connectivity mode\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 != CV_EVENT_LBUTTONDOWN )
		return;

	Point seed = Point(x,y);
	int lo = ffillMode == 0 ? 0 : loDiff;
	int up = ffillMode == 0 ? 0 : upDiff;
	int flags = connectivity + (newMaskVal << 8) +
		(ffillMode == 1 ? CV_FLOODFILL_FIXED_RANGE : 0);
	int b = (unsigned)theRNG() & 255;
	int g = (unsigned)theRNG() & 255;
	int r = (unsigned)theRNG() & 255;
	Rect ccomp;

	Scalar newVal = isColor ? Scalar(b, g, r) : Scalar(r*0.299 + g*0.587 + b*0.114);
	Mat dst = isColor ? image : gray;
	int area;

	if( useMask )
	{
		threshold(mask, mask, 1, 128, CV_THRESH_BINARY);
		area = floodFill(dst, mask, seed, newVal, &ccomp, Scalar(lo, lo, lo),
			Scalar(up, up, up), flags);
		imshow( "mask", mask );
	}
	else
	{
		area = floodFill(dst, seed, newVal, &ccomp, Scalar(lo, lo, lo),
			Scalar(up, up, up), flags);
	}

	imshow("image", dst);
	cout << area << " pixels were repainted\n";
}


int main( )
{
	char* filename="0.png";
	image0 = imread(filename, 1);

	if( image0.empty() )
	{
		cout << "Image empty. Usage: ffilldemo <image_name>\n";
		return 0;
	}
	help();
	image0.copyTo(image);
	cvtColor(image0, gray, CV_BGR2GRAY);
	mask.create(image0.rows+2, image0.cols+2, CV_8UC1);

	namedWindow( "image", 0 );
	createTrackbar( "lo_diff", "image", &loDiff, 255, 0 );
	createTrackbar( "up_diff", "image", &upDiff, 255, 0 );

	setMouseCallback( "image", onMouse, 0 );

	for(;;)
	{
		imshow("image", isColor ? image : gray);

		int c = waitKey(0);
		if( (c & 255) == 27 )
		{
			cout << "Exiting ...\n";
			break;
		}
		switch( (char)c )
		{
		case 'c':
			if( isColor )
			{
				cout << "Grayscale mode is set\n";
				cvtColor(image0, gray, CV_BGR2GRAY);
				mask = Scalar::all(0);
				isColor = false;
			}
			else
			{
				cout << "Color mode is set\n";
				image0.copyTo(image);
				mask = Scalar::all(0);
				isColor = true;
			}
			break;
		case 'm':
			if( useMask )
			{
				destroyWindow( "mask" );
				useMask = false;
			}
			else
			{
				namedWindow( "mask", 0 );
				mask = Scalar::all(0);
				imshow("mask", mask);
				useMask = true;
			}
			break;
		case 'r':
			cout << "Original image is restored\n";
			image0.copyTo(image);
			cvtColor(image, gray, CV_BGR2GRAY);
			mask = Scalar::all(0);
			break;
		case 's':
			cout << "Simple floodfill mode is set\n";
			ffillMode = 0;
			break;
		case 'f':
			cout << "Fixed Range floodfill mode is set\n";
			ffillMode = 1;
			break;
		case 'g':
			cout << "Gradient (floating range) floodfill mode is set\n";
			ffillMode = 2;
			break;
		case '4':
			cout << "4-connectivity mode is set\n";
			connectivity = 4;
			break;
		case '8':
			cout << "8-connectivity mode is set\n";
			connectivity = 8;
			break;
		}
	}

	return 0;
}


點選圖示改變影象中的連圖區域的顏色:



(轉載請註明作者和出處:http://blog.csdn.net/xiaowei_cqu 未經允許請勿用於商業用途)


 

 

相關文章