如何使用 OpenCV 检测图像中的色块? [英] How to detect colored patches in an image using OpenCV?
问题描述
我正在尝试通过移动相机检测房间条件下的图片(黑白草图)是否着色.
I am trying to detect if the picture(black and white sketch) is colored or not in the room conditions by a mobile camera.
我已经能够得到这个结果
I have been able to get this result
使用以下代码
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);
我怎样才能成功地找出图像是否有颜色.颜色可以是任意的,它有房间条件.请帮助我,因为我是初学者.
How can I sucessfully find out if the image is colored or not. The color can be any and it has room conditions. Please help me on this as I am beginner to it.
谢谢
推荐答案
在HSV color-space
中处理彩色图像是一个很好的方向.我拆分了频道,发现 S
频道很棒.因为是色彩度的饱和度
.
To process the colorful image in HSV color-space
is a good direction. And I split the channels and find the S
channel is great. Because S
is Saturation(饱和度)
of color.
然后将 S
阈值设为 100
,你会得到这个.
Then threshold the S
with thresh of 100
, you will get this.
很容易在脱粒后的二值图像中分离彩色区域.
It will be easy to separate the colorful region in the threshed binary image.
正如@Mark 所建议的,我们可以使用除固定阈值之外的自适应阈值.因此,在标志中添加 THRESH_OTSU
.
As @Mark suggest, we can use adaptive thresh other than the fixed one. So, add THRESH_OTSU
in the flags.
核心python代码如下:
Core python code is presented as follow:
##(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
相关答案:
这篇关于如何使用 OpenCV 检测图像中的色块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!