0

I am using the below code for color-based segmentation using K-means. In this code, each cluster is saving into one image. In my case requirement is a bit different. I want to save only blue color images. Could you please help me how can I save only blue color images?

import numpy as np
import cv2
import pdb
from matplotlib import pyplot as plt
img = cv2.imread('a.png')
Z = np.float32(img.reshape((-1,3)))

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 4
_,labels,centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.reshape((img.shape[:-1]))
reduced = np.uint8(centers)[labels]

result = [np.hstack([img, reduced])]
for i, c in enumerate(centers):
    mask = cv2.inRange(labels, i, i)
    mask = np.dstack([mask]*3) # Make it 3 channel
    ex_img = cv2.bitwise_and(img, mask)
    ex_reduced = cv2.bitwise_and(reduced, mask)
    result.append(np.hstack([ex_img, ex_reduced]))
    pdb.set_trace()

cv2.imwrite('watermelon_out.jpg', np.vstack(result))

Original Image Original Image

After using this code I am getting result link below: aa

Expected Result: Expected Result

6
  • If you want only blue color image choose only the center which represents blue color cluster and plot all point in that cluster. you are plotting for all the clusters. Commented Jul 23, 2020 at 21:18
  • I know that but I am not able to implement this. Commented Jul 23, 2020 at 21:47
  • for all centers find the one closer to (0, 255, 0) distance wise and plot only for that center. Commented Jul 23, 2020 at 21:57
  • Could you please write the code snippet I am unable to implement this. Commented Jul 23, 2020 at 23:15
  • @RajatSingh Sorry I didn't see your code. I am applying it now. I will update you the final status. Thanks for sharing the code. Commented Jul 23, 2020 at 23:17

1 Answer 1

0
+50

This should only print the blue image. First find the center which is closest to blue color and then plot points only in cluster represented by that center

import numpy as np
import cv2
import pdb
from matplotlib import pyplot as plt
img = cv2.imread('a.png')
Z = np.float32(img.reshape((-1,3)))

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 4
_,labels,centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.reshape((img.shape[:-1]))
reduced = np.uint8(centers)[labels]


blue_dis = 99999999
blue_center = -1
b = (255, 50 , 0)
for i, c in enumerate(centers):
    dis = (c[0]-b[0])**2 + (c[1]-b[1])**2 + (c[1]-b[1])**2
    if dis < blue_dis:
        blue_center = i
        blue_dis = dis


    

result = [np.hstack([img, reduced])]
for i, c in enumerate(centers):
    if i!=blue_center:
        continue
    mask = cv2.inRange(labels, i, i)
    mask = np.dstack([mask]*3) # Make it 3 channel
    ex_img = cv2.bitwise_and(img, mask)
    ex_reduced = cv2.bitwise_and(reduced, mask)
    result.append(np.hstack([ex_img, ex_reduced]))
    pdb.set_trace()

cv2.imwrite('watermelon_out.jpg', np.vstack(result))
Sign up to request clarification or add additional context in comments.

8 Comments

This code is working for most of the images when I am changing the value of b = (0,255,0) into b = (255,0,0). For few images this code is NOT working properly. In that case, it is showing pink regions. Do you have any permanent solution?
i guess i have used wrong rgb value for blue color. The correct value is (0, 0, 255). If you can try with this value it should work. Also the code assumes there is a blue image present in image if not it will plot one that is closest to blue color.
b=(0, 0, 255) should work but unfortunately it is not working on different images. I am trying to figuring out the issue. b=(255, 0, 0) is working on 70% images.
can you share center values for images where b=(0, 0, 255) is not working. and also the image if possible. Maybe color we are looking for isn't exactly blue.
Great!!! Now it working properly. Thanks a lot. I am giving you 50 bounty reputation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.