如何使用OpenCV检测图像中的彩色色块?

Too*_*bal 6 opencv image-processing computer-vision color-detection

我试图通过移动相机检测房间条件下的图片(黑白素描)是否有颜色.

在此输入图像描述

我已经能够得到这个结果

在此输入图像描述

使用以下代码

Mat dest = new Mat (sections[i].rows(),sections[i].cols(),CvType.CV_8UC3);
Mat hsv_image = new Mat (sections[i].rows(),sections[i].cols(),CvType.CV_8UC3);

Imgproc.cvtColor (sections[i],hsv_image,Imgproc.COLOR_BGR2HSV);

List <Mat> rgb = new List<Mat> ();
Core.split (hsv_image, rgb);
Imgproc.equalizeHist (rgb [1], rgb [2]);
Core.merge (rgb, sections[i]);
Imgproc.cvtColor (sections[i], dest, Imgproc.COLOR_HSV2BGR);

Core.split (dest, rgb);
Run Code Online (Sandbox Code Playgroud)

我怎样才能成功地发现图像是否有颜色.颜色可以是任何颜色,它有房间条件.因为我是初学者,请帮助我.

谢谢

Kin*_*t 金 15

处理彩色图像HSV color-space是一个很好的方向.我拆分频道,发现S频道很棒.因为S有Saturation(???)颜色.

在此输入图像描述

然后阈值S与阈值100,你会得到这个. 在此输入图像描述

在脱粒二进制图像中分离彩色区域将是容易的.


正如@Mark建议的那样,我们可以使用除固定阈值之外的自适应阈值.所以,添加THRESH_OTSU标志.

核心python代码如下:

##(1) read into  bgr-space
img = cv2.imread("test.png")

##(2) convert to hsv-space, then split the channels
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

##(3) threshold the S channel using adaptive method(`THRESH_OTSU`)  
th, threshed = cv2.threshold(s, 100, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY)

##(4) print the thresh, and save the result
print("Thresh : {}".format(th))
cv2.imwrite("result.png", threshed)


## >>> Thresh : 85.0
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

相关答案:

  1. 检测图像中的彩色片段
  2. opencv android中的边缘检测
  3. OpenCV C++/Obj-C:检测一张纸/方形检测


归档时间:

查看次数:

2299 次

最近记录:

8 年,8 月 前