我想使用OpenCV分离图像前景。这是我想要使用的代码:
file_name="foo.jpg"
path=""
img=cv2.imread(path+file_name)
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (50, 50, 450, 360)
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
img = img * mask2[:, :, np.newaxis]
我知道我可以通过执行以下操作裁剪包含图像前景的矩形:
img[h1:h2,w1:w2]
但我怎样才能找到h1,h2,h3和h4?