Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Color (>2-channel) Histogram, Java API

In short: why would Imgproc.calcHist work on 2, but not on 3 (or more) channels/dimensions? Is this a bug? If so, is it already reported, maybe even worked on? Can we expect a fix any time soon? And if not - or in any way - what are my options to deal with this? Any suggestions/workarounds?

In full; the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1, 2);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...results with a hist of type -1x-1xCV_32FC1. While I don't get an error or anything, this is just garbage. :(

But then, on the other hand, the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                //0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...ends up with a flawless histogram of type 256x256xCV_32FC1. Wait, what?

What kind of bug/mess is this? While it's nice to get a histogram for only 2 channels, have a second/closer look at the returned Mat. A single-channel(!) 256x256 matrix? Really? Wouldn't you've expected to get a 256x1xCV_32FC2 or something?

I mean... I have a weird feeling as to why the above code fails to compute a histogram for 3 channels. Or have you ever seen a 256x256x256 matrix around here? ;)

So... what are my options to compute my 3-channel/dimensional histogram? :/

Thanks. :)

Color (>2-channel) Histogram, Java API

In short: why would Imgproc.calcHist work on 2, but not on 3 (or more) channels/dimensions? Is this a bug? If so, is has it already reported, maybe even worked on? been reported (someone working on this)? Can we expect a fix any time soon? And if not - or in any way regardless - what are my options to deal with this? Any suggestions/workarounds?

In full; the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1, 2);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...results with a hist of type -1x-1xCV_32FC1. While I don't get an error or anything, this is just garbage. :(

But then, on the other hand, the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                //0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...ends up with a flawless histogram of type 256x256xCV_32FC1. Wait, what?

What kind of bug/mess is this? While it's nice to get a histogram for only 2 channels, have a second/closer look at the returned Mat. A single-channel(!) 256x256 matrix? Really? Wouldn't you've expected to get a 256x1xCV_32FC2 or something?

I mean... I have a weird feeling as to why the above code fails to compute a histogram for 3 channels. Or have you ever seen a 256x256x256 matrix around here? ;)

So... what are my options to compute my 3-channel/dimensional histogram? :/

Thanks. :)

Color (>2-channel) Histogram, Java API

In short: why would Imgproc.calcHist work on 2, but not on 3 (or more) channels/dimensions? Is this a bug? If so, has it already been reported (someone working on this)? Can we expect a fix any time soon? And if not - or regardless - what are my options to deal with this? Any suggestions/workarounds?

In full; the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1, 2);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...results with a hist of type -1x-1xCV_32FC1. While I don't get an error or anything, this is just garbage. :(

But then, on the other hand, the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                //0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...ends up with a flawless histogram of type 256x256xCV_32FC1. Wait, what?

What kind of bug/mess is this? While it's nice to get a histogram for only 2 channels, have a second/closer look at the returned Mat. A single-channel(!) 256x256 matrix? Really? Wouldn't you've expected to get a 256x1xCV_32FC2 or something?

I mean... I have a weird feeling as to why the above code fails to compute a histogram for 3 channels. Or have you ever seen a 256x256x256 matrix around here? ;).

So... what are my options to compute my 3-channel/dimensional histogram? :/

Thanks. :)

Color (>2-channel) Histogram, Java API

In short: why would Imgproc.calcHist work on 2, but not on 3 (or more) channels/dimensions? Is this a bug? If so, has it already been reported (someone working on this)? Can we expect a fix any time soon? And if not - or regardless - what are my options to deal with this? Any suggestions/workarounds?

In full; the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1, 2);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...results with a hist of type -1x-1xCV_32FC1. While I don't get an error or anything, this is just garbage. :(

But then, on the other hand, the following code:

public class Histogram3D {
    private List<Mat> images;
    private MatOfInt channels;
    private Mat mask;
    private Mat hist;
    private MatOfInt histSize;
    private MatOfFloat ranges;

    public Histogram3D() {
        histSize = new MatOfInt(256, 256);
        ranges = new MatOfFloat(0.0f, 255.0f,
                                //0.0f, 255.0f,
                                0.0f, 255.0f); 
        channels = new MatOfInt(0, 1);
        mask = new Mat();
        hist = new Mat();
    }

    public void computeHistogram(Mat image) {
        images = new ArrayList<>();
        images.add(image);

        Imgproc.calcHist(images, channels, mask, hist, histSize, ranges);
    }
    // ...

...ends up with a flawless histogram of type 256x256xCV_32FC1.. Hmm, granted, how should hist even look like with 3 dimensions? 256x256x256xCV_32FC1 does not compute. I've never seen a 256x256xCV_32FC256-Mat before... How about (256x256)x256xCV_32FC1 maybe? A list/an array of 256 256x256xCV_32FC1 would sound reasonable, but the methods signature wouldn't fit that any longer? ...

So... what are my options to compute my 3-channel/dimensional histogram? :/(But ok, if my concern is a "color histogram", I might wanna check out other color spaces...)

Thanks. :)