计算机视觉图像遮罩问题

时间:2018-06-30 10:08:05

标签: python-3.x opencv cv2

有人可以帮助我吗?当我使用此功能时,它适用于红色和黄色信号图像,但不适用于绿色。绿色信号图像全为黑色。知道怎么了。但是令人惊讶的是,如果我将其设置为[ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetData(bool withfilters, string filteredtext) ,它将显示绿色信号图像,而其他两个图像是黑色。我正在使用BGR2HSV导入图像,所以我猜默认为Matplotlib

def mask(rgb_image):

RGB

用于读取图像的代码:

hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2HSV)

## mask of red color range 1
red_mask1 = cv2.inRange(hsv_image, (0,20,0), (10,255,255))

## mask of red color range 2
red_mask2 = cv2.inRange(hsv_image, (170,20,0), (180,255,255))

## mask of green
green_mask = cv2.inRange(hsv_image, (40,0,0), (80,255,255))

## mask of yellow
yellow_mask = cv2.inRange(hsv_image, (10,30,100), (30,255,255))

## final mask
mask1 = cv2.bitwise_or(red_mask1, red_mask2)
mask2 = cv2.bitwise_or(mask1, yellow_mask)
mask3 = cv2.bitwise_or(mask2, green_mask)

target = cv2.bitwise_and(rgb_image,rgb_image, mask=mask3)

plt.imshow(target)

1 个答案:

答案 0 :(得分:1)

有效:我的绿色范围不正确。应该是低:(80,20,20),高:(170,255,255)。