Skip to content Skip to sidebar Skip to footer

How To Remove The Background Of An Object Using Opencv (python)

I need to remove the green color from the background and leave only the wheat grains in black background. Any suggestion will be appreciated? Here is how image looks like:

Solution 1:

You mean this? :

import cv2
import numpy as np

img = cv2.imread("image.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv, (0, 0, 0), (75, 255, 255))
imask = mask > 0
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

cv2.imwrite("result.png", green)

Output enter image description here

Post a Comment for "How To Remove The Background Of An Object Using Opencv (python)"