OpenCV Tutorials Dongchul Kim Media System Lab., Yonsei University http://msl.yonsei.ac.kr e-mail: dongchul@gmail.com 2009. 12
Contents Introduction Tutorials Reference About OpenCV : 5W1H Overview 기본 셋업 사진 불러와서 이진화 하기 카메라 연결하여 실시간 이진화 하기 이진화 조절 슬라이드바 달기 모션 검출기 Reference
The Birth of OpenCV 비전 기반 최적화된 소스로 비전 연구의 발전을 도모 이식성 좋고, 최적화된 성능으로 비전 기반 상업의 발전 많은 사람들이 같은 작업을 반복하는 것을 방지 상업용이라고 할지라도 소스코드 공개 의무가 없는 라이센스 프리 보다 읽기 쉽고 공유하기 쉬운 소스 코드 작성을 유도 The Birth of OpenCV
Introduction About OpenCV Who : Shinn Lee, Intel Performance Library Team Vadim Pisarevsky, Victor Eruhimov, Valery Kuriakin When : 1999. 1. alpha ~ 2009. 2.0 beta Where : Intel What : Open Source Computer Vision Library, OpenCV Why : Fast, Easy, License free.. How : C/C++, Optimized Intel CPU
OpenCV Overview More than 500 algorithms Basic structure and operation Image Analysis Object Recognition Motion Analysis and Object Tracking 3D Reconstruction
OpenCV Overview Image data manipulation Image and video I/O allocation, release, copying, setting, conversion Image and video I/O file and camera based input, image/video file output Matrix and vector manipulation, and linear algebra routines products, solvers, eigenvalues, SVD Various dynamic data structures lists, queues, sets, trees, graphs Basic image processing filtering, edge detection, corner detection, sampling and interpolation, color conversion, morphological operations, histograms, image
OpenCV Overview Structural analysis Camera calibration Motion analysis connected components, contour processing, distance transform, various moments, template matching, Hough transform, polygonal approximation, line fitting, ellipse fitting, Delaunay triangulation Camera calibration finding and tracking calibration patterns, calibration, fundamental matrix estimation, homography estimation, stereo correspondence Motion analysis optical flow, motion segmentation, tracking Object recognition eigen-methods, HMM
Learning OpenCV, O’Reilly Media
http://en.wikipedia.org/wiki/Computer_vision
OpenCV FAQ OpenCV를 처음 사용하시는 분들께 by 썬샤인(wrldvsn) http://cafe.naver.com/opencv/809 OpenCV 관련 어떤 질문이 많이 올라왔을까? by 미장이(jinhoy97) http://cafe.naver.com/opencv/5162 OpenCV에 목숨걸지 말자 by 아메톤(ameton) http://cafe.naver.com/opencv/887 템플릿 매칭(Template Matching) 강좌 및 소스 by 티구티구(t9t9) http://t9t9.com/37 허프 변환(Hough Transform) 강좌 및 소스 by 티구티구(t9t9) http://t9t9.com/113 이진화(Thresholding) 강좌 및 소스 by 티구티구(t9t9) http://t9t9.com/110 OpenCV로 키보드 입력 처리 소스 by 티구티구(t9t9) http://t9t9.com/115 윤곽(contour) 추출, 에지(edge) 추출 by 티구티구(t9t9) http://t9t9.com/112 카메라 두대 연결 소스 by 티구티구(t9t9) http://cafe.naver.com/opencv/7423 전체화면(fullscreen) 띄우기 소스 by 티구티구(t9t9) http://cafe.naver.com/opencv/9263
Tutorials
OpenCV Install http://sourceforge.net/projects/opencvlibrary
OpenCV Settings OpenCV 1.0, 1.1 Library: Project > Setting > Link cv.lib cvaux.lib highgui.lib cxcore.lib Directories Include C:\PROGRAM FILES\OPENCV\CV\INCLUDE C:\PROGRAM FILES\OPENCV\CVAUX\INCLUDE C:\PROGRAM FILES\OPENCV\OTHERLIBS\HIGHGUI C:\PROGRAM FILES\OPENCV\CXCORE\INCLUDE Library C:\PROGRAM FILES\OPENCV\LIB DLLs copy C:\PROGRAM FILES\OPENCV\bin\*.dll to C:\WINDOWS\SYSTEM32
OpenCV Settings OpenCV 1.2 (2.0 beta) Library: Project > Setting > Link cv120.lib cvaux120.lib highgui120.lib cxcore120.lib Directories Include C:\PROGRAM FILES\OPENCV1.2\INCLUDE Library C:\PROGRAM FILES\OPENCV1.2\LIB DLLs copy C:\PROGRAM FILES\OPENCV\BIN\*.dll to C:\WINDOWS\SYSTEM32
OpenCV Overview CXCORE 그리기, 문자열, 기초 수학 관련 자료구조, 행렬연산, 자료변환, 메모리 관리, 에러처리, 동적 로딩 등 CV 영상처리, 구조분석, 움직임, 추적, 패턴인식, 카메라 보정 등 ML Clustering, Classification, 자료분석 HIGHGUI 영상 화면 출력, 키보드 입력 카메라 연결 (VFW 기반) CVCAM 카메라 연결 인터페이스 (DX 기반) 1.1 이후부터 HighGui에 통합됨
IplImage B G R ... width = 320, height = 240 nChannels = 3, depth = 8 typedef struct _IplImage{ int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI *roi; struct _IplImage *maskROI; void *imageId; struct _IplTileInfo *tileInfo; int imageSize; char *imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char *imageDataOrigin; } IplImage; B G R ... width = 320, height = 240 nChannels = 3, depth = 8
OpenCV Tutorials 사진 불러와서 이진화 하기 Load Image file Thresholding IplImage cvCreateImage cvLoadImage Thresholding cvThreshold Connect Camera cvCapture Control Slidebar cvCreateTrackbar Frame Difference cvAbsDiffz 사진 불러와서 이진화 하기 카메라 연결하여 실시간 이진화 하기 임계값 조절 슬라이드바 달기 움직임 검출하기 (Motion Detect)
Load Image file and Thresholding Threshold Control Slide bar Motion Detect
cvThreshold #include "stdafx.h" #include "opencv/cv.h" #include "opencv/cxcore.h" #include "opencv/highgui.h" #pragma comment(lib, "cv120.lib") #pragma comment(lib, "cxcore120.lib") #pragma comment(lib, "highgui120.lib") int _tmain(int argc, _TCHAR* argv[]) { IplImage* img = cvLoadImage("input.jpg"); IplImage* img_gray = cvCreateImage( cvGetSize(img), 8, 1); cvCvtColor(img, img_gray, CV_RGB2GRAY); cvThreshold( img_gray, img_gray, 128, 255, CV_THRESH_BINARY ); cvNamedWindow("test"); cvShowImage("test", img); cvNamedWindow("bin"); cvShowImage("bin", img_gray); cvWaitKey(0); return 0; } OpenCV Library 기본 설정들 Load image file Convert Color Thresholding Create Window & Show Image
cvCaptureFromCam Connect Camera Camera Setting // OpenCV 기본 헤더 셋업 int _tmain(int argc, _TCHAR* argv[]) { IplImage* img = 0; IplImage* img_gray = 0; CvCapture* cap = cvCaptureFromCAM(0); cvNamedWindow("cam"); cvNamedWindow("bin"); cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_WIDTH, 640); cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_HEIGHT, 480); for(;;) img = cvRetrieveFrame( cap ); if(!img_gray) img_gray = cvCreateImage( cvGetSize(img), 8, 1); } cvCvtColor( img, img_gray, CV_RGB2GRAY); cvThreshold( img_gray, img_gray, 128, 255, CV_THRESH_BINARY); cvShowImage("cam", img); cvShowImage("bin", img_gray); char ch = cvWaitKey(30); if(ch == 27) break; cvReleaseCapture(&cap); return 0; Connect Camera Camera Setting
cvCreateTrackbar Camera Connect & Setting Trackbar Keyboard Event // OpenCV 기본 헤더 셋업 int _tmain(int argc, _TCHAR* argv[]) { int T = 128; IplImage* img = 0; IplImage* img_gray = 0; CvCapture* cap = cvCaptureFromCAM(0); cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_WIDTH, 640); cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_HEIGHT, 480); cvNamedWindow("cam"); cvNamedWindow("bin"); cvCreateTrackbar("T", "bin", &T, 255, NULL); for(;;) img = cvRetrieveFrame( cap ); if(!img_gray) img_gray = cvCreateImage( cvGetSize(img), 8, 1); } cvCvtColor( img, img_gray, CV_RGB2GRAY); cvThreshold( img_gray, img_gray, T, 255, CV_THRESH_BINARY); cvShowImage("cam", img); cvShowImage("bin", img_gray); char ch = cvWaitKey(30); if(ch == 27) break; cvReleaseCapture(&cap); cvReleaseImage(&img_gray); return 0; Camera Connect & Setting Trackbar Keyboard Event
cvAbsDiff Camera Connect & Setting Frame Difference Keyboard Event // OpenCV 기본 헤더 셋업 int _tmain(int argc, _TCHAR* argv[]) { int T = 128; IplImage *img = 0; IplImage *img_gray = 0, *img_gray_prev = 0; IplImage *img_diff = 0; CvCapture* cap = cvCaptureFromCAM(0); cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_WIDTH, 640); cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_HEIGHT, 480); cvNamedWindow("cam"); cvNamedWindow("diff"); for(;;) img = cvRetrieveFrame( cap ); if(!img_gray) img_gray = cvCreateImage( cvGetSize(img), 8, 1); img_gray_prev = cvCreateImage( cvGetSize(img), 8, 1); img_diff = cvCreateImage( cvGetSize(img), 8, 1); } cvCvtColor( img, img_gray, CV_RGB2GRAY ); cvAbsDiff( img_gray_prev, img_gray, img_diff ); cvShowImage("cam", img); cvShowImage("diff", img_diff); char ch = cvWaitKey(30); if(ch == 27) break; else if(ch == ' ') cvSaveImage( "output.jpg", img_diff ); cvCopyImage( img_gray, img_gray_prev ); cvReleaseCapture(&cap); cvReleaseImage(&img_gray); cvReleaseImage(&img_gray_prev); return 0; Camera Connect & Setting Frame Difference Keyboard Event
Reference 국내 커뮤니티, http://www.opencv.co.kr OpenCV Library Download, http://sourceforge.net/projects/opencvlibrary/ OpenCV Wiki, http://opencv.willowgarage.com/wiki OpenCV Yahoo Groups, http://tech.groups.yahoo.com/group/OpenCV Computer Vision, http://en.wikipedia.org/wiki/Computer_vision OpenCV 저자 이문호씨 웹사이트, http://www.conv2.com OpenCV 유용 소스 및 팁, http://t9t9.com/category/Research/OpenCV 참고 책 황선규, “OpenCV 제대로 배우기” 정성환, 이문호, “OpenCV를 이용한 컴퓨터 비전 실무 프로그래밍” 임동훈, “초보자를 위한 OpenCV를 이용한 영상처리” 강의자료, http://opencv.t9t9.com