码迷,mamicode.com
首页 > 其他好文 > 详细

灰度图像亮度对比度调整的简单代码

时间:2016-06-30 21:22:21      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

代码:

#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;
}

效果:

技术分享

技术分享

 

灰度图像亮度对比度调整的简单代码

标签:

原文地址:http://www.cnblogs.com/bluebean/p/5631159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!