简体   繁体   中英

JavaCV K Means Runtime Error

I am a beginner of JavaCV. I am trying to process K Means on a image. However, I got a runtime error from following code. I don't know how to solve it and process K Means successfully. Thanks for your help.

This is the error:

OpenCV Error: Assertion failed (labels.isContinuous() && labels.type() == CV_32S &&  
(labels.cols == 1 || labels.rows == 1) && labels.cols + labels.rows - 1 == data.rows) in 
cvKMeans2

This is my JavaCV code:

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.*;
import org.bytedeco.javacv.CanvasFrame;

public class KMeansTest {
public static void runTest(){
    IplImage image = cvLoadImage("img/2014-05-18_181424.png");
    CvMat model = image.asCvMat();

    //Number of cluster
    int k=2;
    CvMat cluster = model.clone();

    cvKMeans2(model, k, cluster, cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0));

    CanvasFrame canvas = new CanvasFrame("My Image", 1);
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    canvas.showImage(cluster.asIplImage());
}
}

You need to make sure all the assertion conditions are true.

(labels.isContinuous() && labels.type() == CV_32S && (labels.cols == 1 || labels.rows == 1) && labels.cols + labels.rows - 1 == data.rows)

Here labels refers your model , therefore you need to make sure all the properties are correctly set before you passed it into the cvKMeans2 function.

Access the each property and see which one causing the error.

Simple System.out.println("Type :" + model .type()); would do.

I can clearly see that your model.type() is not CV_32S, since you cloned it from IplImage directly it in default format IPL_DEPTH_8U, convert it to IPL_DEPTH_32S. Likewise, figure out one by one.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM