Study/OpenCV 명암도 영상 변환한 결과 영상 보기 코딩하는 야구쟁이 2011. 9. 8. 14:39 // ConsoleCV.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "ConsoleCV.h" // OpenCV 관련 헤더파일 #include #include #include #include #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; }