opencv calcHist results are not what expected

为君一笑 提交于 2019-12-13 15:47:01

问题


In openCV, I have a matrix of integers (a 4000x1 Mat). Each time I read different ranges of this matrix: Mat labelsForHist = labels(Range(from,to),Range(0,1));

The size of the ranges is variable. Then I convert the labelsForHist matrix to float(because calcHist doesnt accept int values!) by using:

labelsForHist.convertTo(labelsForHistFloat, CV_32F);

After this I call calcHist with these parameters:

Mat hist;
int histSize = 4000;
float range[] = { 0, 4000 } ;
int channels[] = {0};
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
calcHist(&labelsForHistFloat,1,channels,Mat(),hist,1,&histSize,&histRange,uniform,accumulate);

The results are normalized by using:

 normalize(hist,hist,1,0,NORM_L1,-1,Mat());

The problem is that my histograms doesn't look like what I was expecting. Any idea on what I am doing wrong or does the problem come from other part of the code (and not calculation of histograms)?

I expect this sparse histogram:

while I get this flat histogram, for same data:

The first hist was calculated in python, but I want to do the same in c++

There is a clustering process before calculating histograms, so if there is no problem with creating histograms then deffinitly the problem comes from before that in clustering part!

来源:https://stackoverflow.com/questions/24404141/opencv-calchist-results-are-not-what-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!