calcHist invalid Mat when dims = 3 (n-dimensional histogram)
Hi all, I would like to generate a 3-dimensional HSV Histogram. It works for 2-D HS Histogram but fails to generate Histogram for 3-dimension. Can someone help me understand what I am doing wrong?
cv::cvtColor(matImage, matImageHSV, CV_BGR2HSV);
// Quanta Ratio
int scale = 10;
// Quantize the hue to 36 levels
// and the saturation to 25 levels
int hbins = 36, sbins = 25, vbins = 25;
int histSize[] = {hbins, sbins, vbins};
// hue varies from 0 to 179, see cvtColor
float hranges[] = { 0, 180 };
// saturation varies from 0 (black-gray-white) to
// 255 (pure spectrum color)
float sranges[] = { 0, 256 };
float vranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges, vranges };
// we compute the histogram from the 0-th and 1-st channels
int channels[] = {0, 1, 2};
int dims = 3;
int nImages = 1;
// Calculate histogram
cv::MatND hist;
cv::calcHist(&matImageHSV, nImages, channels, cv::Mat(), // do not use mask
hist, dims, histSize, ranges,
true, // the histogram is uniform
false );
There is no exception or error is thrown.
But the output hist matrix is invalid with values (rows, cols) as (-1, -1).
I don't know ios but there is a 3d histogram tutorial with c++ code here
so already line #1 of your program fails, trying to convert a 1 channel img to hsv.
Berak, Not sure about it is failing in line#1. Since it is working perfectly for 2-dimension. It fails in the calcHist function.
I have edited the question, Exception is thrown by a statement below calcHist() call. calcHist just generates an invalid hist matrix.
no, there is no exception now. (there was one related to cvtColor, before)