# What is the best way to calculate the Weber ratio for an image?

I am new to openCV and am taking a class in digital image processing. I am working through prior years assignment in preparation for our upcoming assignment.

1. Write a program to display information about an image. The information we are interested in is the dimensions of the image, its average gray scale intensity, its Weber ratio, and its format (gif/jpeg/tiff). If the input image is color, convert it to grayscale before computing the above attributes, and output a message that the input image is in color.

Is there a better way to calculate the Weber Ratio? I have used the following code so far to calculate the mean overall intensity, should I take absolute value |meanval - each point intensity| divided by meanval to get this ratio?:

CvSize dim = cvGetSize(image);
Mat img(image);
Mat grayimg;
imgtype = img.type();
chanl = img.channels();

cout <<  "The image type is: " << imgtype << endl;
if (chanl == 3)
{
cout << "The original image was in color. Channels = :" << chanl << endl;
cvtColor( img, grayimg, CV_BGR2GRAY );
}
else
{
grayimg = img;
}
Scalar meanval = mean(grayimg,noArray());

edit retag close merge delete

"should I take absolute value |meanval - each point intensity| divided by meanval to get this ratio?" - sounds good ;) try and come back.. btw, docs

1

Auwch CvSize dim = cvGetSize(image); you are starting to learn OpenCV then leave every function with a Cv in front alone and jump to its C++ counterside! It will spare you HEAPS of troubles in the long run.

1

Remember, your professor may be on this forum as well. And he probably gave you example code using the C++ interface.

Sort by » oldest newest most voted

Would someone look over the following code and screen capture to see if I am calculating the Weber ratio correctly? I was very confused at first mixing cv1 and cv2. Pound signs and brackets removed on include

include opencv\cv.h

include opencv\highgui.h

using namespace cv; using namespace std;

int main() { char infilestr; // input file name

int imgtype;                            // image type

int chanl;                          // number of channels

int imgdepth;
Mat img;
int diff = 0;
float weber = 0;
int totpixels = 0;
float totweber = 0;

while (!img.data)
{
cout << "Please enter the name of the image file you wish to open:" << endl;
cin.getline(infilestr, 255, '\n');
if (!img.data)                          // check for valid image
{
cout << "The image file you specified could not be loaded, try again." << endl;
}
}                                           // end while

Mat::MSize dim = img.size;
Mat grayimg;
imgtype = img.type();
chanl = img.channels();
imgdepth = img.depth();

cout <<  "The image type is: " << imgtype << endl;
cout << "The image depth is: " << imgdepth << endl;
if (chanl == 3)
{
cout << "The original image was in color. Channels = :" << chanl << endl;
cvtColor( img, grayimg, CV_BGR2GRAY );
}
else
{
grayimg = img;
}
Scalar meanval = mean(grayimg,noArray());

Mat Web(img);
int height = img.rows;
int width = img.cols * 3;
int step = img.step;
int h = grayimg.rows;
int w = grayimg.cols;

for( int y=0; y < h; y++ )
{
uchar* inp= grayimg.ptr<uchar>(y);
for( int x=0; x < w; x++ )
{
diff = abs(meanval(0) - inp[x]);
weber = diff/meanval(0);
totweber += weber;
totpixels++;
}
}

totweber = totweber / totpixels;
cout << "The average intensity of the grayscale image is: " << meanval(0) << endl;
cout << "The average weber ratio for the grayscale image is: " << totweber << endl;
namedWindow("Example1", CV_WINDOW_NORMAL);
imshow("Example1", grayimg);
waitKey(0);
return 0;


} more

1

hey, good ;)

i'd say you solved it. note, that you don't even have to iterate over the pixels to do that:

Mat grayf;
grayimg.convertTo(grayf,CV_32F);

Scalar meanval = mean(grayimg);

Mat weber = abs(grayf - meanval) / meanval(0);
double ratio = (sum(weber)(0) / weber.total());


and some small things: use Size dim = img.size() instead of Mat::MSize dim = img.size;

1

Thanks, I will rewrite for the real assignment since it will probably be changed. The old O'Reilly book uses cv1 and the online documentation used both cv1 and cv2. At least I can now access my way through the image pixel by pixel. Many more interesting things to come. Thanks loads for your help!

Official site

GitHub

Wiki

Documentation