Search code examples
pythonopencvimage-processingscikit-imageedge-detection

How do i remove the background from this image, just want the lettuce from the picture?


image of lettuce

Just want to retain the lettuce, I have hundreds of image like this and would be comparing the size of lettuce, so to began with I tried the canny edge detection but it doesn't seems to work, any idea how shall move ahead with this


Solution

  • You can convert the RGB image into HSV image and segment the Green color region.

    import cv2
    import numpy as np
    
    frame=cv2.imread('a.png')
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    lower = np.array([50,50,50])
    upper = np.array([70,255,255])
    
    mask = cv2.inRange(hsv, lower, upper)
    res = cv2.bitwise_and(frame,frame, mask= mask)
    
    cv2.imshow('frame',frame)
    cv2.imshow('res',res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    The final output