Ask Your Question
0

Assertion failed (j < nimages) in histPrepareImages, file /home/ibytecode/OpenCV/modules/imgproc/src/histogram.cpp, line 148

asked 2017-05-11 02:47:54 -0600

sriram04 gravatar image
     //I am new to opencv in java. can't identify error in histogram plotting.

//so help needed.

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // Step-2 : Requesting user to enter the number of distinct colors in the image
    int ncluster;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter No of Distinct colors :");
    ncluster = scanner.nextInt();

    /* Step 3:
     * You might have to change the path according to your installation. 
     */

    Mat img = Imgcodecs.imread("/home/users/bsriram/Documents/GDPjt/GDPL6.png");//, Imgcodecs.CV_LOAD_IMAGE_COLOR);

    Mat img32F = new Mat();

    img.convertTo(img32F, CvType.CV_32F);
    Imgcodecs.imwrite("src.png", img32F);

    int z = img32F.channels();
    int y = img32F.rows();
    int x = img32F.cols();

    System.out.println("cols = " + x);
    System.out.println("rows = " + y);
    System.out.println("channels = " + z);
    System.out.println("dims" + img32F.dims());
    System.out.println("type" + img32F.type());
    System.out.println("depth" + img32F.depth());



    List<Mat> channels = new ArrayList<>(z); 
    Core.split(img32F, channels);

    for (int i = 0; i < z; i++) {
        System.out.println("Processing Plane " + i);

        Mat label01 = new Mat();// = Mat.zeros(y, x, CvType.CV_32F);
        Mat center01 = new Mat();
        TermCriteria criteria = new TermCriteria(TermCriteria.MAX_ITER, 300, 1e-4);
        Core.kmeans(img32F, z, label01, criteria, ncluster, Core.KMEANS_RANDOM_CENTERS, center01);
        System.out.println(Arrays.asList(label01));


        TermCriteria criteriak = new TermCriteria(TermCriteria.MAX_ITER, 300, 1e-4);
        Mat label02 = new Mat();
        Mat center02 = new Mat();
        Core.kmeans(img32F, z, label02, criteriak, ncluster, Core.KMEANS_RANDOM_CENTERS, center02);
        System.out.println(Arrays.asList(label02));


        //replace the label02 values with mode values of label02 itself
        //to do so find the histogram of label02

        MatOfInt histSize = new MatOfInt(256, 256, 256);
        MatOfFloat range = new MatOfFloat(0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f);
        int channelArray[] = {0, 1, 2};
        MatOfInt histChannels = new MatOfInt(channelArray);


        Mat histMat = new Mat();
        ArrayList<Mat> matList = new ArrayList();
        matList.add(label02);
        Imgproc.calcHist(matList, histChannels, new Mat(), histMat, histSize, range);
        System.out.println("histMat : "+histMat);

    }

}

}

edit retag flag offensive close merge delete

Comments

the labels from kmeans only have a single (int) channel, not 3, and the range is [0,nCluster], not [0,255]

berak gravatar imageberak ( 2017-05-11 02:56:33 -0600 )edit

thanks bro for your valuable answer , ill try to fix it soon

sriram04 gravatar imagesriram04 ( 2017-05-11 03:03:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-29 17:22:06 -0600

seemahan gravatar image

Usually j < nImages occurs when dims and channels don't match, in case you are using openCV function calHist()

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-05-11 02:47:54 -0600

Seen: 3,623 times

Last updated: May 11 '17