1

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)

Tanmay
  • 23
  • 1
  • 7
  • Hm... I was facing a similar problem but with leaves... I was having a hard time differentiating leaves it they overlapped in the image... I still haven't found the solution though :( – Sabito stands with Ukraine May 24 '20 at 18:48
  • do you anything in advance, like the number of lemons in the image or their size(s) or an roi? – Micka May 24 '20 at 18:52
  • @Micka nothing is provided in advance. The actual problem is to find the number of lemons in the image and segment them. I found a way around the number of lemons part but have no solution to the segmentation part. – Tanmay May 24 '20 at 19:02
  • @Tanmay can you explain what ```cnts = cnts[0] if len(cnts) == 2 else cnts[1] ``` is doing? – Sabito stands with Ukraine May 24 '20 at 19:22
  • Please consult the duplicate I've linked. It is a way of segmenting out individual objects that all share the same shape and colour distribution in an image. This will most likely solve your problem. – rayryeng May 24 '20 at 19:51
  • @rayryeng the link is really great and segmented the lemons collectively more accurately and smoothly, but it still is not segmenting them each individually. – Tanmay May 25 '20 at 16:54
  • Not true. You can use each of the markers derived from the marker image to help isolate out each lemon. – rayryeng May 25 '20 at 20:50

0 Answers0