KG_1
KG_1

Reputation: 109

OpenCV bitwise_and produces original image and not masked image

I am trying to group scatter points which are of same color using hsv values, I am able to create a mask (as seen below) but when I try to do a bitwise_and operation on the original image(og_image) and the mask, the result is always the original image and not the masked image. Am I doing something wrong over here? Any help would be appreciated. thanks in advance!

Original Image -

Orignal_image

mask(for indianred color) -

mask

output -

output

here is the code -

import cv2
import numpy as np
import copy

def extract_colors(image):
    # Convert the image to HSV color space.
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    ## some processing
    return hsv_image, #colors


def remove_black_white(img):
    # some processing to detect pixels with same r,g,b values
    colors = set() # a set of colors with same r,g,b values
    for color in colors:
        img[np.all(img==color, axis=-1)] = (0,0,0)
    return img

if __name__ == "__main__":
    image = cv2.imread("875.png")
    og_image = copy.deepcopy(image)
    black_img = remove_black_white(image)
    hsvImage, colors = extract_colors(black_img)
    result = copy.deepcopy(og_image)
    lower = np.array([min_h, min_s, min_v], dtype=np.uint8)
    upper = np.array([max_h, max_s, max_v], dtype=np.uint8)
    mask = cv2.inRange(hsvImage, lower, upper)
    res = cv2.bitwise_and(og_image, og_image, mask)
    cv2.imshow('mask_'+k, mask)
    cv2.imshow('masked_image_'+k, res)
    cv2.waitKey(0)

Kindly help!

Upvotes: 1

Views: 145

Answers (0)

Related Questions