Ask Your Question

Revision history [back]

Java - OpenCV - KMeans Assertion Failed

I'm attempting to implement a Bag Of Features descriptor for visual data classification using Sift. At the moment I'm very new to this and am just trying to learn the OpenCV library a bit more, but i have come across an issue using the Core.kmeans() function. When run, i receive the following error.

OpenCV Error: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0) in cv::kmeans, file ..\..\..\..\opencv\modules\core\src\matrix.cpp, line 2701 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\matrix.cpp:2701: error: (-215) data.dims <= 2 && type == CV_32F && K > 0 in function cv::kmeans ]

Now from looking around to attempt to fix this I've come to some understandings of the process that needs to be taken to do this, particularly from This Answer. But to no avail.

Can anyone explain to me how can I fix this problem?

*Edit: * I am attempting to implement a solution outlined Here

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.core.TermCriteria;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;


class SIFT {
    public static void main(String[] args) {

        // load in system library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        new SiftTrainer().run();
}

class SiftTrainer extends Thread {
    // Trainer class for image identifer

    public DescriptorExtractor detector = DescriptorExtractor.create(DescriptorExtractor.ORB);
    // cfreate fast feature detector
    public FeatureDetector fd = FeatureDetector.create(FeatureDetector.FAST);

    public SiftTrainer() {}

    public void run() {
        // select a certain portion of the images loaded
        // to extract features and build vocabulary
            // read in image
            Mat image1 = Highgui.imread("C:/controlImage.jpg");
            Mat image2 = Highgui.imread("C:/controlImage2.jpg");

            Imgproc.cvtColor(image1, image1, Imgproc.COLOR_BGR2GRAY);
            Imgproc.cvtColor(image2, image2, Imgproc.COLOR_BGR2GRAY);

            Mat dist32_1 = new Mat(image1.rows(),image1.cols(),CvType.CV_32F);
            Mat dist32_2 = new Mat(image2.rows(),image2.cols(),CvType.CV_32F);
            image1.convertTo(dist32_1, CvType.CV_32F);
            image2.convertTo(dist32_2, CvType.CV_32F);

            MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
            MatOfKeyPoint keypoints2 = new MatOfKeyPoint();


            Mat descriptor = new Mat();
            // detect feature points
            fd.detect(image1, keypoints1);
            fd.detect(image2, keypoints2);
            // compute the descriptors for each keypoint
            detector.compute(image1, keypoints1, descriptor);
            detector.compute(image2, keypoints2, descriptor);

            Mat featuresUnclustered = new Mat();
            // put all the feature descriptors in a single mat object
            featuresUnclustered.push_back(descriptor);


        // construct the BOWMeansTrainer
        // the number of bags
        int dictionarySize = 100;
        // define the term criteria
        TermCriteria crit = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
        // define the retries number
        int retries = 1;
        // necessary flags
        int flags = Core.KMEANS_PP_CENTERS;
        Mat bestMatches = new Mat();
        // create the BOF trainer
        Core.kmeans(image1, 6, bestMatches, crit, retries, flags);


        System.out.println(bestMatches);
    }
}

class SiftIdentifer {
    // Identifer class for our image
}