OpenCV grabcut() background color and Contour in Python
<p>I am using Python and OpenCV. I am now using <code>grabcut()</code> to crop out the object I want. Here is my code:</p>

<pre><code>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]
</code></pre>

<p><a href="https://i.stack.imgur.com/gpo6i.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gpo6i.jpg" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/yNFCs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yNFCs.jpg" alt="enter image description here"></a></p>

<p>Afterwards, I try to find out the contour.</p>

<p>I have tried to find the contour by the code below:</p>

<pre><code>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)
</code></pre>

<p>And it returns <code>a contours array</code> with length <code>48</code>. When I draw this out:
<a href="https://i.stack.imgur.com/BqZqK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BqZqK.png" alt="enter image description here"></a></p>

<p><strong>First question is how can I get the contour (array) of this grab cut?</strong></p>

<p><a href="https://i.stack.imgur.com/WKjAC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WKjAC.png" alt="enter image description here"></a>
Second question: as you can see, the background color is black. <strong>How can I change the background color to white?</strong></p>

<p>Thank you.</p>