我有一个图像和一个面具.两者都是numpy数组.我通过GraphSegmentation(cv2.ximgproc.segmentation)获取掩码,因此该区域不是矩形,而是不分割.我想得到一个与蒙面区域大小相同的矩形,但我不知道有效的方法.
换句话说,未屏蔽的像素值为0,屏蔽像素的值大于0,所以我想得到一个矩形...
我的代码如下
segmentation = cv2.ximgproc.segmentation.createGraphSegmentation()
src = cv2.imread('image_file')
segment = segmentation.processImage(src)
for i in range(np.max(segment)):
dst = np.array(src)
dst[segment != i] = 0
cv2.imwrite('output_file', dst)
Run Code Online (Sandbox Code Playgroud)