Click here to Skip to main content
15,867,849 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have an image and i want to compute and draw its histogram i use the calchist function but i'm confused about its previous calculations now i'm a new a bout the c function which deal with memory so i need an explain to this code or anew easiest code for calchist function

this is my code:


Mat Histogram::BuildHistogram(Mat srcImage,bool accumulate){
Mat histMat;
//compute histogram
int *c=(int *)calloc(sizeof(int),_channels.size());
for(int i=0;i<_channels.size();i++)
{
c[i]=_channels[i];

}

int *h=(int *)calloc(sizeof(int),_channels.size());
for(int i=0;i<_channels.size();i++)
{
h[i]=_histSize[_channels[i]];

}

float **ranges=(float **)calloc(sizeof(float*),_channels.size());
int size=_channels.size();
for(int i=0;i<size;i++)>
{
float *x=(float *)calloc(sizeof(float),2);
int index=2*_channels[i];
x[0]=_histRange[index];
x[1]=_histRange[index+1];
ranges[i]=x;

}
if(accumulate==true)
_histMat.copyTo(histMat);

calcHist(&srcImage,1,c,_mask,histMat,_channels.size(),h,(const float **)ranges, true, accumulate);
for(int i=0;i<size;i++)>
{
free(ranges[i]);
}
free(ranges);
free(c);
free(h);
//normalize histogram
normalize( histMat, _histMat,1,0,NORM_L1);
Scalar v=cv::sum(_histMat);
if(accumulate==true)
histMat.copyTo(_histMat);
//return histogram
return histMat;
}
Posted

1 solution

There is a very good example for histogram calculation in the openCV documentation. Everything you need should be there:

http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900