Ask Your Question

Cluster crushes with unknown exception

asked 2018-01-10 10:50:41 -0500

Svistoplyas gravatar image

Dear Community

I seek for your help. I found this project that builds Bag-of-Features Descriptor on SIFT Features on OpenCV 2.3. I will use OpenCV 3.4.0

I tried to rewrite first part of it to Java and there are two things that are not similar between his and my code. Firstly I chose only 3 images and he chose 20. Secondly I'm not sure that TermCriteria types in C++ and Java are similar.

He uses this


type. But I have only these types to choose

TermCriteria.EPS TermCriteria.COUNT TermCriteria.MAX_ITER

Here is my code

public static void main(String[] args) throws Exception{
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);

    Mat img;
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat desc = new Mat();
    Mat featuresUnclustered = new Mat();

    for(String file : filename){
        img = Imgcodecs.imread(file, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        detector.detect(img, keypoints);
        descriptor.compute(img, keypoints, desc);

    //Construct BOWKMeansTrainer
    //the number of bags
    int dictionarySize=200;
    //define Term Criteria
    TermCriteria tc = new TermCriteria(TermCriteria.EPS | TermCriteria.MAX_ITER,100,0.001);
    //retries number
    int retries=1;
    //necessary flags
    int flags=Core.KMEANS_PP_CENTERS;
    //Create the BoW (or BoF) trainer
    BOWKMeansTrainer bowTrainer = new BOWKMeansTrainer(dictionarySize,tc,retries,flags);
    //cluster the feature vectors
    Mat dictionary=bowTrainer.cluster(featuresUnclustered);
    //store the vocabulary

And this is exception that i got

image description

Any information will be helpful

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-01-10 11:04:17 -0500

berak gravatar image

updated 2018-01-10 11:16:58 -0500

you cannot use ORB descriptors here, they are binary ones, and float descriptors are required for kmeans clustering. this means, you have to use SIFT,SURF or AKAZE. (no please DON'T try to convert them, it's useless)

btw, you won't be able to construct a BOWImgDescriptorExtractor from java (it's all no problem from c++ or python, just the java wrappers are a bit retarded here), so sooner or later, you'll have to dive into the c++ src code, and rewrite it on your own.

then, to make sense, you need like ~500 - 1000 images, 3 will be never enough

edit flag offensive delete link more


Thank you for answer. Firstly I've chosen SIFT descriptors and after that I changed it to SURF and ORB, because I thought that the problem was somehow in SIFT.

I suppose it easier to choose C++ or Python to write than fix source code.

Svistoplyas gravatar imageSvistoplyas ( 2018-01-10 20:58:25 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-01-10 10:49:24 -0500

Seen: 138 times

Last updated: Jan 10 '18