Reputation: 5342
I want to obtain the average values of a MAT and MatND variable, just to estimate the sharpness and brightness. However, I have been facing real issues with the vague values I have been encountering. I tried my best, but am still confused. I really do not know, if am doing the right thing.
calcHist(&src_yuv,1,channels,Mat(),hist,1,histSize,ranges,true,false);
Size d = hist.size();
rows = d.height;
cols = d.width;
for(int k=0;k<hbins;k++)
{
for(int l=0;l<sbins;l++)
{
total = total + hist.at<float>(k,l);
}
}
brightness = total/(rows*cols);
Here , am trying to calculate the histogram of the luma channel of src_yuv, which is in YUV format and average the values. Am I doing it the right way? If I change the datatype within <' '>, ranging from uchar to long int, am ending up with different values, which is understandable. But I dunno which is the right data type to use. Moreover, should I loop it within hbins,sbins or rows, cols? Please help me. am stuck at this for a long time.
Laplacian(src_gray,dst,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
Size s = dst.size();
rows = s.height;
cols = s.width;
total = 0;
max = 0;
for(int k=0;k<rows;k++)
{
for(int l=0;l<cols;l++)
{
total = total + dst.at<>(k,l);
}
}
average = total/(rows*cols);
What is the exact way to compute average in the above case? Could you please help me here? I tried different datatypes, starting from in for the mat, and long int for the total and averages. Its a gray scale image, and the result in the laplacian convoluted image.
Upvotes: 0
Views: 584
Reputation: 17015
convert the input src_yuv to BGR before calcHist and you will get the desired output with the same code.
EDIT: for YUV:-
out = imread("Lena.jpg");
out.convertTo(out, CV_RGB2YCrCb);
MatND hist;
int hbins = 30, sbins = 32;
int histSize[] = {hbins, sbins};
float hranges[] = { 0, 180 };
float sranges[] = { 0, 256 };
int channels[] = {0,1,2};
const float* ranges[] = { hranges, sranges };
calcHist( &out, 1, channels, Mat(),
hist, 2, histSize, ranges,
true,
false );
Size d = hist.size();
int rows = d.height;
int cols = d.width;
float total;
float brightness;
for(int k=0;k<hbins;k++)
{
for(int l=0;l<sbins;l++)
{
total = total + hist.at<float>(k,l);
}
}
brightness = total/(rows*cols);
this gives me brightness to be 246.895
Upvotes: 1