Knn implementation in java error

asked 2017-05-28 08:30:22 -0600

Tomna gravatar image

updated 2017-05-28 10:07:20 -0600

i am trying to implement the KNN and an assertion error happened no idea how to fix it.

here is my error

OpenCV Error: Assertion failed (test_samples.type() == CV_32F && test_samples.cols == samples.cols) in findNearest, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/knearest.cpp, line 325 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/knearest.cpp:325: error: (-215) test_samples.type() == CV_32F && test_samples.cols == samples.cols in function findNearest ]

public class Recognition {

    protected static final String PATH_numbers = "path to file";
    protected static final Mat img = Imgcodecs.imread("/path/4.jpg");


    void run() throws IOException {
                           Mat trainingData = new Mat();
                            Mat trainingLabels = new Mat();

        ////// here the training images which a 20 images were 2 of each charachter 0,0,1,1,2,2..9,9
        for (File file : new File(PATH_numbers).listFiles()) {
            Mat img = getMat(file.getAbsolutePath());


            img.convertTo(img, CvType.CV_32FC1); // Convert to float
            Imgproc.resize(img, img, new Size(74, 60));
            Mat imgresized = img.reshape(1, 1); // make continous

            trainingData.push_back(imgresized);
            trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(1)));

        }

        Mat response = new Mat();
        Mat tmp;
        tmp = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32FC1); // Convert to float
        KNearest knn = KNearest.create();
        knn.train(trainingData, Ml.ROW_SAMPLE, trainingLabels);



        Mat test = new Mat();
        Imgproc.resize(img, test, new Size(74, 60));
        Mat results = new Mat();
        float label = knn.findNearest(test.reshape(1, 1),1, results);
        System.out.println(label);

    }

}
edit retag flag offensive close merge delete

Comments

@berak need your skills :) any ideas?

Tomna gravatar imageTomna ( 2017-05-28 08:32:12 -0600 )edit

hehe, wtf is this: final Mat img = "path to image /4.jpg"); <-- this won't even compile, but i guess, there s no image in it. (i bet a beer, your test image is empty)

also you "shadow" your Mat variable with a local one.

remove all those global static vars, bad idea in general ! you also can't run opencv code, before the native libs are loaded, so you cannot imread() in a static context !

then, clean it up, e.g. you don't need to convert something to float 3 times in a row, you never use your "response" Mat, etc. it all will get much easier with less cruft !

berak gravatar imageberak ( 2017-05-28 08:46:06 -0600 )edit

@berak i edited however not gonna fix the error. and for the image i just coppied the wrong line of code sorry

Tomna gravatar imageTomna ( 2017-05-28 09:01:56 -0600 )edit