Repository: opencv
Issue: Problem Accessing Histogram Elements in Android Version of OpenCV
Labels: bug,auto-transferred,priority: normal,category: java bindings

Transferred from http://code.opencv.org/issues/1447

```
|| soriel - on 2011-10-25 21:26
|| Priority: Normal
|| Affected: None
|| Category: java bindings
|| Tracker: Bug
|| Difficulty: None
|| PR: None
|| Platform: None / None
```
## Problem Accessing Histogram Elements in Android Version of OpenCV

```
I need to access the elements of a @Mat@ that is created by @Mat.calcHist@ call. However, it appears that @Mat.get@ does not return the correct set of values. Here is an example:

<pre><code class="java">
public void createHistogram(Bitmap img) {

   Mat hist = new Mat();

   Mat mat_img = Utils.bitmapToMat(img);

   ArrayList<Mat> images = new ArrayList<Mat>();
   List<Integer> channels = new ArrayList<Integer>();
   List<Integer> histSize = new ArrayList<Integer>();
   List<Float> ranges = new ArrayList<Float>();

   images.add(mat_img);

   channels.add(0);
   channels.add(1);
   channels.add(2);

   // 10x10x10
   histSize.add(10);
   histSize.add(10);
   histSize.add(10);
   ranges.add(0.0f);
   ranges.add(256.0f);
   ranges.add(0.0f);
   ranges.add(256.0f);
   ranges.add(0.0f);
   ranges.add(256.0f);

   Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges, false);

   float[] data = new floatr10;
   StringBuffer temp = new StringBuffer();
     for (int r = 0; r < 10; r++)
       for (int g = 0; g < 10; g++){
         hist.get(r, g, data);
         for (int b = 0; b < 10; b++)
         temp.append(",").append(data[b])
       }
   Log.i(TAG, Core.sumElems(hist)); // Prints out the number of pixels in the image (correct)
   Log.i(TAG, temp.toString()); // Prints out all 0s (Incorrect)
}
</code></pre>
```
## History
##### Kirill Kornyakov on 2012-02-14 15:16

```
Andrey, could you please look into this in the background.
-   Target version set to 2.4.0
-   Priority changed from High to Normal
-   Assignee changed from Kirill Kornyakov to Andrey Pavlenko
```
##### Andrey Kamaev on 2012-02-25 12:23

```
-   Description changed from I need to access the elements of a Mat that
    is created by Mat.calcHist call. ... to I need to access the
    elements of a @Mat@ that is created by @Mat.calcHist@ ca... More
```
##### Alexander Shishkov on 2012-03-21 11:38

```
-   Target version deleted (2.4.0)
```
##### Alexander Shishkov on 2012-03-25 20:43

```
-   Assignee deleted (Andrey Pavlenko)
```
##### Andrey Pavlenko on 2012-03-26 08:26

```
-   Assignee set to Andrey Pavlenko
```
##### Andrey Pavlenko on 2012-04-02 12:32

```
Mat::get() and Mat::put() don't work for Mat-s of more than 2 dimensions.
-   Target version set to 2.4.0
```
##### Andrey Pavlenko on 2012-04-26 11:58

```
The support of 3+ dimensional Mat-s in Java API will be done after 2.4 release.
Currently as a workaround I suggest use calcHist() for 1 or 2 channels only, e.g.:
<pre> <code class="java">
public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() {
    List<Mat> images = Arrays.asList(rgbLena);

    Mat hist3D = new Mat();
    List<Mat> histList = Arrays.asList( new Mat[] {new Mat(), new Mat(), new Mat()} );

    MatOfInt histSize = new MatOfInt(10);
    MatOfFloat ranges = new MatOfFloat(0f, 256f);

    for(int i=0; i<rgbLena.channels(); i++)
    {
        Imgproc.calcHist(images, new MatOfInt(i), new Mat(), histList.get(i), histSize, ranges);

        assertEquals(10, histList.get(i).checkVector(1));
    }

    Core.merge(histList, hist3D);

    assertEquals(CvType.CV_32FC3, hist3D.type());
    assertEquals(10, hist3D.checkVector(3));

    Mat truth = new Mat(10, 1, CvType.CV_32FC3);
    truth.put(0, 0,
             0, 24870, 0,
             1863, 31926, 1,
             56682, 37677, 2260,
             77278, 44751, 32436,
             69397, 41343, 18526,
             27180, 40407, 18658,
             21101, 15993, 32042,
             8343, 18585, 47786,
             300, 6567, 80988,
             0, 25, 29447
            );

    assertMatEqual(truth, hist3D, EPS);
}
</code></pre>
-   Target version deleted (2.4.0)
```
##### Andrey Pavlenko on 2012-06-20 08:15

```
-   Category changed from android to java bindings
```