OpenCV學習:改變圖像的對比度和亮度


  本實例演示簡單地改變圖像的對比度和亮度,使用了如下線性變換來實現像素值的遍歷操作:

  

  The parameters α > 0 and β often called the gain and bias parameters;

  sometimes these parameters are said to control contrast and brightness respectively.

  代碼如下:

// 改變圖像的對比度和亮度  
#include <opencv2/opencv.hpp>  
using namespace cv;
using namespace std;

double alpha; /** < Simple contrast control */
int beta;     /** < Simple brightness control */

int main( int argc, char** argv )
{
    /// 加載圖像
    Mat image = imread( "./Res/James Harden.jpg" );

  /// 目標圖像空間預分配 Mat new_image
= Mat::zeros( image.size(), image.type() ); /// 輸入初始化值 cout <<" Basic Linear Transforms "<<endl; 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(); cin.get(); return 0; }

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM