title: Using a PNG image as mask for GrabCut
body: <p>I have a png image with green and red lines and transparent background, which I need to use is as a mask for executing GrabCut. But I get unexpected results. Here's my code:</p>
  
  <pre><code>//find the mask
  Mat mask;
  mask.create( image.size(), CV_8UC1);
  mask.setTo(Scalar::all(GC_BGD));
  Mat maskImg = imread("messi5.png");
  
  for(int i=0; i&lt;maskImg.cols; i++)    
      for(int j=0; j&lt;maskImg.rows; j++)    
      {               
          //if it's red, make it white
          if ((int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[0]==0 &amp;&amp; (int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[1] == 0 &amp;&amp; (int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[2] == 255) {
  
              mask.at&lt;cv::Vec3b&gt;(j,i)[0]= GC_BGD;
              mask.at&lt;cv::Vec3b&gt;(j,i)[1] = GC_BGD;
              mask.at&lt;cv::Vec3b&gt;(j,i)[2] = GC_BGD;
          }           
  
          //if it's green, make it black
          if ((int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[0]==0 &amp;&amp; (int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[1] == 255 &amp;&amp; (int)maskImg.at&lt;cv::Vec3b&gt;(j,i)[2] == 0) {    
              mask.at&lt;cv::Vec3b&gt;(j,i)[0] = GC_FGD;
              mask.at&lt;cv::Vec3b&gt;(j,i)[1] = GC_FGD;
              mask.at&lt;cv::Vec3b&gt;(j,i)[2] = GC_FGD;
          }
      }
  
   ...
  </code></pre>
  
  <p>Here's the output: <a href="http://prntscr.com/40kt4e" rel="nofollow">http://prntscr.com/40kt4e</a>. I guess it's like there's no rectangle, it only sees the GC_FGD pixels, everything else is considered BG. And it looks somehow scaled, but I have no idea how to fix it.</p>