04-24 00:00
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] Color를 Grayscale로 변환하기 본문

Programming/OpenCV

[OpenCV] Color를 Grayscale로 변환하기

cinema4dr12 2016. 8. 7. 22:45

이번 포스팅에서는  cvtColor() OpenCV 함수를 이용하여 컬러 이미지를 그레이스케일 이미지로 변환하는 방법에 대하여 알아보겠습니다.


cvtColor() Prototype은 다음과 같습니다:


void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
  • src – 입력 이미지: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), 또는 single-precision floating-point.

  • dst – 입력 이미지와 동일한 크기와 뎁스(depth)의 출력 이미지.

  • code – 컬러 공간 변환 코드.

  • dstCn – 최종 이미지의 채널 수; 파라미터가 0이라면 채널 수는 자동으로 srccode로부터 계산된다.

Example 1 - WebCam 입력 영상을 흑백으로 변환하기

WebCam으로부터 입력받은 컬러 이미지를 흑백(Grayscale)로 변환하는 코드입니다.


WebCamToGrayscale.cpp

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <opencv2/opencv.hpp>
#include <stdlib.h>
 
using namespace cv;
 
//////////////////////////////////////////////////////////////////
// function : main
int main( int argc, char** argv )
{
    // frame capture from webcam
    cv::VideoCapture capture( 0 );
  
    // is camera opened?
    if!capture.isOpened() ) {
        std::cerr << "Cannot open camera" << std::endl;
        return 0;
    }
 
    // create a window
    cv::namedWindow( "Webcam"1 );
 
    // while loop for image capture from webcam
    whiletrue ) {
        bool frame_valid = true;
  
        cv::Mat frame;
        cv::Mat gray;
  
        try {
            // get a new frame from webcam
            capture >> frame;
 
            // convert RGB image to gray
            cv::cvtColor( frame, gray, CV_RGB2GRAY );
        }
        catch( cv::Exception& e ) {
            std::cerr << "Exception occurred. Ignoring frame... " << e.err << std::endl;
            frame_valid = false;
        }
 
        if ( frame_valid ) {
            try {
                // print the output
                cv::imshow( "Webcam", gray );
            }
            catch( cv::Exception& e ) {
                std::cerr << "Exception occurred. Ignoring frame... " << e.err << std::endl;
            }
        }
 
        if ( cv::waitKey( 30 ) >= 0 ) break;
    }
 
 
    return 0;
}
cs


Line 34에서 Web Cam을 통해 입력된 Color Image(frame)를 Grayscale Image(gray)로 변환합니다. 출력 결과는 아래 이미지와 같습니다.


Result

Example 2 - Color Image to Grayscale

아래의 Color Image를 Grayscale Image로 변환해 보겠습니다.



ColorImageToGrayscale.cpp

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
#include "opencv2/opencv.hpp"
 
using namespace std;
using namespace cv;
 
 
/*/////////////////////////////////////
@ function: main
*//////////////////////////////////////
int main()
{
    /// Read 8-bit grayscale image
    cv::Mat image = cv::imread( "{YOUR_IMAGE_PATH}/frozen.jpg", CV_LOAD_IMAGE_COLOR );
 
    if!image.data) {
        cout <<  "Could not open or find the image" << std::endl;
        return -1;
    }
 
    // Create a new matrix to hold the gray image
    cv::Mat gray;
 
    // convert RGB image to gray
    cv::cvtColor( image, gray, CV_BGR2GRAY );
 
    cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );  
    cv::imshow( "Display window", image );                 
 
    cv::namedWindow( "Result window", CV_WINDOW_AUTOSIZE );   
    cv::imshow( "Result window", gray );
 
    cv::waitKey( 0 );                                          
    return 0;
}
cs


Result


Comments