Presentation is loading. Please wait.

Presentation is loading. Please wait.

2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com OpenCV 기초 2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com.

Similar presentations


Presentation on theme: "2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com OpenCV 기초 2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com."— Presentation transcript:

1 2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com
OpenCV 기초 박정식 심규동

2 OpenCV Installation

3 OpenCV Installation

4 OpenCV Installation http://opencv.org/downloads.html
Visual studio 2015의 경우 3.1 Visual studio 2013의 경우 3.0 그 이하 버전의 경우 2.4x 설치

5 OpenCV Installation

6 OpenCV library 라이브러리를 사용하기 위해서는 헤더파일, lib 파일, dll 파일이 필요 헤더 파일 dll 파일
함수, 구조체, 클래스 등의 프로토타입이 선언되어 있으며, 컴파일에 필요 dll 파일 함수의 구현 내용이 변환된 기계어 코드의 집합. 실행시에 필요 lib 파일 각 함수에 대응되는 빈 껍데기 함수가 저장되어 있음. 링크시 필요. 각 함수의 실제 내용은 실행시에 dll 파일로부터 로드됨

7 Configurations of OpenCV

8 Configurations of OpenCV

9 Configurations of OpenCV

10 Configurations of OpenCV

11 Visual studio solution platform
OpenCV 설치경로/build 에 x64만 있는 경우: x64 x64, x86 모두 있는 경우: x64, x86(Win32) 모두 사용 가능

12 Configurations of OpenCV
Setting library path Project  Properties->Include Directories, Library Directories

13 Configurations of OpenCV
Project  Properties  Include Directories Project  Properties  Library Directories 헤더 파일의 경로 설정 Lib 파일의 경로 설정 Visual studio 버전을 의미

14 Configurations of OpenCV

15 Configurations of OpenCV
Setting path for dll files

16 OpenCV Test 링크에 사용할 라이브러리 파일 지정

17 OpenCV Test 링크에 사용할 라이브러리 파일 지정

18 Opencv Basic example

19 OpenCV Test OpenCV는 네임스페이스 cv 사용
#include "opencv2/opencv.hpp" int main() { cv::Mat srcImg = cv::imread("Lenna.png"); printf("Image Width: %d\n", srcImg.cols); printf("Image Height: %d\n", srcImg.rows); printf("Image Channel Num: %d\n", srcImg.channels()); cv::namedWindow("Source Image"); cv::imshow("Source Image", srcImg); cv::waitKey(0); cv::destroyAllWindows(); return 0; } OpenCV는 네임스페이스 cv 사용

20 cv::Mat Mat 주로 1, 2차원 배열 형태의 데이터에 대해 사용 rows cols Channels()
영상, 행렬, 벡터 등 rows 행의 수, 높이(영상) cols 열의 수, 폭(영상) Channels() 채널 수 (RGB의 경우 3)

21 Point, Size cv::Point cv::Point2f cv::Point2d cv::Size cv::Size2f
2차원 정수 좌표. 멤버 변수: x,y cv::Point2f 2차원 실수 좌표 (float). 멤버 변수: x,y cv::Point2d 2차원 실수 좌표 (double). 멤버 변수: x,y cv::Size 정수인 크기 값을 저장. 멤버 변수: width, height cv::Size2f 실수인 크기 값을 저장 (float). 멤버 변수: width, height

22 Rect, RotatedRect cv::Rect cv::RotatedRect 사각형을 표현.
멤버 변수: x, y, width, height cv::RotatedRect 회전된 사각형을 표현 멤버 변수 Point2f center Size2f size float angle 멤버 함수 Rect boundingRect() (x, y) width height boundingRect size.height size.width center angle

23 Load image Mat imread(const string& filename, int flags=1) 파일로부터 영상 로드
파일명. bmp, jpe, png, etc. flags > 0 3채널 영상(BGR) =0 1채널 영상(grayscale로 강제 변환) < 0 알파 채널 포함

24 Save image bool imwrite(const string& filename, Mat img) 영상을 파일로 저장
파일명. bmp, jpe, png, etc. img 영상 데이터

25 GUI functions namedWindow(const string &winname, int flags) 윈도우 생성
윈도우 이름. 이것으로 윈도우를 식별 flags WINDOW_NORMAL WINDOW_AUTOSIZE 기본값 WINDOW_OPENGL 생성된 윈도우는 cv::destroyWindow 또는 cv::destroyAllWindows로 제거

26 GUI functions imshow(const string& winname, InputArray mat) 윈도우에 영상 출력
영상 출력할 윈도우의 이름 mat 영상

27 GUI functions int waitKey(int delay=0) 윈도우에 키 입력 delay return value
키 입력 대기 시간 (ms) 0일 경우 키가 입력될때까지 block return value 입력된 키의 ASCII 코드

