title: OpenCV Python Bindings for GrabCut Algorithm
body: <p>I've been trying to use the OpenCV implementation of the grab cut method  via the Python bindings. I have tried using the version in both cv and cv2 but I am having trouble finding out the correct parameters to use to get the method to run correctly. I have tried several permutations of the parameters and nothing seems to work (basically every example I've seen on Github). Here are a couple examples I have tried to follow:</p>
  
  <p><a href="https://github.com/Tug/nyp/blob/bff6aba87aa705c971e752aef02c1f982de623aa/fis/algo/segmentation/labelize.py" rel="noreferrer">Example 1</a></p>
  
  <p><a href="https://github.com/pathammer/Kinect-Mouse/blob/fa760b2ff8fc535eff90c0388f25c441afd60344/Mouse-ntk/nestk/deps/opencv/tests/python/test.py#L630" rel="noreferrer">Example 2</a></p>
  
  <p>And here is the method's documentation and a known bug report:</p>
  
  <p><a href="http://opencv.itseez.com/modules/imgproc/doc/miscellaneous_transformations.html?highlight=grabcut#cv2.grabCut" rel="noreferrer">Documentation</a></p>
  
  <p><a href="https://code.ros.org/trac/opencv/ticket/1046" rel="noreferrer">Known Grabcut Bug</a></p>
  
  <p>I can get the code to execute using the example below, but it returns a blank (all black) image mask. </p>
  
  <pre><code>img = Image("pills.png")
  mask = img.getEmpty(1)
  bgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
  fgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
  for i in range(0, 13*5):
      cv.SetReal2D(fgModel, 0, i, 0)
      cv.SetReal2D(bgModel, 0, i, 0)
  
  rect = (150,70,170,220)
  tmp1 = np.zeros((1, 13 * 5))
  tmp2 = np.zeros((1, 13 * 5))
  cv.GrabCut(img.getBitmap(),mask,rect,tmp1,tmp2,5,cv.GC_INIT_WITH_RECT)
  </code></pre>
  
  <p>I am using SimpleCV to load the images. The mask type and return type from img.getBitmap() are:</p>
  
  <pre><code>iplimage(nChannels=1 width=730 height=530 widthStep=732 )
  iplimage(nChannels=3 width=730 height=530 widthStep=2192 )
  </code></pre>
  
  <p>If someone has a working example of this code I would love to see it. For what it is worth I am running on OSX Snow Leopard, and my version of OpenCV was installed from the SVN repository (as of a few weeks ago). For reference my input image is this:
  <img src="https://i.stack.imgur.com/r50lO.png" alt="Input Image"></p>
  
  <p>I've tried changing the result mask enum values to something more visible. It is not the return values that are the problem. This returns a completely black image. I will try a couple more values. </p>
  
  <pre><code>img = Image("pills.png")
  mask = img.getEmpty(1)
  bgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
  fgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
  for i in range(0, 13*5):
      cv.SetReal2D(fgModel, 0, i, 0)
      cv.SetReal2D(bgModel, 0, i, 0)
  
  rect = (150,70,170,220)
  tmp1 = np.zeros((1, 13 * 5))
  tmp2 = np.zeros((1, 13 * 5))
  cv.GrabCut(img.getBitmap(), mask, rect, tmp1, tmp2, 5, cv.GC_INIT_WITH_MASK)
  mask[mask == cv.GC_BGD] = 0
  mask[mask == cv.GC_PR_BGD] = 0
  mask[mask == cv.GC_FGD] = 255
  mask[mask == cv.GC_PR_FGD] = 255
  result = Image(mask)
  result.show()
  result.save("result.png")
  </code></pre>