I have the following image and have to segment each lemon separately.
I have tried a few things and the best I could get is this.
The code I used:
import cv2
img=cv2.imread('lemons1.jpeg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower=(10,150,0)
upper=(100, 255, 80)
mask1 = cv2.inRange(hsv, lower, upper)
mask = cv2.dilate(mask1, None, iterations=5)
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
blobs = 0
#Drawing contours and counting the lemons
for c in cnts:
cv2.drawContours(img, [c], -1, (36,255,12), 4)