title: openCV KNN findNearest Error
body: <p>I tried to implement the KNN findNearest function. My program shall recognize numbers in a picture, but I get an error if the program uses findNearest. Here's the code:</p>
  
  <pre><code>`private void searchingData() {
  
      img_gray = new Mat();
      img_blur = new Mat();
      img_thres = new Mat();
  
      Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_BGR2GRAY);
      Imgproc.GaussianBlur(img_gray, img_blur, new Size(5,5), 0);
      //Imgproc.adaptiveThreshold(img_blur, img_thres, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 7, 5);
      Imgproc.Canny(img_blur, img_thres, 10, 100); 
  
      Imgproc.findContours(img_thres, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
  
      for(int i=0; i&lt; contours.size();i++){
  
          Rect rect = Imgproc.boundingRect(contours.get(i));
  
          if (rect.height &lt; 50 &amp;&amp; rect.height &gt; 20){ 
  
              System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width);
  
              Mat subImg = new Mat();
              Imgproc.resize(img.submat(rect), subImg, new Size(10,10));
  
              //Found numbers then try to recognize it
              recognize(subImg);
          }
      }
  }
  
  public void learn() { //get the training data and train the KNN
  
      Mat sample, training_img = new Mat(), res = new Mat();
  
      for (int i = 1; i &lt; 10; i++) {
  
          String path = String.format(".../Documents/numbers/%03d.png", i);
          sample = Imgcodecs.imread(path, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
  
          Mat m = new Mat(new Size(1,1), CvType.CV_32FC1);
          m.setTo(new Scalar(i));
          res.push_back(m);
  
          training_img.push_back(prepareImg(sample));
      }
  
      model = KNearest.create();
      model.train(training_img, Ml.ROW_SAMPLE, res);
  
  }
  
  private Mat prepareImg(Mat img) { //change image in the right format
      Mat sample = new Mat(), sized = new Mat();
  
      Imgproc.resize(img, sized, new Size(10,10));
      sized.reshape(1,1).convertTo(sample, CvType.CV_32FC1);
  
      return sample;
  }
  
  public void recognize(Mat getImg) {
  
      Mat results = new Mat();
      Mat dists   = new Mat();
  
      float result = model.findNearest(prepareImg(getImg), 2, results, new Mat(), dists);
  
      //result should be the number in getImg
      System.out.println(result);
  }`
  </code></pre>
  
  <blockquote>
    <p>OpenCV Error: Assertion failed (test_samples.type() == CV_32F &amp;&amp;
    test_samples.cols == samples.cols) in
    cv::ml::BruteForceImpl::findNearest, file
    C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\ml\src\knearest.cpp,
    line 325 Exception in thread "main" CvException
    [org.opencv.core.CvException: cv::Exception:
    C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\ml\src\knearest.cpp:325:
    error: (-215) test_samples.type() == CV_32F &amp;&amp; test_samples.cols ==
    samples.cols in function cv::ml::BruteForceImpl::findNearest ]</p>
  </blockquote>
  
  <p>Thanks a lot!</p>