6

I'am trying to use threshold for segmentation color. but it's not doesn't work. how can i segmentation red and green in this picture.

Thank

this image after using Kmeans

This image after using Kmeans

enter image description here

This image after Segmentation using threshold

Mycode

import numpy as np
import cv2

img = cv2.imread('watermelon.jpg')
Z = img.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 = 4
ret,label,center=cv2.kmeans(Z,K, criteria,10,cv2.KMEANS_RANDOM_CENTERS)

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
gray = cv2.cvtColor(res2,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

#segmentation
gray = cv2.cvtColor(res2,cv2.COLOR_BGR2GRAY)
ret, threshseg = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

cv2.imwrite('img_CV2_95.jpg',threshseg)
cv2.imwrite('img_CV2_94.jpg',res2)


cv2.imshow('threshseg',threshseg)
cv2.imshow('thresh',thresh)
cv2.imshow('res2',res2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
2
  • It would be really useful if you attached an unmodified input image. (And I guess you're still on OpenCV 2.4.x, given that you're calling kmeans with only 5 arguments? That would be useful to mention as well.) Commented Oct 14, 2018 at 14:34
  • 1
    Reshape label back to same width/height as img, then use inRange to create mask selecting all the pixels with given label. Something like this... Commented Oct 14, 2018 at 14:54

2 Answers 2

6

I'd take the advantage of the labels array and use that for segmentation.

First reshape it back to the same width/height of the input image.

labels = labels.reshape((img.shape[:-1]))

Now, let's say you want to grab all the pixels with label 2.

mask = cv2.inRange(labels, 2, 2)

And simply use it with cv2.bitwise_and to mask out the rest of the image.

mask = np.dstack([mask]*3) # Make it 3 channel
ex_img = cv2.bitwise_and(img, mask)

The nice thing about this approach is that you don't need to hardcode any colour ranges, so the same algorithm will work on many different images.


Sample Code:

Note: Written for OpenCV 3.x. Users of OpenCV 2.4.x need to change the call of cv2.kmeans appropriately (see docs for the difference).

import numpy as np
import cv2

img = cv2.imread('watermelon.jpg')
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]))

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

Sample Output:

Sample Output

Sample Output with different colours:

Another Sample Output

Sign up to request clarification or add additional context in comments.

Comments

5

If I understand correctly you would like to seperate green and red? If that is the case you could transform the image to HSV color space and extract colors with cv2.inRange() and cv2.bitwise_and(). Note that the code is made with OpenCV 3 and Python 3.5. Hope it helps a bit. Cheers!

Example code:

import cv2
import numpy as np

# Read the image and transform it to HSV color space
img = cv2.imread('watermelon.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# define ranges for colors in HSV color space you wish to display

## LIGHT AND DARK GREEN
lower_green_light = np.array([20,20,50])
upper_green_light = np.array([130,150,255])

## DARK GREEN
lower_green = np.array([0, 70, 50])
upper_green = np.array([170, 180, 100])

## RED
lower_red = np.array([170, 130, 0])
upper_red = np.array([180, 255, 255])

# Threshold with inRange() get only specific colors
mask_green = cv2.inRange(hsv, lower_green, upper_green)
mask_green_light = cv2.inRange(hsv, lower_green_light, upper_green_light)
mask_red = cv2.inRange(hsv, lower_red, upper_red)

# Perform bitwise operation with the masks and original image
res_green = cv2.bitwise_and(img,img, mask= mask_green)
res_green_light = cv2.bitwise_and(img,img, mask= mask_green_light)
res_red = cv2.bitwise_and(img,img, mask= mask_red)

# Display results
cv2.imshow('red', res_red)
cv2.imshow('green', res_green)
cv2.imshow('light green', res_green_light)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

enter image description here

enter image description here

enter image description here

Comments

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.