So I am using the OpenCV 2 Computer Vision Application Programming
Cookbook, and I am stuck on the Histogram example from chapter four. I
was trying to translate the C++ code to Java, but there are a lot of
methods that are not the same in the JavaCV API, I worked around a
couple of them but eventually got stuck when trying to access the
histogram data.
Here is the C++ code:
//The class used to get the histogram, returns a histogram for a
grayscale image
class Histogram1D {
private:
int histSize[1]; // number of bins
float hranges[2]; // min and max pixel value
const float* ranges[1];
int channels[1]; // only 1 channel used here
public:
Histogram1D() {
// Prepare arguments for 1D histogram
histSize[0]= 256;
hranges[0]= 0.0;
hranges[1]= 255.0;
ranges[0]= hranges;
channels[0]= 0; // by default, we look at channel 0
}
// Computes the 1D histogram.
cv::MatND getHistogram(const cv::Mat &image) {
cv::MatND hist;
// Compute histogram
cv::calcHist(&image,
1, // histogram from 1 image only
channels, // the channel used
cv::Mat(), // no mask is used
hist, // the resulting histogram
1, // it is a 1D histogram
histSize, // number of bins
ranges // pixel value range
);
return hist;
}
//Initialization code:
// Read input image
cv::Mat image= cv::imread("../group.jpg", 0); // open in b&w
// The histogram object
Histogram1D h;
// Compute the histogram
cv::MatND histo= h.getHistogram(image);
//Looping over the data from the histogram:
// Loop over each bin
for (int i=0; i<256; i++)
cout << "Value " << i << " = " <<
histo.at<float>(i) << endl;
//Desire Output:
...
Value 7 = 159
Value 8 = 208
Value 9 = 271
Value 10 = 288
Value 11 = 340
Value 12 = 418
Value 13 = 432
Value 14 = 472
Value 15 = 525
...
Now the first problem I encountered was creating the histogram, In the
code above, the histogram information is simply given to the calcHist
method, however I couldn't do the same in Java because that is a C++
method according to OpenCV API:
http://opencv.itseez.com/modules/imgproc/doc/histograms.html#calchist
And JavaCV only has the C method. So I searched through the API and
ended up creating a separate CvHistogram using the cvCreateHistogram
method. Then I used that histogram and an a grayscale image with the
calcHist method and it compiled fine.
The real problem is that I can't seem to be able to access the actual
data in the histogram. In the above code they are using the "at"
method, that's not in the CvHistogram class. Then I searched this
group's discussion and found this:
http://groups.google.com/group/javacv/browse_thread/thread/f77a9eca87193567/25bdaa78d675ac54?lnk=gst&q=histogram#25bdaa78d675ac54
From that I tried to look for the method getByteBuffer in the sources,
and found it to be in the cxcore package:
http://www.google.com/codesearch#search/&q=getbytebuffer%20package:http://javacv%5C.googlecode%5C.com&type=cs
The thing is I don't know how to access that package. Here is my code
as you can see I can't get past the call to calcHist, whenever I call
the method asByteBuffer I get an a buffer without an array(used
hasArray method). Any help on how to access the values would be a huge
help thanks!
My Java Code:
public class Histogram {
public static void main(String[] args){
IplImage image = cvLoadImage("C:/Users/Cesar/Desktop/Project
Workspace/Workspace"
+ "/Projects/Netbeans/JavaCV_TestRun/src/
JavaCV_TestRun/util/Histogram_baboom.jpg", CV_LOAD_IMAGE_GRAYSCALE);
CvHistogram hist = cvCreateHist(1, new int[]{256},
CV_HIST_ARRAY, new float[][]{{0.0f, 255.0f}}, 1);
// cvShowImage("Before Calc", image);
// cvWaitKey();
cvCalcHist(new IplImage[]{image}, hist, 0, null);
}
}
All I am doing is loading an image in grayscale, then I want to access