28 OpenCV Test 2 #include "opencv2/opencv.hpp" int main() { cv::Mat srcImg = cv::imread("Lenna.png"); cv::namedWindow("Source Image"); while (1) cv::imshow("Source Image", srcImg); int key = cv::waitKey(10); if (key == 27) break; } cv::destroyAllWindows(); return 0; ESC 키 입력

29 GUI function setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0 ) 마우스 콜백 함수 등록 winname 대상 윈도우의 이름 onMouse 콜백 함수 void (*MouseCallback)(int event, int x, int y, int flags, void* userdata) event 마우스 이벤트 x, y: 마우스 포인터 좌표 flags: 이벤트 플래그. Alt, shift, ctrl, 키 입력여부 userdata 콜백 함수에 전달할 데이터

30 Mouse callback example
#include "opencv2/opencv.hpp" cv::Mat srcImg; void on_mouse(int event, int x, int y, int flags, void* param){ if (event == CV_EVENT_LBUTTONDOWN){ printf("Mouse Pointer: %d, %d\n", x, y); cv::circle(srcImg, cv::Point(x, y), 5, cv::Scalar(255, 0, 0), 2); } int main() { srcImg = cv::imread("Lenna.png"); cv::namedWindow("Source Image"); cv::setMouseCallback("Source Image", on_mouse); while (1) cv::imshow("Source Image", srcImg); int key = cv::waitKey(10); if (key == 27) break; cv::destroyAllWindows(); return 0;

31 circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 영상에 원을 그림 Img 대상 영상 Center 원의 중심 좌표 Radius 원의 반지름 Color 색상 (BGR) thickness 선 두께 lineType 8: 8-connected line 4: 4-connected line CV_AA: andialiased line

32 Mouse callback 2 … Mat의 데이터에 접근 x: 1 2 3 y Mat의 영상 데이터 구조 - BGR
void on_mouse(int event, int x, int y, int flags, void* param){ if (event == CV_EVENT_LBUTTONDOWN){ unsigned char B = srcImg.at<unsigned char>(cv::Point(3 * x, y)); unsigned char G = srcImg.at<unsigned char>(cv::Point(3 * x + 1, y)); unsigned char R = srcImg.at<unsigned char>(cv::Point(3 * x + 2, y)); printf("Mouse Pointer: %d, %d \n", x, y); printf("Color Value : %d, %d, %d \n", B, G, R); } Mat의 데이터에 접근 x: 1 2 3 y Mat의 영상 데이터 구조 - BGR

33 Color space conversion

34 Color space conversion
cvtColor(const Mat &src, Mat &dst, int code, int dstCn=0 ) code CV_BGR2GRAY, CV_BGR2HSV … dstCn dst의 채널 수 0인 경우 src의 채널 수와 code를 통해 자동으로 설정

35 Color space conversion
#include "opencv2/opencv.hpp" int main() { cv::Mat srcImg = cv::imread("Lenna.png"); cv::Mat dstImg; cv::cvtColor(srcImg, dstImg, CV_BGR2GRAY); cv::namedWindow("Source Image"); cv::namedWindow("Result Image"); cv::imshow("Source Image", srcImg); cv::imshow("Result Image", dstImg); int key = cv::waitKey(0); cv::destroyAllWindows(); cv::imwrite("convertedImage.jpg", dstImg); return 0; }

36 Color space conversion

37 Color space conversion
HSV, YCbCr, RGB로도 변환 CV_BGR2HSV HSV의 각 채널 값 범위 H: 0 ~ 180 S: 0 ~ 255 V: 0 ~ 255 CV_BGR2YUV CV_BGR2RGB

38 camshift

39 CAMShift RotatedRect CamShift(Mat probImage, Rect& window, TermCriteria criteria) probImage 확률 분포 맵 window 초기 탐색 윈도우 criteria camshift 종료 조건. 예) Return value 추적된 영역 회전된 사각형으로 표현 cv::TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 1)  오차 변화가 1 이하인 경우 또는, 20회 반복한 경우 종료

40 확률 분포 맵을 Mat으로 변환 가로크기가 width, 세로크기가 height인 영상와 같은 크기의 float 배열인 prob라는 변수가 있을 경우 Double 배열의 경우 cv::Mat matProbMap = cv::Mat(height, width, CV_32F); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { matProbMap.at<float>(cv::Point(j, i)) = probMap[i*width + j]; } cv::Mat matProbMap = cv::Mat(height, width, CV_64F); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { matProbMap.at<double>(cv::Point(j, i)) = probMap[i*width + j]; }

41 Thank you Q&A


Download ppt "2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com OpenCV 기초 2016.05.31 박정식 심규동 nangsik83@gmail.com 89kdsim@gmail.com."

Similar presentations


Ads by Google