Ask Your Question

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

edit retag close merge delete

## Comments

I don't know ios but there is a 3d histogram tutorial with c++ code here

( 2019-02-20 08:50:42 -0500 )edit

so already line #1 of your program fails, trying to convert a 1 channel img to hsv.

( 2019-02-20 09:14:13 -0500 )edit

Berak, Not sure about it is failing in line#1. Since it is working perfectly for 2-dimension. It fails in the calcHist function.

( 2019-02-20 09:35:54 -0500 )edit
1

I have edited the question, Exception is thrown by a statement below calcHist() call. calcHist just generates an invalid hist matrix.

( 2019-02-20 10:07:37 -0500 )edit

no, there is no exception now. (there was one related to cvtColor, before)

( 2019-02-21 07:41:37 -0500 )edit

## 1 answer

Sort by ยป oldest newest most voted

But the output hist matrix is invalid with values (rows, cols) as (-1, -1).

a multi (more than 2d) dimensional mat can't express it's dimensions using rows & cols only, thus they're left empty(at -1) and you have to query the Mat::size member (note: Not the function !):

cv::Mat hist; // MatND is an alias for Mat, since a very long time already ..

cv::calcHist(&matImageHSV, nImages, channels, cv::Mat(), // do not use mask
hist, dims, histSize, ranges,
true, // the histogram is uniform
false );

cout << hist.dims << endl;
cout << hist.size << endl;

3
36 x 25 x 25  // hbins, sbins, vbins, again !

more

## Comments

( 2019-02-21 07:42:36 -0500 )edit
1

Thanks a lot; LBerger and @berak. I am new to OpenCV, my assumptions where wrong, thanks for correcting.

( 2019-02-21 07:49:36 -0500 )edit

you'll see the same with the 4d blob Mat's from opencv's dnn, keep it in mind ;)

( 2019-02-21 07:57:14 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2019-02-20 08:43:45 -0500

Seen: 544 times

Last updated: Feb 21 '19