代码:
#includeusing namespace cv;int brightness = 5;int contrast = 5;Mat src;Mat brightnessContrast;void BrightnessContrastFilter(int, void*){ float bri = brightness / 5.0f; float cont = contrast / 5.0f; //增亮图像 cv::Mat bright = src*(bri); //平均亮度 float sum = 0; for (int i = 0; i < bright.rows; i++){ for (int j = 0; j < bright.cols; j++){ sum += bright.at (i, j); } } float aver = sum / (bright.cols*bright.rows); cv::Mat averImg = cv::Mat::ones(bright.rows, bright.cols, bright.type()); averImg *= aver; //根据contrast进行插值 brightnessContrast = (bright - averImg)*cont + averImg; imshow("brightnessContrast", brightnessContrast);}int main(){ src = imread("densityGray.png", 0); imshow("src", src); namedWindow("brightnessContrast"); createTrackbar("brightness", "brightnessContrast", &brightness, 20, BrightnessContrastFilter); createTrackbar("contrast", "brightnessContrast", &contrast, 20, BrightnessContrastFilter); BrightnessContrastFilter(0, 0); imshow("brightnessContrast", brightnessContrast); waitKey(); destroyAllWindows(); return 0;}
效果: