Problem Accessing Histogram Elements in Android Version of OpenCV (Bug #1447)


Added by soriel - over 5 years ago. Updated almost 2 years ago.


Status:Open Start date:
Priority:Normal Due date:
Assignee:Andrey Pavlenko % Done:

0%

Category:java bindings
Target version:-
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

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:

 1public void createHistogram(Bitmap img) {
 2
 3   Mat hist = new Mat();
 4
 5   Mat mat_img = Utils.bitmapToMat(img);
 6
 7   ArrayList<Mat> images = new ArrayList<Mat>();
 8   List<Integer> channels = new ArrayList<Integer>();
 9   List<Integer> histSize = new ArrayList<Integer>();
10   List<Float> ranges = new ArrayList<Float>();
11
12   images.add(mat_img);
13
14   channels.add(0);
15   channels.add(1);
16   channels.add(2);
17
18   // 10x10x10
19   histSize.add(10);
20   histSize.add(10);
21   histSize.add(10);
22   ranges.add(0.0f);
23   ranges.add(256.0f);
24   ranges.add(0.0f);
25   ranges.add(256.0f);
26   ranges.add(0.0f);
27   ranges.add(256.0f);
28
29   Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges, false);
30
31   float[] data = new floatr10;
32   StringBuffer temp = new StringBuffer();
33     for (int r = 0; r < 10; r++)
34       for (int g = 0; g < 10; g++){
35         hist.get(r, g, data);
36         for (int b = 0; b < 10; b++)
37         temp.append(",").append(data[b])
38       }
39   Log.i(TAG, Core.sumElems(hist)); // Prints out the number of pixels in the image (correct)
40   Log.i(TAG, temp.toString()); // Prints out all 0s (Incorrect)
41}

Related issues

related to Bug #3988: Three-Dimensional Mats still not accessible in OpenCV Java New 2014-10-31

History

Updated by Kirill Kornyakov over 5 years ago

Andrey, could you please look into this in the background.

  • Priority changed from High to Normal
  • Target version set to 2.4.0
  • Assignee changed from Kirill Kornyakov to Andrey Pavlenko

Updated by Andrey Kamaev over 5 years ago

  • 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

Updated by Alexander Shishkov over 5 years ago

  • Target version deleted (2.4.0)

Updated by Alexander Shishkov about 5 years ago

  • Assignee deleted (Andrey Pavlenko)

Updated by Andrey Pavlenko about 5 years ago

  • Assignee set to Andrey Pavlenko

Updated by Andrey Pavlenko about 5 years ago

Mat::get() and Mat::put() don't work for Mat-s of more than 2 dimensions.

  • Target version set to 2.4.0

Updated by Andrey Pavlenko about 5 years ago

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

  1public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() {
 2    List<Mat> images = Arrays.asList(rgbLena);
 3
 4    Mat hist3D = new Mat();
 5    List<Mat> histList = Arrays.asList( new Mat[] {new Mat(), new Mat(), new Mat()} );
 6
 7    MatOfInt histSize = new MatOfInt(10);
 8    MatOfFloat ranges = new MatOfFloat(0f, 256f);
 9
10    for(int i=0; i<rgbLena.channels(); i++)
11    {
12        Imgproc.calcHist(images, new MatOfInt(i), new Mat(), histList.get(i), histSize, ranges);
13
14        assertEquals(10, histList.get(i).checkVector(1));
15    }
16
17    Core.merge(histList, hist3D);
18
19    assertEquals(CvType.CV_32FC3, hist3D.type());
20    assertEquals(10, hist3D.checkVector(3));
21
22    Mat truth = new Mat(10, 1, CvType.CV_32FC3);
23    truth.put(0, 0,
24             0, 24870, 0,
25             1863, 31926, 1,
26             56682, 37677, 2260,
27             77278, 44751, 32436,
28             69397, 41343, 18526,
29             27180, 40407, 18658,
30             21101, 15993, 32042,
31             8343, 18585, 47786,
32             300, 6567, 80988,
33             0, 25, 29447
34            );
35
36    assertMatEqual(truth, hist3D, EPS);
37}

  • Target version deleted (2.4.0)

Updated by Andrey Pavlenko about 5 years ago

  • Category changed from android to java bindings

Updated by Maksim Shabunin almost 2 years ago

Issue has been transferred to GitHub: https://github.com/Itseez/opencv/issues/4308

Also available in: Atom PDF