a tutorial on opencv ppt: chihwei lin speaker: yu-ting chen

59
A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

Upload: sophia-randall

Post on 19-Dec-2015

285 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

A Tutorial on OpenCVPPT: Chihwei Lin

Speaker: Yu-Ting Chen

Page 2: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

2

Outline

• Introduction• Environment Settings• Modules In OpenCV• Examples– Image– video

Page 3: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

3

Introduction

• What is OpenCV?-- The latest version is 2.4.9

OpenCV is a Open Source Computer Vision Library. It consists of a set of C

functions and a few C + + classes, many common algorithms for image processing

and computer vision

Page 4: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

4

Download OpenCV

• The Newest update is version 2.4.9--You can download the release version (2.4) for Unix,

ios, Windows or Android--It has C++, C, Python and Java interfaces

• OpenCV is free for non-commercial and commercial applications

Page 5: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

5

Applications• 2D and 3D feature toolkits• Egomotion estimation• Facial recognition system• Gesture recognition• Human–computer interaction (HCI)• Mobile robotics• Motion understanding• Object identification• Segmentation and Recognition• Stereopsis Stereo vision: depth perception from 2 cameras • Structure from motion (SFM)• Motion tracking Augmented reality

Page 6: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

6

Machine Learning Library

• Boosting (meta-algorithm) • Decision tree learning• Gradient boosting trees • Expectation-maximization algorithm • k-nearest neighbor algorithm • Naive Bayes classifier • Artificial neural networks • Random forest • Support vector machine (SVM)

Page 7: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

7

Environment Settings

Page 8: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

8

Environment Settings

1 Computer > Properties

2 Advanced system settings

3 Environment Variables

Page 9: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

9

Environment Settings

4 Path

5 C:\opencv\build\x86\vc10\bin

Page 10: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

10

Environment Settings

Page 11: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

11

Environment Settings

Page 12: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

12

Environment Settings

Page 13: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

13

Environment Settings

Page 14: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

14

Environment Settings

• You may also need to add tbb_xxx.dll into system variables

Page 15: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

15

Environment Settings

• Step 1: download OpenCV 2.4.9• Step 2: unzip to C:\opencv\• Step 3: Environment Variables– Computer > Properties > Advanced system settings

> Environment Variables > System variables > Path > C:\opencv\build\x86\vc10\bin

• Step 4: To construct a project in Visual Studio 2010

Page 16: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

16

Environment Settings

• Step 5:select project > right click > properties > VC++ Directories– Include Directories• C:\opencv\build\include• C:\opencv\build\include\opencv• C:\opencv\build\include\opencv2

– Library Directories• C:\opencv\build\x86\vc10\lib

Page 17: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

17

Environment Settings

• Step 6:select project > right click > properties > Linker > input(d->debug mode)– Additional Dependencies

• opencv_calib3d249d.lib• opencv_contrib249d.lib• opencv_core249d.lib• opencv_features2d2429d.lib• opencv_flann249d.lib• opencv_gpu249d.lib• opencv_haartraining_engined.lib• opencv_highgui249d.lib• opencv_imgproc249d.lib• opencv_legacy249d.lib

• opencv_ml249d.lib• opencv_nonfree249d.lib• opencv_objdetect249d.lib• opencv_photo249d.lib• opencv_stitching249d.lib• opencv_ts249d.lib• opencv_video249d.lib• opencv_videostab249d.lib

Page 18: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

18

Environment Settings

• If your opencv is old version and you do not rebuild the opencv library– You also need tbb_debug.lib– Download http://threadingbuildingblocks.org/

• Step 7:Environment Variables– Computer > Properties > Advanced system settings

> Environment Variables > System variables > Path > C:\tbb43_20140724oss\bin\ia32\vc10

Page 19: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

19

Environment Settings

• Step 8:select project > right click > properties > VC++ Directories– Library Directories• C:\tbb43_20140724oss\lib\ia32\vc10

Page 20: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

20

Modules In OpenCV

