[原創]opencv圖像亮度/對比度調整實驗

[原創]opencv圖像亮度/對比度調整實驗

Author: chad
Mail: [email protected]

亮度和對比度調整實驗
兩種常用的點過程(即點算子),是用常數對點進行 乘法 和 加法 運算:
g(x) = A*f(x) + B

兩個參數 A和 B 一般稱作 增益 和 偏置 參數。我們往往用這兩個參數來分別控制 對比度 和 亮度 。

你可以把 f(x) 看成源圖像像素,把 g(x) 看成輸出圖像像素。這樣一來,上面的式子就能寫得更清楚些:
g(i,j) = A*f(i,j) + B
其中, i 和 j 表示像素位於 第i行 和 第j列 。
降低亮度時保證g(x) < f(x), 增加亮度時g(x) > f(x) .

源碼位置:
opencv-3.0.0-rc1/samples/cpp/tutorial_code/ImgProc/BasicLinearTransforms.cpp
源碼如下 :

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
double alpha; /**< 控制對比度 */
int beta;  /**< 控制亮度 */
int main( int argc, char** argv )
{    
    /// 讀入用戶提供的圖像    
    Mat new_image = Mat::zeros( image.size(), image.type() );    
    /// 初始化    
    cout << "-------------------------" << endl;    
    cout << "* Enter the alpha value [1.0-3.0]: ";    
    cin >> alpha;    cout << "* Enter the beta value [0-100]: ";    
    cin >> beta;    
    new_image(i,j) = alpha*image(i,j) + beta    
    for( int y = 0; y < image.rows; y++ )    {        
        for( int x = 0; x < image.cols; x++ )        {            
                for( int c = 0; c < 3; c++ )            {  
                    new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );           
                    }
                }
        }    
        /// 創建窗口    
        namedWindow("Original Image", 1);    
        namedWindow("New Image", 1);    /// 顯示圖像  
        imshow("Original Image", image);    
        imshow("New Image", new_image);    
        /// 等待用戶按鍵    
        waitKey();    
        return 0;
}

實驗結果如下:

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章