0

I have an image and I need to erase all the green elements.

Right now I'm doing this,

hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
lower_green, upper_green = np.array([36, 25, 25]), np.array([70, 255, 255])
mask1 = cv.inRange(hsv, lower_green, upper_green)
result = cv.bitwise_and(img, img, mask=mask)

cv.imwrite("logo.png", result)

The problem is if I do this, I extract all the colors from my image except the green and I want the opposite. I want to keep the original image without green.

For this reason I tried to use two masks, using the green values as limiters. But now I obtain a black image and furthermore I think I'm complicating the method.

hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
lower_green, upper_green = np.array([36, 25, 25]), np.array([70, 255, 255])
mask1 = cv.inRange(hsv, np.array([0, 25, 25]), lower_green)
mask2 = cv.inRange(hsv, upper_green, np.array([179, 25, 25]))
mask = cv.bitwise_or(mask1, mask2)
result = cv.bitwise_and(img, img, mask=mask2)

cv.imwrite("logo.png", result)

I'm doing correctly? How can I do this? I always find explanations about how to find the color to eliminate the rest, but not my goal.

Thank you!

1
  • please illustrate your issue with pictures. Commented Dec 12, 2020 at 21:59

2 Answers 2

1

I believe you want to invert your mask.

result = cv.bitwise_and(img, img, mask=~mask)

Note the ~, that inverts the mask.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe I don't understand well but if you want to work with green channel why did you convert image to HSV? in RGB; extract channels, remove green channel (or range) and merge channels again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.