title: comparing two contours using matchshapes
body: <p>I am trying to compare two contours using matchshapes() function in opencv.<br>
  These are the two images I am trying to compare.<br>
  <img src="https://i.stack.imgur.com/QWSEm.jpg" alt="http://s4.postimg.org/aiahof9v1/image.jpg"> <img src="https://i.stack.imgur.com/pGUdM.jpg" alt="http://s11.postimg.org/za2euph43/image.jpg">  The problem is matchshapes() showing that these two images are nearly matching i.e it is returning a very small value.  I don't know whether we can use matchshapes() to compare different contour shapes.<br>
  Can you suggest some way to compare different contour shapes of hand?<br>
  Here is my code..</p>
  
  <pre><code>#include &lt;opencv2\opencv.hpp&gt;
  #include &lt;string&gt;
  
  using namespace cv;
  using std::cout;
  Mat1b converttogray(Mat3b testcases);
  int findmaxcontour(vector&lt;vector&lt;Point&gt;&gt; contours);
  RNG rng(12345);
  vector&lt;vector&lt;Point&gt;&gt; getcontours(Mat1b image_gray, int i);
  
  /*--------------- SKIN SEGMENTATION ---------------*/
  int main() 
  {
      Mat1b image_gray;
      int max_contour;
      vector&lt;vector&lt;Point&gt; &gt; contours;
      vector&lt;vector&lt;Point&gt; &gt;  contours2;
      Mat3b first;
      Mat3b frame;
      Mat1b frame_gray;
      String one = "E:/photo/" + std::to_string((0 + 5));
      String a = "a.png";
      String b = "b.png";
      String c = "c.png";
      cout &lt;&lt; one + a &lt;&lt; "\n";
      cout &lt;&lt; one + b &lt;&lt; "\n";
      cout &lt;&lt; one + c &lt;&lt; "\n";
      first = imread(one + c, CV_LOAD_IMAGE_COLOR);
  
      image_gray = converttogray(first);
  
      contours = getcontours(image_gray, 1);
  
      max_contour = findmaxcontour(contours);
  
      frame = imread("E:/photo/1.png", CV_LOAD_IMAGE_COLOR);
  
      frame_gray = converttogray(frame);
  
      contours2 = getcontours(frame_gray, 2);
  
      int x = findmaxcontour(contours2);
  
      cout &lt;&lt; "\n" &lt;&lt; matchShapes(contours[max_contour], contours2[x], CV_CONTOURS_MATCH_I1, 0.0);
  
      waitKey(0);
  }
  
  Mat1b converttogray(Mat3b testcases)
  {
      Mat1b image_gray;
      cvtColor(testcases, testcases, CV_BGR2HSV);
  
      GaussianBlur(testcases, testcases, Size(7, 7), 1, 1);
      medianBlur(testcases, testcases, 15);
      for (int r = 0; r&lt;testcases.rows; ++r)
      {
          for (int c = 0; c&lt;testcases.cols; ++c)
          // 0&lt;H&lt;0.25  -   0.15&lt;S&lt;0.9    -    0.2&lt;V&lt;0.95   
          if ((testcases(r, c)[0]&gt;5) &amp;&amp; (testcases(r, c)[0] &lt; 17) &amp;&amp; (testcases(r, c)[1]&gt;38) &amp;&amp; (testcases(r, c)[1]&lt;250) &amp;&amp; (testcases(r, c)[2]&gt;51) &amp;&amp; (testcases(r, c)[2]&lt;242)); // do nothing
          else for (int i = 0; i&lt;3; ++i)  testcases(r, c)[i] = 0;
      }
  
      cvtColor(testcases, testcases, CV_HSV2BGR);
      cvtColor(testcases, image_gray, CV_BGR2GRAY);
      threshold(image_gray, image_gray, 30, 255, CV_THRESH_BINARY);
      morphologyEx(image_gray, image_gray, CV_MOP_ERODE, Mat1b(3, 3, 1), Point(-1, -1), 3);
      morphologyEx(image_gray, image_gray, CV_MOP_OPEN, Mat1b(7, 7, 1), Point(-1, -1), 1);
      morphologyEx(image_gray, image_gray, CV_MOP_CLOSE, Mat1b(9, 9, 1), Point(-1, -1), 1);
  
      medianBlur(image_gray, image_gray, 15);
      return image_gray;
  }
  
  int findmaxcontour(vector&lt;vector&lt;Point&gt;&gt; contours)
  {
      int largest_area = 0;
      int largest_contour_index = 0;
  
      for (int i = 0; i&lt; contours.size(); i++) // iterate through each contour. 
      {
          double a = contourArea(contours[i], false);  //  Find the area of contour
          cout &lt;&lt; a &lt;&lt; "\n";
          if (a&gt;largest_area)
          {
              largest_area = a;
              largest_contour_index = i;                //Store the index of largest contour
          }
      }
  
      return largest_contour_index;
  }
  
  vector&lt;vector&lt;Point&gt;&gt; getcontours(Mat1b image_gray, int i)
  {
      Mat canny_output;
      vector&lt;vector&lt;Point&gt;&gt; contours;
      vector&lt;Vec4i&gt; hierarchy;
      Canny(image_gray, canny_output, 50, 150, 3);
  
      int x=0;
  
      findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
      x = findmaxcontour(contours);
  
      Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
      for (int i = 0; i&lt; contours.size(); i++)
      {
          Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
          drawContours(drawing, contours, x, color, 2, 8, hierarchy, 0, Point());
      }
  
      /// Show in a window
      namedWindow(std::to_string(i), CV_WINDOW_AUTOSIZE);
      imshow(std::to_string(i), drawing);
      return contours;
  }
  </code></pre>