This is my code to cfalculate histogram..
void histogram(Mat src,char name[512])
{
Mat histg,histb,histr;
vector<Mat>channel;
split(src,channel);
calcHist( &channel[0], 1, 0, Mat(),histg, 1, &histSize,&range, true,false);
calcHist( &channel[1], 1, 0, Mat(),histb, 1, &histSize,&range, true,false);
calcHist( &channel[2], 1, 0, Mat(),histr, 1, &histSize,&range, true,false);
valuesb=(float*)malloc(histSize*sizeof(float)); //array allocated(1)
int hist_w=512; int hist_h =500;
int bin_w = cvRound( (double) hist_w/histSize );
Mat hist_mat(hist_w,hist_h,CV_8UC3,Scalar(0,0,0));
Mat hist_matg(hist_w,hist_h,CV_8UC3,Scalar(0,0,0));
Mat hist_matr(hist_w,hist_h,CV_8UC3,Scalar(0,0,0));
normalize(histg, histg, 0, hist_mat.rows, NORM_MINMAX, -1, Mat() );
normalize(histr, histr, 0, hist_mat.rows, NORM_MINMAX, -1, Mat() );
normalize(histb, histb, 0, hist_mat.rows, NORM_MINMAX, -1, Mat() );
int count=0;
for(int buckets=1,count=0;buckets<(histSize);buckets++)
{
line(hist_mat,Point(bin_w*(buckets-1),hist_h-cvRound(histg.at<float>(buckets-1))),Point(bin_w*(buckets),hist_h-cvRound(histg.at<float>(buckets))),Scalar(0,0,255),1);
line(hist_matg,Point(bin_w*(buckets-1),hist_h-cvRound(histb.at<float>(buckets-1))),Point(bin_w*(buckets),hist_h-cvRound(histb.at<float>(buckets))),Scalar(0,255,0),1);
line(hist_matr,Point(bin_w*(buckets-1),hist_h-cvRound(histr.at<float>(buckets-1))),Point(bin_w*(buckets),hist_h-cvRound(histr.at<float>(buckets))),Scalar(255,0,0),1);
valuesb[count]=histg.at<float>(buckets-1); //values feeded to array(2)
count++;
}
int m=find_mean(valuesb,histSize);//function called(3)
}
and this is find_mean function..
int find_mean(float arr[],int size)
{
float sum=1.0;
for(int loop=0;loop<size;loop++)
{
float num=arr[loop];
sum+=num;
}
printf("%f\n",(float)sum);
return 0;
}
when I am printing the value of 'sum' variable after loop it is like '-4758485.485' but before loop it is fine I dont know why sum+=num working correctly.Please dont dislike, I dont know how shall i explain my problem more clear than this .Please read the code patiently .