我正在使用 Python 和 OpenCV。我现在正在使用grabcut()
裁剪出我想要的对象。这是我的代码:
img = cv2.imread('test.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (2,2,630,930)
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]
之后,我尝试找出轮廓。
我试图通过下面的代码找到轮廓:
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
And it returns a contours array
with length 48
. When I draw this out:
![enter image description here](https://i.stack.imgur.com/BqZqK.png)
第一个问题是如何获得此抓取切割的轮廓(数组)?
Second question: as you can see, the background color is black. How can I change the background color to white?
谢谢。