opencv python k-means seperate all the colours into different images

asked 2017-03-03 19:00:38 -0500

Prototype gravatar image

following is the opencv python code to get clustered image. After clustering, how do I seperate each color into different images i.e one cluster should be in one image , and the others in seoerate images. Can some please help me as soon as possible? If there is a way to put all the colors in one image one after the other like in the form of a bar graph, that would be great too. Please help me as soon as possible.

import numpy as np
import cv2

cap = cv2.VideoCapture(1)

# Capture frame-by-frame
ret, frame1 =
ret, frame2 =
ret, frame3 =
ret, frame4 =
ret, frame5 =
ret, frame =

Z = frame.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 3

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((frame.shape))

edit retag flag offensive close merge delete