BGR values of masked image (OpenCV, Python)

2019-07-23 14:51发布

Using the follow image..

car

... 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..

masked_car

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

1条回答
Viruses.
2楼-- · 2019-07-23 15:48

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.

查看更多
登录 后发表回答