Ask Your Question
0

issue with mat::at<type>

asked 2018-01-24 02:55:47 -0600

Shivanshu gravatar image

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 .

edit retag flag offensive close merge delete

Comments

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.

Tetragramm gravatar imageTetragramm ( 2018-01-24 17:48:33 -0600 )edit

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?

Shivanshu gravatar imageShivanshu ( 2018-01-26 00:57:35 -0600 )edit

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.

Tetragramm gravatar imageTetragramm ( 2018-01-27 15:05:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-27 15:10:22 -0600

Tetragramm gravatar image

Ah! I noticed the real problem.

You store values in valuesb[0] through valuesb[histSize-2]. Because count = buckets-1, and buckets goes to histSize-1. So when you call find_mean, it looks at every value, including valuesb[histSize-1], which you never actually set.

If you're trying to find the mean of histg, why not use the cv::mean function? Or since you're actually calculating the sum+1, why not use cv::sum?

edit flag offensive delete link more

Comments

I didnt intend to use inbuilt function frequently because,Every redymade functions in opencv will hide all those stuff which I must learn.See I tried to build a mean finding function and it seemed to be easy .but I face an error soon ,and so I learned new thing,new knowledge about it which I was going to miss while using inbuild function.Dont you think so?Using a searching function and building a searching function are two different things totally.at first end you are using a readymade thing without using your brain actually and in other your brain is used totally,and if this happen there is more chance of learning and gaining skill

Shivanshu gravatar imageShivanshu ( 2018-02-02 09:21:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-24 02:55:47 -0600

Seen: 160 times

Last updated: Jan 27 '18