Ask Your Question
2

Color (>2-channel) Histogram, Java API

asked 2013-04-19 22:51:22 -0600

Lucky Luke gravatar image

updated 2013-04-19 23:07:54 -0600

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. :)

edit retag flag offensive close merge delete

Comments

I have the same problem in java. Did you solve it or you just move to C++ to solve this?

sao gravatar imagesao ( 2014-07-15 13:09:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-04-21 04:44:43 -0600

Andrey Pavlenko gravatar image

The problem is that Java binding for the Mat C++ class supports 1 or 2 dimensional Mats only. See the issue #1447. The possible workarounds are use 1 or 2 dimensional histograms or use native C++ code for this task.

edit flag offensive delete link more

Comments

Ahh, I see. Well, in this case it's a non-issue, since I can totally live with 2-dimensional histograms (especially in the realms of color).

Yet it makes me wonder if I will eventually run into this sort of problem (no higher dimensional matrices for the java guys...) elsewhere (guess I'll learn soon enough...).

So long and thanks for all the fish! :)

Lucky Luke gravatar imageLucky Luke ( 2013-04-21 08:01:14 -0600 )edit

Question Tools

Stats

Asked: 2013-04-19 22:51:22 -0600

Seen: 2,491 times

Last updated: Apr 21 '13