Ask Your Question
1

*java* api Histogram calculation

asked 2013-03-11 16:46:19 -0600

Shikhar gravatar image

updated 2013-03-12 01:40:08 -0600

Hi! I have been trying to write a function to compute the 3D histogram of a color image. i am very new to OpenCV. I am unable to get the result. I am getting the following error

OpenCV Error: Assertion failed (csz == 0 || csz == dims) in unknown function, file ..\..\..\src\opencv\modules\imgproc\src\histogram.cpp, line 1419
CvException [org.opencv.core.CvException: ..\..\..\src\opencv\modules\imgproc\src\histogram.cpp:1419: error: (-215) csz == 0 || csz == dims]
    at org.opencv.imgproc.Imgproc.calcHist_1(Native Method)
    at org.opencv.imgproc.Imgproc.calcHist(Imgproc.java:2775)
    at shikhar.logic.OpenCVTest.getColorHistogram(OpenCVTest.java:82)
    at shikhar.logic.OpenCVTest.compareImages(OpenCVTest.java:133)
    at shikhar.logic.OpenCVTest.main(OpenCVTest.java:30)
OpenCV Error: Assertion failed (csz == 0 || csz == dims) in unknown function, file ..\..\..\src\opencv\modules\imgproc\src\histogram.cpp, line 1419
CvException [org.opencv.core.CvException: ..\..\..\src\opencv\modules\imgproc\src\histogram.cpp:1419: error: (-215) csz == 0 || csz == dims]
    at org.opencv.imgproc.Imgproc.calcHist_1(Native Method)
    at org.opencv.imgproc.Imgproc.calcHist(Imgproc.java:2775)
    at shikhar.logic.OpenCVTest.getColorHistogram(OpenCVTest.java:82)
    at shikhar.logic.OpenCVTest.compareImages(OpenCVTest.java:134)
    at shikhar.logic.OpenCVTest.main(OpenCVTest.java:30)
java.lang.NullPointerException
    at org.opencv.imgproc.Imgproc.compareHist(Imgproc.java:2848)
    at shikhar.logic.OpenCVTest.compareImages(OpenCVTest.java:135)
    at shikhar.logic.OpenCVTest.main(OpenCVTest.java:30)
-1.0

the code I had written was

public static Mat getColorHistogram(String imgLocation)
{
    try
    {
        Mat img=new Mat();
        img=org.opencv.highgui.Highgui.imread(imgLocation);
        List<Mat> imagesList=new ArrayList<>();
        imagesList.add(img);
        //Mat tempChannel=new Mat(new Size(3,1),org.opencv.core.CvType.
        int histSizeArray[]={256,256,256};
        int channelArray[]={0,1,2};
        MatOfInt channels=new MatOfInt(channelArray);
        //mChannels = new MatOfInt[] { new MatOfInt(0), new MatOfInt(1), new MatOfInt(2) };
        Mat hist=new Mat();
        MatOfInt histSize=new MatOfInt(256);
        //histSize.fromArray(histSizeArray);
        //histSize = new MatOfInt(256);
        float hrangesArray[]={0.0f,255.0f};
        MatOfFloat ranges=new MatOfFloat(0.0f,255.0f);
        org.opencv.imgproc.Imgproc.calcHist(imagesList, channels,new Mat(), hist, histSize, ranges);
        return hist;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return null;
    }
}

Here the line 82 corresponds to the calcHist line

edit retag flag offensive close merge delete

Comments

Hi have you find a solution !!

Marwen Z gravatar imageMarwen Z ( 2013-03-31 03:47:00 -0600 )edit

How did you fix it.

dewmal gravatar imagedewmal ( 2013-04-06 08:37:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-09-02 09:16:46 -0600

ejehardenberg gravatar image

updated 2014-09-02 09:20:47 -0600

I just ran into this problem and solved it. The important things to note are the following areas within the native library:

CV_Assert(nimages > 0 && dims > 0);
CV_Assert(rsz == dims*2 || (rsz == 0 && images.depth(0) == CV_8U));
CV_Assert(csz == 0 || csz == dims);

dims is the number of elements in the histSize vector, rsz is the range vector size, and csz is the channel vector size. Looking at your code we can see that the third assertion fails because the channel size (your channels vector) is of size 3, but the size of your histSize vector is only 1! So what you should do is change

MatOfInt histSize=new MatOfInt(256);

to

MatOfInt histSize=new MatOfInt(256, 256, 256);

now of course you will get an assertion error on the second assertion in the native code above, so you should then notice that your ranges vector must be modified to satisfy the requirement of rsz == dims*2, in other words, specify a range for each bucket like so:

 MatOfFloat ranges=new MatOfFloat(0.0f,255.0f, 0.0f, 255.0f, 0.0f, 255.0f);

Hope this helps

Edit: Also, you should think about using 256.0f instead of 255.0f in your ranges since it is exclusive on the top (it says it somewhere in the documentation but I don't have a link)

edit flag offensive delete link more

Comments

hi that's work i add 2 lines to the the same code

Core.normalize(hist,hist);
System.out.println("hsit \n"+ hist.dump());

but i have a problem with the hist.dump()

i just want to see the result and restore it in a fille (.txt for exemple ) the error

    OpenCV Error: Assertion failed (m.dims <= 2) in `anonymous-namespace'::FormattedImpl::FormattedImpl, file ..\..\..\..\opencv\modules\core\src\out.cpp, line 86
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\out.cpp:86: error: (-215) m.dims <= 2 in function `anonymous-namespace'::FormattedImpl::FormattedImpl
]at org.opencv.core.Mat.nDump(Native Method)
    at org.opencv.core.Mat.dump(Mat.java:2490)
    at Hello.main(Hello.java:115)
renalte gravatar imagerenalte ( 2015-01-06 08:24:43 -0600 )edit

Question Tools

Stats

Asked: 2013-03-11 16:46:19 -0600

Seen: 5,875 times

Last updated: Sep 02 '14