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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// ConsoleCV.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "ConsoleCV.h"
 
// OpenCV 관련 헤더파일
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvcam.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
 
CWinApp theApp;
 
using namespace std;
 
 
void imageViewer();
char *getFileDialog();
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
     
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed") << endl;
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.
        imageViewer( );
    }
     
    return nRetCode;
}
 
void imageViewer( )
{
    IplImage *color_image, *gray_image;
    // STEP 1 : 영상 파일 선택
    char *readFileName = getFileDialog();
    if( !readFileName ) return;
    // STEP 2 : 컬러 영상을 읽는다.
    color_image = cvLoadImage( readFileName, CV_LOAD_IMAGE_COLOR );
    // STEP 3 : 읽기 성공이면
    if( color_image != 0 ) {
        // 캡션 제목이 title1 을 갖는 창을 생성
        char *title1 = "First Image Viewer using OpenCV";
        cvNamedWindow( title1, CV_WINDOW_AUTOSIZE );
        // 생성된 창에 image 객체를 뿌려준다.
        cvShowImage( title1, color_image );
        // 변환 결과를 담을 명암도 영상을 초기화 한다.
        gray_image = cvCreateImage( cvGetSize(color_image),
            IPL_DEPTH_8U, 1 );
        // 컬러 영상을 명암도 영상으로 변환한다.
        cvCvtColor( color_image, gray_image, CV_BGR2GRAY );
        // 캡션 제목이 title2 을 갖는 창을 생성
        char *title2 = "First Gray Viewer using OpenCV";
        cvNamedWindow( title2, CV_WINDOW_AUTOSIZE );
        // 생성된 창에 image 객체를 뿌려준다.
        cvShowImage( title2, gray_image );
        cvWaitKey(0); // 키보드 입력을 기다린다.
        cvReleaseImage( &color_image ); // 할당된 리소스를 해제한다.
        cvReleaseImage( &gray_image );
        cvDestroyWindow( title1 );
        cvDestroyWindow( title2 ); // 창을 닫는다.
    }
    else
        printf("[ERROR] 영상 파일을 읽어올 수 없습니다.\n");
    return;
}
 
// 파일 대화상자에서 갖고 온다.
char *getFileDialog()
{
    char *readFileName = (char *)NULL;
    // 파일 포맷
    char szFilter[] = "지원 영상 파일(*.bmp, *.jpg, *.gif, *.png, *.tif) \
        |*.bmp;*.jpg;*.gif;*.png;*.tif||";
    printf("\n");
    printf("\t * 파일 대화상자를 이용하여 영상 파일을 선택하십시오.\n\n");
        printf("\t \n");
    CFileDialog fileDlg(TRUE, NULL, NULL,
        OFN_EXPLORER|OFN_HIDEREADONLY, szFilter);
    if( fileDlg.DoModal() == IDOK )
    {
        // CString to char *
        readFileName
            = (char *)strdup((char *)(LPCTSTR)fileDlg.GetPathName());
    }
    return readFileName;
}
Posted by 코딩하는 야구쟁이
,