split image on the basis of color

巧了我就是萌 提交于 2021-01-01 08:30:48

问题


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.


回答1:


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



回答2:


You can solve the problem by calculating the histogram of the image. The below plot shows the peaks of the image.

From this, you can threshold the colors. The code and result:

import cv2 
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread("inputs/hist.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray],[0],None,[256],[0,256])
colors = np.where(hist>5000)
img_number = 0
for color in colors[0]:
    print(color)
    split_image = img.copy()
    split_image[np.where(gray != color)] = 0
    cv2.imwrite(str(img_number)+".jpg",split_image)
    img_number+=1
plt.hist(gray.ravel(),256,[0,256])
plt.savefig('plt')
plt.show()

Results:



来源:https://stackoverflow.com/questions/48735574/split-image-on-the-basis-of-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!