Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

     //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);

    }

}

}