question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cv2.CalcHist doesn't return histogram

See original GitHub issue

I calculate histogram of RGB image with this func:

static void calc_hist_bgr(Mat bgr,  Mat hist)
        {
           int[] channels = new int[3] {0, 1, 2}; 
           const int b_bins = 8; 
           const int g_bins = 8; 
           const int r_bins = 8;
           int[] hist_size = {b_bins, g_bins, r_bins};
           float[] branges = {0, 256};
           float[] granges = {0, 256};
           float[] rranges = {0, 256};
           float[][] ranges = {branges, granges, rranges};
           Mat mask = new Mat();
           const int dims = 3;
           Mat[] srcs = {bgr};         
           Cv2.CalcHist(srcs, channels, mask, hist, dims, hist_size, ranges, true, false);
        }

My bgr image is: Mat [77x59xCV_8UC3,…] But i got hist image: Mat [-1x-1xCV_32FC1…]

What was i wrong ? Thanks

Sorry, i have tried to insert the code format but i couldn’t. I don’t know why

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
fdncredcommented, Oct 6, 2016

You might want to look at HistSample.cs and KAZESample2.cs for tips. When I had to use it, I modeled mine off HistSample.cs (below) and it worked fine.

    public void Run()
    {
        Mat src = Cv2.ImRead(FilePath.Image.Lenna, ImreadModes.GrayScale);

        // Histogram view
        const int Width = 260, Height = 200;
        Mat render = new Mat(new Size(Width, Height), MatType.CV_8UC3, Scalar.All(255));

        // Calculate histogram
        Mat hist = new Mat();
        int[] hdims = {256}; // Histogram size for each dimension
        Rangef[] ranges = { new Rangef(0,256), }; // min/max 
        Cv2.CalcHist(
            new Mat[]{src}, 
            new int[]{0}, 
            null,
            hist, 
            1, 
            hdims, 
            ranges);

        // Get the max value of histogram
        double minVal, maxVal;
        Cv2.MinMaxLoc(hist, out minVal, out maxVal);

        Scalar color = Scalar.All(100);
        // Scales and draws histogram
        hist = hist * (maxVal != 0 ? Height / maxVal : 0.0);
        for (int j = 0; j < hdims[0]; ++j)
        {
            int binW = (int)((double)Width / hdims[0]);
            render.Rectangle(
                new Point(j * binW, render.Rows),
                new Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
                color, 
                -1);
        }

        using (new Window("Image", WindowMode.AutoSize | WindowMode.FreeRatio, src))
        using (new Window("Histogram", WindowMode.AutoSize | WindowMode.FreeRatio, render))
        {
            Cv2.WaitKey();
        }
    }
1reaction
nanoMVcommented, Oct 11, 2016

It did not work !

Just simple sample:

Mat src = new Mat(@"C:\desert.jpg", ImreadModes.Color); Mat hist = new Mat(); int[] hdims = {256,256,256}; Rangef[] ranges = {new Rangef(0,256),new Rangef(0,256),new Rangef(0,256) }; Cv2.CalcHist(new Mat[]{src}, new int[]{0,1,2}, null, hist, 3, hdims, ranges); I got the hist with size of (-1,-1);

But if with one channel, it works: Mat src = new Mat(@"C:\desert.jpg"); Mat hist = new Mat(); int[] hdims = {256}; Rangef[] ranges = {new Rangef(0,256),}; Cv2.CalcHist(new Mat[]{src}, new int[]{0}, null, hist, 1, hdims, ranges); I got the hist with size of (256,1)

Read more comments on GitHub >

github_iconTop Results From Across the Web

calcHist() doesn't return green histogram as expected
I am using OpenCV on Ubuntu 12.04. No fancy IDEs. Just compiling and running from the command-line. This is my code for calculating...
Read more >
cv2.calcHist - sum of histogram does not equal pixel count
I just observed a strange behaviour of cv2.calcHist. When calculating the sum of all bin-values it sometimes does not add up to the...
Read more >
Get value from histogram - vision
I have a lot of images to threshold. The correct threshold seems to be where the histogram of the image dies out a...
Read more >
3 : 2D Histograms
2D Histogram in OpenCV. It is quite simple and calculated using the same function, cv.calcHist(). For color histograms, we need to convert the...
Read more >
Python OpenCV - cv2.calcHist method
calcHist () function to calculate the image histograms. ... Return: It returns an array of histogram points of dtype float32.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found