Ask Your Question

how to estimate the (weber) contrast of an image?

asked 2012-08-29 09:49:18 -0500

501 Not Implemented gravatar image

Hi, can someone help me to calculate the contrast of an luminance-image? I've already implement a method to calculate the contrast of color and a approach for the luminance- contrast. I think the weber-contrast is a good solution. Isn't it?

The formula i found on Wikipedia: weber contrast there I representing the luminance of the features and I_b the background luminance.

For my implementation i use JavaCV. The code is:

public double analyse(CvMat input) {
    double contrast = 0;

    // convert to lab to extract luminance channel

    cvCvtColor(input, input, CV_BGR2Lab);

    CvMat background = cvCreateMat(input.rows(), input.cols(), CV_8UC1);
    cvSplit(input, background, null, null, null);

    //calc local background
    cvSmooth(background, background, CV_BLUR, 5);
    JavaCVUtil.showImage(background.asIplImage(), "");
    int width = input.cols();
    int height = input.rows();

    for (int y = 0; y < height; y++) {

        for (int x = 0; x < width; x++) {
            contrast += (input.get(y, x, 0) - background.get(y, x))
                    / background.get(y, x);

    contrast /= (height * width);
    return contrast


Maybe someone can say me what's wrong with this code. For example, for the following image i get a NaN Error: image description


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-08-29 10:46:30 -0500

unxnut gravatar image

Could it be that your background(x,y) luminance gets a value of zero for a certain pixel in the loop where you accumulate contrast. I do not see you checking for that. Thus, if there is a single pixel with a luminance of zero in the image, you will get the exception.

edit flag offensive delete link more


oh yes, you're right. I correct it. thx

501 Not Implemented gravatar image501 Not Implemented ( 2012-08-29 10:53:11 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2012-08-29 09:49:18 -0500

Seen: 1,929 times

Last updated: Aug 29 '12