【发布时间】:2019-06-29 07:08:41
【问题描述】:
我有一个奶牛场的图像。在图像中,有两个感兴趣区域 (ROI)。在每个 ROI 中,我希望一切都是黑色的。
ROI的每个角的坐标是-
1= [0, 1440]
2= [0, 1087]
3= [977, 80]
4= [1925, 67]
5= [2560, 800]
6= [2560, 1440]
7= [1465, 1440]
8= [1455,60]
我正在使用以下代码来掩盖红色区域,并使 ROI 中的所有内容都变黑。
import cv2, numpy as np
original_frame = cv2.imread("original.jpg")
frame = original_frame.copy()
# pts - location of the corners of the roi
pts = np.array([[0, 1450], [0, 1087], [977, 80], [1925, 67], [2560, 800], [2560, 1440]])
(x,y,w,h) = cv2.boundingRect(pts)
pts = pts - pts.min(axis=0)
mask = np.zeros(original_frame.shape, np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
result = cv2.bitwise_and(original_frame, mask)
cv2.imwrite("out.jpg", result)
结果相当不错,但仍然覆盖了顶部的一些额外区域。
如果我尝试通过更改来掩盖蓝色区域
pts = np.array([[1455,60], [1925, 67], [2560, 800], [2560, 1440],[1465, 1440] ])
有没有办法为蓝色 ROI 获得正确的结果?
【问题讨论】:
标签: python-3.x opencv3.0