Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png");
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png");
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png");
Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png", 0);
Imgproc.resize(one_feature, one_feature, new Size(20, 20));
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

Edit: import as grayscale and resize image added to one_feature in an attempt to solve the error.

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png", 0);
Imgproc.resize(one_feature, one_feature, new Size(20, 20));
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

Edit: import as grayscale and resize image added to one_feature in an attempt to solve the error.

Edit 2: Here's the training/testing code that I'm using

// samples/data/digits.png, have a look at it.
Mat digits = Imgcodecs.imread("/Users/jeremyk/Desktop/digits.png", 0);

// setup train/test data:
Mat trainData = new Mat(),
        testData = new Mat();
List<Integer> trainLabs = new ArrayList<Integer>(),
        testLabs = new ArrayList<Integer>();
// 10 digits a 5 rows:
for (int r=0; r<50; r++) {
    // 100 digits per row:
    for (int c=0; c<100; c++) {
        // crop out 1 digit:
        Mat num = digits.submat(new Rect(c*20,r*20,20,20));
        // we need float data for knn:
        num.convertTo(num, CvType.CV_32F);
        // 50/50 train/test split:
        if (c % 2 == 0) {
            // for opencv ml, each feature has to be a single row:
            trainData.push_back(num.reshape(1,1));
            // add a label for that feature (the digit number):
            trainLabs.add(r/5);
        } else {
            testData.push_back(num.reshape(1,1));
            testLabs.add(r/5);
        }
    }
}

// make a Mat of the train labels, and train knn:
KNearest knn = KNearest.create();
knn.train(trainData, Ml.ROW_SAMPLE, Converters.vector_int_to_Mat(trainLabs));
// now test predictions:
for (int j=0; j<testData.rows(); j++)
{
    Mat one_feature = testData.row(j);
    int testLabel = testLabs.get(j);

    Mat res = new Mat();
    float p = knn.findNearest(one_feature, 1, res);
    System.out.println(testLabel + " " + p + " " + res.dump());
}

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png", 0);
Imgproc.resize(one_feature, one_feature, new Size(20, 20));
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

Edit: import as grayscale and resize image added to one_feature in an attempt to solve the error.

Edit 2: Here's the training/testing code that I'm using

// samples/data/digits.png, have a look at it.
Mat digits = Imgcodecs.imread("/Users/jeremyk/Desktop/digits.png", 0);

// setup train/test data:
Mat trainData = new Mat(),
        testData = new Mat();
List<Integer> trainLabs = new ArrayList<Integer>(),
        testLabs = new ArrayList<Integer>();
// 10 digits a 5 rows:

Edit 3: Here's the updated training/test code that I'm using

for (int r=0; r<50; r++) { // 100 digits per row: for (int c=0; c<100; c = 0; c< numberOfTrainingDigits; c++) { // crop out 1 digit: digit = Imgcodecs.imread("/Users/jeremyk/Desktop/training/digit" + c + ".png"); Mat num = digits.submat(new Rect(c*20,r*20,20,20)); new Mat(); Imgproc.resize(digit, num, new Size(60, 110)); // we need float data for knn: num.convertTo(num, CvType.CV_32F); // 50/50 train/test split: if (c % 2 == 0) { // for opencv ml, each feature has to be a single row: trainData.push_back(num.reshape(1,1)); // add a trainLabs.add(c); }

// Add test digit
Mat testDigit = Imgcodecs.imread("/Users/jeremyk/Desktop/testdigit.png", 0);
Mat num = testDigit.submat(new Rect(0,0,60,110));
num.convertTo(num, CvType.CV_32F);
testData.push_back(num.reshape(1,1));
// Add label for that feature (the digit number):
            trainLabs.add(r/5);
        } else {
            testData.push_back(num.reshape(1,1));
            testLabs.add(r/5);
        }
    }
}
test digit
testLabs.add(6);

// make a Mat of the train labels, and train knn:
KNearest knn = KNearest.create();
knn.train(trainData, Ml.ROW_SAMPLE, Converters.vector_int_to_Mat(trainLabs));
 // now test predictions:
for (int j=0; j<testData.rows(); j++)
{
    Mat one_feature = testData.row(j);
    int testLabel = testLabs.get(j);

    Mat res = new Mat();
    float p = knn.findNearest(one_feature, 1, res);
    System.out.println(testLabel + " " + p + " " + res.dump());
}

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png", 0);
Imgproc.resize(one_feature, one_feature, new Size(20, 20));
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

