opencv提高图像对比度

// 提高图片亮度和对比度.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace  cv;


int main()
{   
    Mat src,dest;
    src = imread("test.jpg");

    namedWindow("src", CV_WINDOW_AUTOSIZE);
    imshow("src", src);

    dest = Mat::zeros(src.size(), src.type());
    int width = src.cols;
    int height = src.rows;
    int channels = src.channels();

    int alphe = 1.8; //(alphe > 1)
    int beta = -30;// 负数对比度越高
    Mat m1;
    src.convertTo(m1, CV_32F); //将原始图片数据(CV_8U类型)转换成CV_32类型,以提高操作的精度
    for (int row = 0; row < height;row++) {
        for (int col = 0; col < width; col++) {
            if (channels == 3) { //对于3通道
                float b = m1.at(row,col)[0];
                float g = m1.at(row, col)[1];
                float r = m1.at(row, col)[2];

                dest.at(row, col)[0] = saturate_cast(alphe * b + beta);
                dest.at(row, col)[1] = saturate_cast(alphe * g + beta);
                dest.at(row, col)[2] = saturate_cast(alphe * r + beta);
            }
            else if (channels == 1){ //对于单通道
                int pix = src.at(row, col);
                dest.at(row,col) = saturate_cast(alphe * pix + beta);
            }
        }
    }
    namedWindow("change");
    imshow("change",dest);
    waitKey();

    return 0;
}

你可能感兴趣的:(opencv)