# Revision history [back]

### how to estimate the (weber) contrast of an 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: 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);

}
}
//normalize
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:

greetings