05-09 00:07
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] 이미지 Contrast & Brightness 조정하기 본문

Programming/OpenCV

[OpenCV] 이미지 Contrast & Brightness 조정하기

cinema4dr12 2015. 3. 17. 00:33

Brightness & Contrast 조정

\( g(x) = \alpha f(x) + \beta \)


where


\(f(x)\): Source Image Pixels,

\(g(x)\): Output Image Pixels

\(\alpha\): Gain( > 0), \(\beta\): Bias


이를 픽셀 단위로 풀어 쓰면,


\( g(i,j) = \alpha f(i,j) + \beta \)


i : 행, j : 열

Example Code

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 "stdafx.h"
#include "opencv2/opencv.hpp"
 
using namespace cv;
 
int _tmain(int argc, _TCHAR* argv[])
{
    double alpha; /**< Simple contrast control */
    int beta; /**< Simple brightness control */
 
    Mat image = imread( "../landscapes-267a.jpg" );
    Mat new_image = Mat::zeros( image.size(), image.type() );
 
    /// Initialize values
    alpha = 0.2;
    beta = 50;
 
    /// Do the operation new_image(i,j) = alpha*image(i,j) + beta
    forint y = 0; y < image.rows; y++ ) {
        forint x = 0; x < image.cols; x++ ) {
            forint c = 0; c < 3; c++ ) {
                new_image.at<Vec3b>( y, x )[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>( y, x )[c] ) + beta );
            }
        }
    }
 
    /// Create Windows
    namedWindow( "Original Image"1 );
    namedWindow( "New Image"1 );
 
    /// Show stuff
    imshow( "Original Image", image );
    imshow( "New Image", new_image );
 
    /// Wait until user press some key
    waitKey();
    return 0;
}
cs


■ Mat::zeros( image.size(), image.type() )
Image와 동일한 크기와 타입으로 메모리 확보.


saturate_cast
유효한 픽셀 값 보장을 위함. 픽셀값 범위는 [0, 255]이므로 이를 넘어서는 범위는 자동으로 클램핑(Clamping).

사용된 이미지


Result


\(\alpha = 0.2, \beta = 50\)


Original


Adjusted

Comments