title: OpenCV - find object shape
body: <p>I'm learning OpenCV and I've reached a point where no matter what I do, I get stucked.
  What I'm trying to do is to isolate an object (rectangular object) from its background. </p>
  
  <p>An example is the following picture of a battery:
  <a href="https://i.stack.imgur.com/cu4tQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cu4tQ.jpg" alt="battery-picture"></a></p>
  
  <p>I want to mask that image so that the only thing that remains is the object.</p>
  
  <p>I've tried the following:</p>
  
  <ul>
  <li>Thresholding</li>
  <li>Detect edges using Canny</li>
  <li>Find Contours</li>
  <li>Get the bigger one</li>
  </ul>
  
  <p>But I'm getting some strange area as the bigger one. Here are the resulting pictures:</p>
  
  <ul>
  <li><p>Canny
  <a href="https://i.stack.imgur.com/KKZdA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KKZdA.jpg" alt="canny edges"></a></p></li>
  <li><p>Largest Contour
  <a href="https://i.stack.imgur.com/ZGcjc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZGcjc.jpg" alt="enter image description here"></a></p></li>
  </ul>
  
  <p>Here's the code I'm using:</p>
  
  <pre><code>#include "opencv2/imgproc/imgproc.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  using namespace cv;
  using namespace std;
  
  
  int main( int, char** argv )
  {
  Mat src, srcGray,srcBlur,srcCanny;
  
  string file = "samsung";
  src = imread(file + ".jpg");
  cvtColor(src, srcGray, CV_BGR2GRAY);
  //bilateralFilter(srcGray, srcBlur,11, 17, 17);
  srcBlur = srcGray.clone();
  imshow("Filtered", srcBlur);
  imwrite(file+"-filtered.jpg",srcBlur);
  
  Canny(srcBlur, srcCanny, 0, 100, 3, true);
  imshow("Canny", srcCanny);
  imwrite(file+"-canny.jpg",srcCanny);
  
  
  vector&lt; vector &lt;Point&gt; &gt; contours; // Vector for storing contour
  vector&lt; Vec4i &gt; hierarchy;
  findContours( srcCanny.clone(), contours, hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
  
  
  int largest_contour_index=0;
  int largest_area=0;
  for( int i = 0; i&lt; contours.size(); i++ ){
      double a=contourArea( contours[i],false);  //  Find the area of contour
      if(a&gt;largest_area){
      largest_area=a;
      largest_contour_index=i;                //Store the index of largest contour
      }
   }
  
  
  Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
  drawContours( dst,contours, largest_contour_index, Scalar(255,0,0),CV_FILLED, 8, hierarchy );
  imshow("Largest", dst);
  imwrite(file+"-largest.jpg",dst);
  
  waitKey();
  }
  </code></pre>
  
  <p>This piece of code was intended to get the 'mask' of the object then masking should be applied but I can't move forward because I can't detect the object</p>
  
  <p>My goal is to detect rectangular objects (only one object per image) in different images.</p>
  
  <p>The idea was taken from <a href="https://stackoverflow.com/questions/22519545/automatic-perspective-correction-opencv">here</a>, but I can't manage to get that code to work with a lesser contrast image like mine.</p>
  
  <p>I've also try <a href="http://www.pyimagesearch.com/2014/05/05/building-pokedex-python-opencv-perspective-warping-step-5-6/" rel="nofollow noreferrer">this</a> wich is pretty much the same as I want. </p>
  
  <p>I want to isolate a rectangular object (wich should be the bigger one in the image)</p>
  
  <p>Thanks in advance!</p>
  
  <p>PS: Although I can translate Python to C++ I would appreciate the answer directly in C++ so I can test it faster.</p>