【OpenCV】使用floodfill()实现PhotoShop魔棒功能

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

使用给定颜色填充一个联通的区域

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

一个简单的例子:

[cpp]  view plain copy
  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. }  

点击图标改变图像中的连图区域的颜色:



转载 :http://blog.csdn.net/xiaowei_cqu/article/details/8987387

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值