Digit recognition : find_nearest() sample argument

asked 2015-02-03 07:35:36 -0500

Alexandre G. gravatar image

updated 2015-02-04 10:16:01 -0500

I'm doing digit recognition on Android. I've got the following error thrown when I try to use the find_nearest() function :

OpenCV Error: Bad argument (Input samples must be floating-point matrix (<num_samples>x<var_count>))

I looked up the definition of this function and the error comes from there (knearest.cpp line 322) :

if( !CV_IS_MAT(_samples) ||
    CV_MAT_TYPE(_samples->type) != CV_32FC1 ||
    _samples->cols != var_count )
    CV_Error( CV_StsBadArg, "Input samples must be floating-point matrix (<num_samples>x<var_count>)" );

The type of my sample Mat is already CV_32FC1, so we're down to _samples->cols != var_count.

My inputs for find_nearest() look like this :

Mat results = new Mat();
Mat responses = new Mat();
Mat dists = new Mat();
Mat sample = new Mat();

sample.convertTo(sample, CvType.CV_32FC1);

sample = sample.reshape(1,sample.cols()*sample.rows());

float numberDetected = knn.find_nearest(sample, 50, results, responses, dists);

I added the reshape according to this post on SO, without any luck.

My question is : what is var_count ? And how should I make my Mat cols equal to it ? Thank you!

edit retag flag offensive close merge delete



have you seen this? So what are your inputs of find_nearest()?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-03 08:47:08 -0500 )edit

Thanks for the link. I still don't understand what "used features" stands for ?

As for the inputs, I added them in my post.

Alexandre G. gravatar imageAlexandre G. ( 2015-02-03 09:43:39 -0500 )edit

your sample vec does not seem to have the same length as the features passed in the training

berak gravatar imageberak ( 2015-02-04 13:29:14 -0500 )edit

indeed, I had no error with reshape(1,1). If you make an answer I'll accept it.

Alexandre G. gravatar imageAlexandre G. ( 2015-02-05 03:38:49 -0500 )edit