I am new in image processing and I need to count the number of object of different colours. Here is the image:
At first I have converted the image from RGB to HSV. After that I have segmented the colors with the following:
blue = np.uint8([[[0,191,255]]])
hsv_blue = cv2.cvtColor(blue,cv2.COLOR_RGB2HSV)
lower_blue = np.array([(hsv_blue[0][0][0]-10,100,100)])
upper_blue = np.array([(hsv_blue[0][0][0]+10,255,255)])
mask_1 = cv2.inRange(hsv, lower_blue, upper_blue)
result_1 = cv2.bitwise_and(img, img, mask=mask_1)
plt.imshow(result_1)
result_1 can be seen here
And I did it for the other colours too (I don't know whether or not exists a method to detect colours).
My idea was that to use cv2.findContours() function on the result_1 image. Hence gray scale is necessary for this, I have splitted the result_1 into h,s,v channels with
h,s,v = cv2.split(result_1)
and according to this: Convert HSV to grayscale in OpenCV I only need the value V (I don't have to convert HSV to GRAY). After using findContours I only need to print out the length of the result.
I have tried these solutions:
cent , _ = cv2.findContours(v_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print(len(cent))
and the result is 1 which is incorrect.
cent , _ = cv2.findContours(mask_1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print(len(cent))
and the result is 689.
What am I doing wrong?/Are there simpler/better solutions? (I have to use openCV).