Ask Your Question
0

calcHist invalid Mat when dims = 3 (n-dimensional histogram)

asked 2019-02-20 08:43:45 -0600

ramg8x gravatar image

updated 2019-02-20 10:07:13 -0600

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 flag offensive close merge delete

Comments

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

LBerger gravatar imageLBerger ( 2019-02-20 08:50:42 -0600 )edit

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

berak gravatar imageberak ( 2019-02-20 09:14:13 -0600 )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.

ramg8x gravatar imageramg8x ( 2019-02-20 09:35:54 -0600 )edit
1

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

ramg8x gravatar imageramg8x ( 2019-02-20 10:07:37 -0600 )edit

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

berak gravatar imageberak ( 2019-02-21 07:41:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-21 07:35:17 -0600

berak gravatar image

updated 2019-02-21 07:55:54 -0600

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 !
edit flag offensive delete link more

Comments

LBerger gravatar imageLBerger ( 2019-02-21 07:42:36 -0600 )edit
1

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

ramg8x gravatar imageramg8x ( 2019-02-21 07:49:36 -0600 )edit

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

berak gravatar imageberak ( 2019-02-21 07:57:14 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 684 times

Last updated: Feb 21 '19