#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 image = imread( argv[1] ); 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(); return 0; }
- 爲了訪問圖像的每一個像素,我們使用這一語法: image.at<Vec3b>(y,x)[c] 其中, y 是像素所在的行, x 是像素所在的列, c是R、G、B(0、1、2)之一。
- 因爲
的運算結果可能超出像素取值範圍,還可能是非整數(如果
是浮點數的話),所以我們要用 saturate_cast對結果進行轉換,以確保它爲有效值。
Note
我們可以不用 for 循環來訪問每個像素,而是直接採用下面這個命令:
image.convertTo(new_image, -1, alpha, beta);
convertTo 將執行我們想做的 new_image = a*image + beta 。然而,我們想展現訪問每一個像素的過程,所以選用了for循環的方式。實際上,這兩種方式都能返回同樣的結果。