OpenCV 调整图像亮度以及对比度
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

const char* win_name = "load image";
const char* output = "contrast and brightness change";
const char* path = "C:/Users/Administrator/Desktop/1.jpg";

int main(int argc, char** argv)
{
    Mat src, dst;
    src = imread(path);
    if (src.empty())
    {
        cout << "could not load..." << endl;
        return -1;
    }
    //改变图像通道数
    //cvtColor(src, src, CV_BGR2GRAY);
    imshow(win_name, src);

    int height = src.rows;
    int width = src.cols;
    dst = Mat::zeros(src.size(), src.type());
    float alpha = 0.8;
    float beta = 30;
    for (int row = 0; row < height; row++)
    {
        for (int col = 0; col < width; col++)
        {
            if (src.channels() == 3)//判断图像是否为3通道
            {
                //获取像素值
                float b = src.at<Vec3b>(row, col)[0];//blue
                float g = src.at<Vec3b>(row, col)[1];//green
                float r = src.at<Vec3b>(row, col)[2];//red
                //操作像素值
                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);
            }
            else if (src.channels() == 1)//判断图像是否为1通道
            {
                float v = src.at<uchar>(row, col);//获取像素值
                dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);//操作像素值
            }
        }

    }

    imshow(output, dst);

    waitKey(0);
    return 0;
}
阅读更多
文章标签: opencv
个人分类: Opencv
想对作者说点什么? 我来说一句

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

不良信息举报

OpenCV 调整图像亮度以及对比度

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