BGR values of masked image (OpenCV, Python)

2019-07-23 15:09发布

问题:

Using the follow image..

... I am applying this code to create a circle mask:

import cv2
import numpy as np

img = cv2.imread("car.png")

height, width, depth = img.shape
circle_img = np.zeros((height, width), np.uint8)

mask = cv2.circle(circle_img, (int(width / 2), int(height / 2)), 90, 1, thickness=-1)
masked_img = cv2.bitwise_and(img, img, mask=circle_img)

cv2.imshow("masked", masked_img)
cv2.waitKey(0)

This is the output..

How can I find BGR values of the circle using OpenCV ?

回答1:

You can do it using numpy arrays.

circle_locations = mask == 1
bgr = img[circle_locations]

EDIT: I'm not sure if your mask has values in {0, 1} though I assume it does. If its background value is 0 and all positive values are forground, just change the == 1 to a > 1.