【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
    评论
### 回答1: opencv是一个开源的计算机视觉库,可以在Python中使用。通过使用opencv的色相饱和度转换功能,可以实现类似Photoshop中对图像进行色相和饱和度的调整。 首先,我们需要导入opencv库,并读取一张图片作为输入图像。可以使用cv2.imread()函数来读取图像。 接下来,我们可以通过调用cv2.cvtColor()函数来将图像转换为HSV色彩空间。HSV颜色模型由色调(Hue),饱和度(Saturation)和亮度(Value)组成。我们只需要调整色调和饱和度,所以我们将图像转换为HSV色彩空间。 然后,我们可以通过使用cv2.convertScaleAbs()函数来调整色相和饱和度的值。该函数有三个参数,分别是输入图像、输出图像和缩放因子。我们可以将缩放因子设置为一个小数,以调整图像的色相和饱和度。 最后,我们可以使用cv2.cvtColor()函数将图像转换回BGR颜色空间,然后可以使用cv2.imshow()函数显示调整后的图像。 下面是一个简单的示例代码: ```python import cv2 # 读取输入图像 image = cv2.imread('image.jpg') # 将图片转换为HSV颜色空间 hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # 调整色相和饱和度的值 hue_scale = 0.5 # 色相缩放因子 saturation_scale = 1.5 # 饱和度缩放因子 # 调整色相和饱和度的值 hsv_image[:,:,0] = cv2.convertScaleAbs(hsv_image[:,:,0], alpha=hue_scale) hsv_image[:,:,1] = cv2.convertScaleAbs(hsv_image[:,:,1], alpha=saturation_scale) # 将图片转换回BGR颜色空间 new_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) # 显示调整后的图像 cv2.imshow('Adjusted Image', new_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这个示例代码中,我们将色相缩放因子设为0.5,饱和度缩放因子设为1.5。你可以根据自己的需要调整这两个值来得到不同的效果。调整后的图像会在一个新的窗口中显示出来。 这样,我们就可以通过opencv和Python来实现简单的色相和饱和度调整,类似于Photoshop中的功能。 ### 回答2: 要使用OpenCV和Python实现Photoshop中的色相饱和度功能,可以按照以下步骤进行: 1. 导入必要的库:使用`import cv2`导入OpenCV库。 2. 加载图像:使用`cv2.imread()`函数加载要处理的图像。 3. 转换颜色空间:将加载的图像转换为HSV颜色空间,以便可以对色相和饱和度进行调整。使用`cv2.cvtColor()`函数将图像从BGR颜色空间转换为HSV颜色空间。 4. 调整色相和饱和度:使用`cv2.cvtColor()`函数的第三个参数来调整色相和饱和度。该参数的取值范围为[-180, 180],其中负值表示减少色相或饱和度,正值表示增加色相或饱和度。 5. 转换颜色空间:将调整后的图像转换回BGR颜色空间,以便显示或保存。使用`cv2.cvtColor()`函数将图像从HSV颜色空间转换为BGR颜色空间。 6. 显示或保存结果:使用`cv2.imshow()`函数显示调整后的图像,并使用`cv2.waitKey()`函数等待用户按下键盘上的任意键。或使用`cv2.imwrite()`函数将调整后的图像保存到文件中。 7. 释放资源:使用`cv2.destroyAllWindows()`函数释放窗口和图像资源。 以下是一个示例代码来实现上述步骤: ```python import cv2 # 加载图像 image = cv2.imread('input.jpg') # 转换颜色空间为HSV hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # 调整色相和饱和度 hsv_image[..., 0] += 30 # 增加色相 hsv_image[..., 1] *= 1.5 # 增加饱和度 # 转换颜色空间为BGR adjusted_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) # 显示调整后的图像 cv2.imshow('Adjusted Image', adjusted_image) cv2.waitKey(0) # 保存调整后的图像 cv2.imwrite('output.jpg', adjusted_image) # 释放资源 cv2.destroyAllWindows() ``` 这段示例代码实现了将输入图像的色相增加30度,饱和度增加1.5倍的效果。您可以根据实际需求调整这两个参数来实现不同的色相饱和度调整效果。 ### 回答3: 要使用OpenCV在Python中实现Photoshop的色相饱和度功能,你需要按照以下步骤进行操作: 1. 导入所需的库和模块: ```python import cv2 import numpy as np ``` 2. 读取图像: ```python image = cv2.imread("your_image.jpg") ``` 3. 将图像转换为HSV颜色空间: ```python hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) ``` 4. 调整色相和饱和度参数: ```python hue_shift = 30 # 色相偏移量(取值范围:-180到180) saturation_factor = 1.5 # 饱和度增强因子(取值范围:0到正无穷) hsv_image[:, :, 0] = (hsv_image[:, :, 0] + hue_shift) % 180 hsv_image[:, :, 1] = hsv_image[:, :, 1] * saturation_factor ``` 5. 将修改后的图像转换回BGR颜色空间: ```python result_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) ``` 6. 显示和保存结果图像: ```python cv2.imshow("Result Image", result_image) cv2.imwrite("result_image.jpg", result_image) cv2.waitKey(0) ``` 通过以上步骤,你可以使用OpenCV和Python实现类似Photoshop的色相饱和度功能。根据需要,你可以调整色相和饱和度的参数来得到不同的效果。记得根据自己的实际情况修改读取和保存图像的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值