#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;

int main()
{
    Mat src1,dst,dst2;
    //原图
    src1 = imread(".//pic//kate.png",IMREAD_UNCHANGED);

    if (!src1.data)
    {
        cout << "load error" << endl;
        return -1;
    }

    char input_win[] = "imput image";
    namedWindow(input_win , CV_WINDOW_AUTOSIZE);
    imshow(input_win, src1);

    int height = src1.rows;
    int width = src1.cols;
    dst = Mat::zeros(src1.size(), src1.type());
    dst2 = Mat::zeros(src1.size(), src1.type());

    float alpha = 1.5;//对比度
    float beta = 10;//亮度

    Mat m1;
    src1.convertTo(m1, CV_32F);
    dst2.convertTo(dst2, CV_32F);
    for (int row = 0; row < height; row++)
    {
        for (int col = 0; col < width; col++)
        {
            if (src1.channels() == 3)
            {


                float b = src1.at<Vec3b>(row, col)[0];
                float g = src1.at<Vec3b>(row, col)[1];
                float r = src1.at<Vec3b>(row, col)[2];

                float b2 = m1.at<Vec3f>(row, col)[0];
                float g2 = m1.at<Vec3f>(row, col)[1];
                float r2 = m1.at<Vec3f>(row, col)[2];

                dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta);
                dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g * alpha + beta);
                dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r * alpha + beta);

                dst2.at<Vec3f>(row, col)[0] = saturate_cast<uchar>(b2 * alpha + beta);
                dst2.at<Vec3f>(row, col)[1] = saturate_cast<uchar>(g2 * alpha + beta);
                dst2.at<Vec3f>(row, col)[2] = saturate_cast<uchar>(r2 * alpha + beta);
            }
            else if (src1.channels() == 1)
            {
                float v = src1.at<uchar>(row, col);
                dst.at<uchar>(row, col) = saturate_cast<uchar>(v * alpha + beta);
            }
        }
    }
    imshow("提高对比度", dst);
    imshow("提高对比度(float)", dst);

    waitKey(0);
    return 0;
}
12-16 04:25