我有一个代表我感兴趣的区域的旋转矩形:
[[634 547]
[353 504]
[436 -41]
[717 1]]
我想快速过滤掉不在旋转矩形内的所有像素。请注意,矩形实际上超出了图像的范围,因此如果重要,我必须处理这种边缘情况。
您可以使用fillPoly()
在掩码数组上绘制矩形,并使用掩码数组选择所需的像素:
import cv2
import numpy as np
rect = [[634, 547],[353, 504],[436, -41],[717, 1]]
poly = np.array([rect], dtype=np.int32)
img = np.zeros((800, 800), np.int8)
cv2.fillPoly(img, poly, 255)
imshow(img, cmap="gray")
面具图像:
setRoi
),fillPoly
)例如,您可以: