调整对比度及亮度

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fu18946764506/article/details/78318268
#include<opencv2/opencv.hpp>
using 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<uchar>(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;
}

效果:


展开阅读全文

没有更多推荐了,返回首页