0

I'm developping an application on Qt with OpenCV and I have the following error that I did not have previously and I have the following message:

OpenCV Error: Bad argument (Input samples must be floating-point matrix (x)) in find_nearest, file C:\Users\Pierre\Downloads\opencv\sources\modules\ml\src\knearest.cpp, line 370 terminate called after throwing an instance of 'cv::Exception' what(): C:\Users\Pierre\Downloads\opencv\sources\modules\ml\src\knearest.cpp:370: error: (-5) Input samples must be floating-point matrix (x) in function find_nearest

and also a runtime error such as in Runtime error for QT application? Could you please help me because I'm quite new at Qt? Thanks!

Here is a piece of code:

void MainWindow::on_pushButton_6_clicked()
{
  QList<QLabel*> resLab;
  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "C:/Users/Pierre/Downloads/test1/image.orig", tr("ImageFiles (*.png *.jpeg *.jpg *.bmp)"));
  QPixmap image(fileName);
  ui->label->setPixmap(image.scaled(130, 130, Qt::IgnoreAspectRatio, Qt::FastTransformation));
  Mat image1=QPixmapToCvMat(image,false);
  // un vecteur avec des qlabels pour afficher les images similaires
  /*
    resLab << new QLabel(ui->scrollAreaWidgetContents); pour ajouter un qlabel au vecteur
    resLab.at(i)->setGeometry(330 + (i - round(i / 5) * 5)*(20 + 250), 10 + round(i / 5)*(250 + 10), 250,
    250); pour indiquer la position et les dimensions
    resLab.at(i)->setVisible(true); pour le rendre visible
    resLab.at(i)->setStyleSheet("QLabel { background-color : rgba(85, 85, 127, 100); }"); pour modifier
    le style
    */
  const int nbrDeVoisins = 20;
  cv::KNearest *knnSurf, *knnSift, *knnHist, *knnHu;
  // nos objets kNN qui seront utilisés pour la recherche (Dans OpenCV, la classe cv::KNearest offre une implémentation d’une recherche par similitude basé sur la méthode KNN : les k plus proches voisins)
  Mat featuresSift, featuresSurf, featuresHist, featuresHu;// les caractéristiques
  QStringList listSift, listSurf, listHu, listHist;// les listes avec les noms des images
  const QString dirBdd = "C:/Users/Pierre/Downloads/test1/image.orig";

  cv::FileStorage fs1("surf.yml", FileStorage::READ);// chargement des caractéristiques à partir du yml
  fs1["Features"] >> featuresSurf;
  QStringList list;// chargement d’un fichier text
  QFile fichierTxt("surf.txt");
  fichierTxt.open(QIODevice::ReadOnly);
  while (!fichierTxt.atEnd()) {
    QString uneLigne = fichierTxt.readLine();
    if (uneLigne.endsWith("\n")) uneLigne.truncate(uneLigne.length() - 1);
    list.append(uneLigne);
  }

  cv::Mat trainingClasses(featuresSurf.rows, 1, CV_32FC1); // les voisins retournés par kNN
  for (int i = 0; i<featuresSurf.rows; i++) {// correspondent aux id donnés par cette matrice
    trainingClasses.at<float>(i, 0) = i;
  }
  // initialisation de notre objet kNN

  knnSurf = new cv::KNearest(featuresSurf, trainingClasses, cv::Mat(), false, nbrDeVoisins);

  Mat features = surf(image1); //.reshape(1, 30 * 128).t();

  cv::Mat distances, voisins, resultats;
  // distances: la matrice donne la distance entre chaque voisin et la requête
  // voisins: la matrice avec les IDs des voisins
  // resultats: cette matrice ne sera pas utilisée dans ce TP, elle est utilisée pour faire de la    classification ou de la régression
  knnSurf->find_nearest(features, nbrDeVoisins, resultats, voisins, distances);
  cout<<"here";

  // trouver les voisins les plus proches
  for (int k=0; k<nbrDeVoisins; k++) {
    cout << voisins.at<float>(0, k) << endl;
  }
}

It doesn't print "here" (but prints it if I put it just above)

Community
  • 1
  • 1
  • Are you sure about your opencv code ? Did you try to isolate it from Qt ? – Tryum Mar 27 '17 at 08:19
  • @Tryum on the PC of my collegue, it seems to work well –  Mar 27 '17 at 08:20
  • The exact same code (without Qt) is running ? The runtime error is probably due to an assert/exit/uncactched exception being called from opencv. – Tryum Mar 27 '17 at 08:25
  • @Tryum Could you a bit more develop please ? My collegue works on Qt too and does not have any error actually. I added a piece of code if you 'd like to see it. Thanks! –  Mar 27 '17 at 08:28
  • 1
    OpenCV asserts that the train data you're giving it are note what it expects. I then quits the program, thus giving the runtime error. Are you sure featuresSurf and tranningClasses contains the data type that cv::KNearest expects ? – Tryum Mar 27 '17 at 08:30
  • The problem has nothing to do with Qt. The error clearly says that the input data is in wrong format. Both `featuresSurf` and `trainingClasses` needs to be correct initialized. – bkausbk Mar 27 '17 at 08:46

0 Answers0