OpenCV

coreimgpro

c

video

highgui

and so ongpu 

Objdetect

Features2d

calib3dOpenCV has several

modules

Page 21: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

21

Core

• A compact module defining basic data structures– basic functions used by all other modules.

• The data structures of the new version 2.x were substantially changes

Page 22: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

22

Core – Basic Structures

• Scalar_• Range• Ptr• Mat• …

• DataType• Point_• Point3_• Size_• Rect_

Page 23: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

23

Core – Mat

– Defined Mat classes to represent the matrix, replacing the previous CvMat and lplImage

– The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array• It can be used to store

real or complex-valued vectors and matrices• grayscale or color images• voxel volumes• vector fields• point clouds• Tensors• histograms

– Mat handles all the memory automatically

Page 24: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

24

Core – Mat

Page 25: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

25

Core – Mat

• Data formats– Matrix elements can be of the following types

– Multi-channel types can be specified using following options

Page 26: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

26

Core – Point

• Point_ template class <- CvPoint,CvPoint2D32f• Point3_ template class <- CvPoint2D32f

Page 27: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

27

Core-Point

• class Point_– For convenience, the following type aliases are

defined:

– Example

Page 28: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

28

imgproc

• an image processing module– linear and non-linear image filtering• GaussianBlur 、 Smooth 、 Sobel

– geometrical image transformations• resize, affine and perspective warping, generic table-

based remapping

– color space conversion– histograms– an so on.

Page 29: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

29

Imgproc - GaussianBlur

• GaussianBlur– Smoothes an image using a Gaussian filter– void GaussianBlur(InputArray src, OutputArray dst, Size ksize,

double sigmaX, double sigmaY=0, intborderType=BORDER_DEFAULT )

– Parameters:• src – Source image:The depth should be

CV_8U, CV_16U, CV_16S, CV_32F or CV_64F• dst – Destination image of the same size and type as src .• ksize – Gaussian kernel size.• sigmaX – Gaussian kernel standard deviation in X direction.• sigmaY – Gaussian kernel standard deviation in Y direction.• borderType – Pixel extrapolation method.

Page 30: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

30

Imgproc - resize

• resize– Resizes an image– void resize(InputArray src, OutputArray dst, Size dsize,

double fx=0, doublefy=0, int interpolation=INTER_LINEAR )• Parameters:

– src – Source image.– dst – Destination image– interpolation –Interpolation method:

» INTER_NEAREST - a nearest-neighbor interpolation» INTER_LINEAR - a bilinear interpolation (used by default)» INTER_CUBIC - a bicubic interpolation

Page 31: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

31

video

• a video analysis module – motion estimation– background subtraction• BackgroundSubtractorMOG

– object tracking algorithms.

Page 32: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

32

features2d

• salient feature detectors– FAST– SIFT

• descriptors• descriptor matchers.

Page 33: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

33

Features2d-SIFT

• Extract features and computes their descriptors sing SIFT algorithm• void SIFT::operator()(InputArray img, InputArray mask,

vector<KeyPoint>& keypoints, OutputArray descriptors, booluseProvidedKeypoints=false)

• Parameters:– img – Input 8-bit grayscale image– mask – Optional input mask that marks the regions where we should detect

features.– keypoints – The input/output vector of keypoints– descriptors – The output matrix of descriptors. Pass cv::noArray() if you do not

need them.– useProvidedKeypoints – Boolean flag. If it is true, the keypoint detector is not

run.

Page 34: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

34

SIFT descriptor

Page 35: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

35

Page 36: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

36

Features2d-FAST

• Detects corners using the FAST algorithm• C++: void FAST(InputArray image,

vector<KeyPoint>& keypoints, int threshold, bool nonmaxSupression=true )

• Parameters:– image – Image where keypoints (corners) are detected.– keypoints – Keypoints detected on the image.– threshold – Threshold on difference between intensity of the

central pixel and pixels on a circle around this pixel. – nonmaxSupression – If it is true, non-maximum suppression

