opencv python3 找图片色块_如何使用OpenCV检测图像中的彩色色块?

1586010002-jmsa.png

I am trying to detect if the picture(black and white sketch) is colored or not in the room conditions by a mobile camera.

jVmND.jpg

I have been able to get this result

zt6gi.png

using following code

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 rgb = new List ();

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.

Thanks

解决方案

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.

sqhTI.png

Then threshold the S with thresh of 100, you will get this.

5Ex7a.png

It will be easy to separate the colorful region in the threshed binary image.

As @Mark suggest, we can use adaptive thresh other than the fixed one. So, add THRESH_OTSU in the flags.

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

oPk9y.png

Related answers:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值