issue with mat::at<type>
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 .
I see a potential problem is the normalize functions. You pass Mat() as the last parameter, but the indication that there is no mask is noArray(). This may treat all the values as 0 and not change things. I don't notice any other problems, except you don't ever dealloc valuesb.
So try that and see if it works.
valuesb is not global variable so with termination of function it will be deallocated automatically.. and also I investigated sum+=num for loop<(size-1) then it is working fine giving a genuine output means histg.at<float>(buckets-1); is returning some unsusal garbage value in the last .do you think so?
That's not how malloc works. The new keyword doesn't either. You have to manually delete it.
That's possible. Try printing it out and checking it manually. Although it would be histg.at<float>(buckets), not buckets-1 that causes the problem. Could you also double check that the histogram size is actually equal to histSize? It should be, but best to check.