is applied to detected corners (keypoints).

Page 37: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

37

Features2d-FAST

• Sample of FAST

Page 38: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

38

Features2d-FAST

• Result of FAST

Page 39: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

39

calib3d

• basic multiple-view geometry algorithms– single and stereo camera calibration• findHomography

– elements of 3D reconstruction.

Page 40: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

40

Calib3d-findHomography

• findHomography– Finds a perspective transformation between two planes.– C++: Mat findHomography(InputArray srcPoints,

InputArray dstPoints, int method=0, double ransacReprojThreshold=3, OutputArraymask=noArray() )

– Parameters:• srcPoints – Coordinates of the points in the original plane• dstPoints – Coordinates of the points in the target plane• method –Method used to computed a homography matrix. The following

methods are possible:– 0 - a regular method using all the points– CV_RANSAC - RANSAC-based robust method– CV_LMEDS - Least-Median robust method

• mask – Optional output mask set by a robust method

Page 41: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

41

objdetect

• detection of objects• instances of the predefined classes– CascadeClassifier– for example: faces, eyes, mugs, people, cars, and

so on

Page 42: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

42

highgui

• an easy-to-use interface to video capturing, image and video codecs, as well as simple UI capabilities

Page 43: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

43

Highgui - Reading and Writing Images

• Imdecode• Imencode• Imread• imwrite

Page 44: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

44

Highgui - Reading and Writing Video

• VideoCapture• Open• isOpened• release

• grab• retrieve• read• get• set

Page 45: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

45

Highgui – Other User Interfaces

– createTrackbar– getTrackbarPos– setTrackbarPos– Imshow– namedWindow

– destroyWindow– destroyAllWindows– MoveWindow– ResizeWindow– SetMouseCallback– waitKey

Page 46: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

46

Two ways of using cv namespace

• All the OpenCV classes and functions are placed into the cv namespace– use the cv::specifier

– using namespace cv

Page 47: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

47

Example - Image

Page 48: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

48

imread

• Loads an image from a file

– C++: Mat imread(const string& filename, int flags=1 )

• filename – Name of file to be loaded.

Page 49: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

49

imread

• C++: Mat imread(const string& filename, int flags=1 )– flags –Flags specifying the color type of a loaded

image:• > 0 Return a 3-channel color image

– CV_LOAD_IMAGE_COLOR

• = 0 Return a grayscale image– CV_LOAD_IMAGE_GRAYSCALE

• < 0 Return the loaded image as is– CV_LOAD_IMAGE_ANYCOLOR

Page 50: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

50

imshow

• Displays an image in the specified window.

• C++: void imshow(const string& winname, InputArray mat)– Parameters:• winname – Name of the window.• image – Image to be shown.

Page 51: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

51

namedWindow

• Creates a window.• C++: void namedWindow(const

string& winname, int flags=WINDOW_AUTOSIZE )– Parameters:• winname – Name of the window in the window caption

that may be used as a window identifier.• flags – Flags of the window.

Page 52: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

52

Example - Video

Page 53: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

53

VideoCapture

• Class for video capturing from video files or cameras.– C++: VideoCapture::VideoCapture(const

string& filename)– C++: VideoCapture::VideoCapture(int device)

Page 54: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

54

VideoCapture

Page 55: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

55

VideoCapture::isOpened

• Returns true if video capturing has been initialized already.

• C++: bool VideoCapture::isOpened()

Page 56: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

56

Conversion among Mat, cvMat and IplImage - pointer

• IplImage to Mat

• Mat to IplImage

Page 57: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

57

Conversion among Mat, cvMat and IplImage - pointer

• cvMat to Mat

– Similar to the conversion of IplImage

• Mat to cvMat

Page 58: A Tutorial on OpenCV PPT: Chihwei Lin Speaker: Yu-Ting Chen

58

Conversion among Mat, cvMat and IplImage – non-pointer

• pointer / non-pointer– Non-pointer

– Pointer