title: Why does OpenCV recognize the object only in training images?
body: <p>In order to make my iOS app recognize 1€, 2€ and 0.50€ coins I have been trying to use <code>opencv_createsamples</code> and <code>opencv_traincascade</code> to create my own classifier.xml. So, I cropped 60 images of 2€ coins from a short video like the following:</p>
  
  <p><a href="https://i.stack.imgur.com/K7tux.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K7tux.png" alt="enter image description here"></a></p>
  
  <p>Then, I combined them with random backgrounds using <code>opencv_createsamples</code>. I obtained 12000 images similar to this:</p>
  
  <p><a href="https://i.stack.imgur.com/TdrnQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TdrnQ.png" alt="enter image description here"></a></p>
  
  <p>and I ran the following commands:</p>
  
  <p><code>opencv_createsamples -img positives/i.jpg -bg negatives.txt -info i.txt -num 210 -maxidev 100 -maxxangle 0.0 -maxyangle 0.0 -maxzangle 0.9 -bgcolor 0 -bgthresh 0 -w 48 -h 48</code> (for i from 0 to 60)</p>
  
  <p><code>cat *.txt &gt; positives.txt</code></p>
  
  <p><code>opencv_createsamples -info positives.txt -bg negatives.txt -vec 2.vec -num 12600 -w 48 -h 48</code></p>
  
  <p><code>opencv_traincascade -data final -vec 2.vec -bg negatives.txt -numPos 12000 -numNeg 3000 -numStages 20 -featureType LBP -precalcValBufSize 2048 -precalcIdxBufSize 2048 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -w 48 -h 48</code></p>
  
  <p>Training stopped at 13-th stage. Once I got a <code>cascade.xml</code> I tried it at once (with <code>detectMultiScale()</code>) on a simple image taken by my smartphone but nothing is detected:</p>
  
  <p><a href="https://i.stack.imgur.com/kUt13.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kUt13.jpg" alt="enter image description here"></a></p>
  
  <p>while if I give as input one of the images used as traning, then it works very good:</p>
  
  <p><a href="https://i.stack.imgur.com/DoFY1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DoFY1.png" alt="enter image description here"></a></p>
  
  <p>I can't really understand why this is happening and it's driving me insane, most of all because I have been trying to make it work for  weeks...would you please tell me where I am making the mistake?</p>
  
  <p>The short program I wrote is here:</p>
  
  <pre><code>#include "opencv2/opencv.hpp"
  
  using namespace cv;
  
  int main(int, char**) {
  
  Mat src = imread("2b.jpg");
  
  Mat src_gray;
  
  std::vector&lt;cv::Rect&gt; money;
  
  CascadeClassifier euro2_cascade;
  
  cvtColor(src, src_gray, CV_BGR2GRAY );
  equalizeHist(src_gray, src_gray);
  
  if ( !euro2_cascade.load( "cascade.xml" ) ) {
      printf("--(!)Error loading\n");
      return -1;
  }
  
  euro2_cascade.detectMultiScale( src_gray, money, 1.1, 3, 0|CASCADE_SCALE_IMAGE/*CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE*/, cv::Size(10, 10),cv::Size(2000, 2000) );
  printf("%d\n", int(money.size()));
  
  for( size_t i = 0; i &lt; money.size(); i++ ) {
      cv::Point center( money[i].x + money[i].width*0.5, money[i].y + money[i].height*0.5 );
      ellipse( src, center, cv::Size( money[i].width*0.5, money[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
  }
  
  namedWindow( "Display window", WINDOW_AUTOSIZE );
  imwrite("result.jpg",src);
  }
  </code></pre>
  
  <p>I have also tried to take into consideration <a href="http://docs.opencv.org/3.0-beta/doc/tutorials/features2d/feature_homography/feature_homography.html" rel="nofollow noreferrer">feature homography</a> for my OpenCV version 3.1. Thus, I downloaded opencv_contrib for my version and tried to compile xfeatures2d module but I get a CMake error in CMakeList file...moreover xfeature2d is not even present inside opencv framework for xcode so even if I made it work on my pc then I could not use it in iOS anyway...</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As advised, I've just tried to remove <code>equalizeHist(src_gray, src_gray);</code> and I have set the number of neighbors in detectMultiScale()`` and this is what I get:</p>
  
  <p><a href="https://i.stack.imgur.com/6Ku3h.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6Ku3h.jpg" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>As someone suggested, following <a href="http://answers.opencv.org/question/15723/unable-to-create-positive-samples/" rel="nofollow noreferrer">this</a> tutorial I have just created a .vec file using only the cropped positive images, the ones with only the coin. I used this command:</p>
  
  <p><code>opencv_createsamples -vec i.vec -w 48 -h 48 -num 210 -img ./positives/i.jpg -maxidev 100 -maxxangle 0 -maxyangle 0 -maxzangle 0.9 -bgcolor 0 -bgthresh 0</code>
  (for i from 0 to 60)</p>
  
  <p>So as you can see, there's no used background image to create samples. 
  Then, after downloading <a href="https://github.com/wulfebw/mergevec/blob/master/mergevec.py" rel="nofollow noreferrer">mergevec.py</a>, I combined all the vectors file together. Now I'm gonna launch another LBP training...I hope it'll work better</p>