Edit: import as grayscale and resize image added to one_feature in an attempt to solve the error.

Edit 2: Here's the training/testing code that I'm using

Edit 3: Here's the updated training/test code that I'm using

 for (int c = 0; c< numberOfTrainingDigits; c++) {
        // crop out 1 digit:
        digit = Imgcodecs.imread("/Users/jeremyk/Desktop/training/digit" + c + ".png");
        Mat num = new Mat();
        Imgproc.resize(digit, num, new Size(60, 110));
        // we need float data for knn:
        num.convertTo(num, CvType.CV_32F);
        // for opencv ml, each feature has to be a single row:
        trainData.push_back(num.reshape(1,1));
        trainLabs.add(c);
    }

}

    // Add test digit
image
    Mat testDigit = Imgcodecs.imread("/Users/jeremyk/Desktop/testdigit.png", 0);
 Mat num = testDigit.submat(new Rect(0,0,60,110));
new Mat();
    Imgproc.resize(testDigit, num, new Size(60, 110));
    // we need float data for knn:
    num.convertTo(num, CvType.CV_32F);
 testData.push_back(num.reshape(1,1));
// Add label for test digit
 testLabs.add(6);

 // make a Mat of the train labels, and train knn:
 KNearest knn = KNearest.create();
 knn.train(trainData, Ml.ROW_SAMPLE, Converters.vector_int_to_Mat(trainLabs));

 // now test predictions:
 for (int j=0; j<testData.rows(); j++)
 {
     Mat one_feature = testData.row(j);
     int testLabel = testLabs.get(j);

     Mat res = new Mat();
     float p = knn.findNearest(one_feature, 1, res);
     System.out.println(testLabel + " " + p + " " + res.dump());
 }

KNearest Java Assertion Error

I'm still new to OpenCV, but already learned a lot. I am however stuck on the K-NN Algorithm. I'm writing some software to read energy meters and I already succesfully wrote the image processing part. When the processing is finished, I end up with digits like this:

image description

For the K-NN part (training and etc), I used almost all code from this OpenCV question. However, when running the following code (and doing the nescessary training in advance):

Mat one_feature = Imgcodecs.imread("/Users/jeremyk/Desktop/output/digits/digit0.png", 0);
Imgproc.resize(one_feature, one_feature, new Size(20, 20));
Mat res = new Mat();
one_feature.convertTo(one_feature, CvType.CV_32F);
one_feature.reshape(1,1);
float p = knn.findNearest(one_feature, 1, res);

I'm getting this nasty error:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in cv::ml::BruteForceImpl::findNearest, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp, line 325
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function cv::ml::BruteForceImpl::findNearest]

I can't seem to find any solution to this.. Anyone got a clue?

Thanks in advance!

Edit: import as grayscale and resize image added to one_feature in an attempt to solve the error.

Edit 2: Here's the training/testing code that I'm using

Edit 3: Here's the updated training/test code that I'm using

   for (int c = 0; c< numberOfTrainingDigits; c++) {
        // crop out 1 digit:
        digit = Imgcodecs.imread("/Users/jeremyk/Desktop/training/digit" + c + ".png");
        Mat num = new Mat();
        Imgproc.resize(digit, num, new Size(60, 110));
        // we need float data for knn:
        num.convertTo(num, CvType.CV_32F);
        // for opencv ml, each feature has to be a single row:
        trainData.push_back(num.reshape(1,1));
        trainLabs.add(c);
    }

    // Add test image
    Mat testDigit = Imgcodecs.imread("/Users/jeremyk/Desktop/testdigit.png", 0);
    Mat num = new Mat();
    Imgproc.resize(testDigit, num, new Size(60, 110));
    // we need float data for knn:
    num.convertTo(num, CvType.CV_32F);
    testData.push_back(num.reshape(1,1));
    testLabs.add(6);

    // make a Mat of the train labels, and train knn:
    KNearest knn = KNearest.create();
    knn.train(trainData, Ml.ROW_SAMPLE, Converters.vector_int_to_Mat(trainLabs));

    // now test predictions:
    for (int j=0; j<testData.rows(); j++)
    {
        Mat one_feature = testData.row(j);
        int testLabel = testLabs.get(j);

        Mat res = new Mat();
        float p = knn.findNearest(one_feature, 1, res);
        System.out.println(testLabel + " " + p + " " + res.dump());
    }