Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Knn implementation in java error

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 = "path to image /4.jpg");
    private static Mat trainingData = new Mat();
    private static Mat trainingLabels = new Mat();
    private static Mat trainingData1 = new Mat();
    private static Mat trainingLabels1 = new Mat();
    private static ANN_MLP ann = ANN_MLP.create();

    void run() throws IOException {

        ////// 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));
            img.convertTo(img, CvType.CV_32FC1); 
            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(25, 25));
        test.convertTo(test, CvType.CV_32FC1);
        Mat results = new Mat();
        Mat responses = new Mat();
        Mat dists = new Mat();
        float label = knn.findNearest(test.reshape(1, 1),1, results,responses, dists);
        System.out.println(label);

    }

}

Knn implementation in java error

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 = "path to image /4.jpg");
    private static Mat trainingData = new Mat();
    private static Mat trainingLabels = new Mat();
    private static Mat trainingData1 = new Mat();
    private static Mat trainingLabels1 = new Mat();
    private static ANN_MLP ann = ANN_MLP.create();

    void run() throws IOException {

        ////// 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));
            img.convertTo(img, CvType.CV_32FC1); 
            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(25, 25));
Size(74, 60));
        test.convertTo(test, CvType.CV_32FC1);
        Mat results = new Mat();
        Mat responses = new Mat();
        Mat dists = new Mat();
        float label = knn.findNearest(test.reshape(1, 1),1, results,responses, dists);
        System.out.println(label);

    }

}

Knn implementation in java error

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 = "path to image /4.jpg");
    private static Mat trainingData = new Mat();
    private static Mat trainingLabels = new Mat();
    private static Mat trainingData1 = new Mat();
    private static Mat trainingLabels1 = new Mat();
    private static ANN_MLP ann = ANN_MLP.create();
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));
            img.convertTo(img, CvType.CV_32FC1); 
            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));
        test.convertTo(test, CvType.CV_32FC1);
        Mat results = new Mat();
        Mat responses = new Mat();
        Mat dists = new Mat();
        float label = knn.findNearest(test.reshape(1, 1),1, results,responses, dists);
        System.out.println(label);

    }

}

Knn implementation in java error

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();
        Mat responses = new Mat();
        Mat dists = new Mat();
        float label = knn.findNearest(test.reshape(1, 1),1, results,responses, dists);
results);
        System.out.println(label);

    }

}

Knn implementation in java error

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);

    }

}