JustNick
JustNick

Reputation: 141

How to remove color from image

I have an image with green background, for example:

enter image description here

My purpose is to show everything that is not green

There`s the code to highlight green

import cv2
import numpy as np

low_green = np.array([25, 52, 72])
high_green = np.array([102, 255, 255])

while True:
    img = cv2.imread('someimage.jpg')
    img = cv2.resize(img, (900, 650), interpolation=cv2.INTER_CUBIC)

    # convert BGR to HSV
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # create the Mask
    mask = cv2.inRange(imgHSV, low_green, high_green)

    cv2.imshow("mask", mask)
    cv2.imshow("cam", img)
    cv2.waitKey(10)

And mask image enter image description here

How do i show everything that is black on mask image?

Upvotes: 2

Views: 15424

Answers (2)

JustNick
JustNick

Reputation: 141

Here`s the code:

import cv2
import numpy as np

low_green = np.array([25, 52, 72])
high_green = np.array([102, 255, 255])

while True:
    img = cv2.imread('someimage.JPG')
    img = cv2.resize(img, (900, 650), interpolation=cv2.INTER_CUBIC)

    # convert BGR to HSV
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # create the Mask
    mask = cv2.inRange(imgHSV, low_green, high_green)
    # inverse mask
    mask = 255-mask
    res = cv2.bitwise_and(img, img, mask=mask)

    cv2.imshow("mask", mask)
    cv2.imshow("cam", img)
    cv2.imshow('res', res)
    cv2.waitKey(10)

and the resultenter image description here

Upvotes: 7

Gabriel Fonseca
Gabriel Fonseca

Reputation: 199

you have the green mask, where white is what is green and black what isn't...

So you take the inverse of that mask(black becomes white and white black) and apply such mask on your image.

Upvotes: 3

Related Questions