根据颜色分割图像 [英] split image on the basis of color

查看:44
本文介绍了根据颜色分割图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用聚类= 3的k均值之后获得了图像.现在,我想基于在k均值之后获得的颜色获得3个单独的图像.例如,考虑附带的图像.现在我需要一幅仅包含蓝色方块的图像.一个有字母v,另一个只有背景有没有可能使用OpenCV和python做到这一点.

I have obtained an image after applying k-means with clusters = 3. Now I want to obtain 3 separate images on the basis of colours obtained after k-means. For example, consider the attached image. Now I need one image such that it contains only the blue square. One having the letter v and one with just the background Is there any possible way to do that using OpenCV and python.

推荐答案

最通用,最简单的方法是为每个区域使用三种独特的灰色.(尽管在上图中我可以找到三个以上的灰度级,这可能是由于imgur压缩导致的变化.尽管到最后,k均值应该恰好给出三个BGR值)

The most general and simplest way to do it is using the three unique gray colors for each region. (Although I could find more than three gray levels in the above image, maybe due to variation as a result of compression of imgur. Though, at the end of the day, k-means should give exactly three BGR values)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
unique = np.unique(gray)
c1, c2, c3 = unique[0], unique[1], unique[2]

mask1 = np.zeros_like(gray)
mask1[gray == c1] = 255

mask2 = np.zeros_like(gray)
mask2[gray == c2] = 255

mask3 = np.zeros_like(gray)
mask3[mask3 == c3] = 255

这篇关于根据颜色分割图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