Ask Your Question

Revision history [back]

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[255]; // 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');
    img = imread(infilestr);
    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;

} image description