随笔 - 632  文章 - 17  评论 - 54  阅读 - 94万

OpenCV利用像素点操作调整图像亮度

一、概述

  示例代码:利用简单的数学元素提升图像的亮度。

二、示例图像

  

 

 

三、代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//图像像素点操作
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
int main(int argc, char const *argv[])
{
    Mat input,output;
    input = imread("girl.jpg");
    if(!input.data){
        cout << "can't not found image"<<endl;
        return -1;
    }
    imshow("input",input);
    //获取图片的宽高
    int height = input.rows;
    int width = input.cols;
    double alpha = 1.2;
    double beat =50;
    //按照原图的大小创建一个输出图像
    output = Mat::zeros(input.size(),input.type());
    //以下代码的意思是将input中的像素值取出,然后再经由数学运算赋值到output中
    for(int y=0;y<height;y++){
        for(int x = 0 ;x<width;x++){
            //blue,saturate_cast<uchar>(value)确保值大小范围为0~255之间
            output.at<Vec3b>(y,x)[0] = saturate_cast<uchar>(alpha*input.at<Vec3b>(y,x)[0]+beat);
            //green
            output.at<Vec3b>(y,x)[1] = saturate_cast<uchar>(alpha*input.at<Vec3b>(y,x)[1]+beat);
            //red
            output.at<Vec3b>(y,x)[2] = saturate_cast<uchar>(alpha*input.at<Vec3b>(y,x)[2]+beat);
        }
    }
 
    imshow("output",output);
    waitKey(0);
    return 0;
}

  

posted on   飘杨......  阅读(345)  评论(0)    收藏  举报

点击右上角即可分享
微信分享提示