OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A...
Transcript of OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A...
![Page 1: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/1.jpg)
OpenCV APIA Quick Introduction to the C InterfaceBy David Johnston
![Page 2: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/2.jpg)
![Page 3: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/3.jpg)
Warning: API Version 1.x
![Page 4: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/4.jpg)
Primary OpenCV Interfaces
The 1.x API is based on C.The 2.x API is based on C++.
![Page 5: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/5.jpg)
Goals
Very briefly outline the OpenCV installation process on Windows, OS X, and Linux.
Walk through 4 example programs which should relevant to solving homework 2.
Point out some common API patterns and idioms.
![Page 6: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/6.jpg)
Installing OpenCV
![Page 7: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/7.jpg)
Windows 7 64-bit andVisual C++ 2010 Express
● Install Visual C++ 2010 Express Edition (or some other modern Visual Studio product).
● Download prebuilt binaries and extract them to some desired location.
● Set the OPENCV_DIR environment variable:
> setx -m OPENCV_DIR C:\opencv\build\x64\vc10
![Page 8: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/8.jpg)
Windows 7 64-bit andVisual C++ 10.0 Express
● Add %OPENCV_DIR%\bin to your system path.
● Modify Visual Studio properties to find necessary files. The following describes this process in great detail:
docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html
![Page 9: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/9.jpg)
Install on Mac OS X
● Install XCode or the Apple Command Line Tools. Both are available with a (free) Apple ID.
● Install a package manager such as macports or homebrew.
● Install OpenCV.
![Page 10: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/10.jpg)
Install on Linux
● Install via native package manager (ie. apt-get, yum, etc.)
![Page 11: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/11.jpg)
Build on UNIX-Like System
cc `pkg-config --cflags --libs\ opencv` -o foo foo.c
![Page 12: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/12.jpg)
Four Code Demos
![Page 13: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/13.jpg)
CvMat cvMat(...)
CvMat* cvCreateMat(...)
CvMat* cvCreateMatHeader(...)
IplImage* cvCreateImage(...)
CvSeq* cvCreateSeq(...)
Some Constructors forImportant Data Structures
![Page 14: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/14.jpg)
Ex 1: Image Workflow
Loading, modifying, saving, and closing an image file.
See filter.c.
![Page 15: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/15.jpg)
Macros
OpenCV defines a lot of macros.
● Most are prefixed with CV_*● Many are function-specific● The online documentation is usually pretty clear
![Page 16: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/16.jpg)
In-Place Matrix Operations
cvNot(img, img);
![Page 17: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/17.jpg)
Manual Memory Management
IplImage* img = cvLoadImage(...);
/* do something worthwhile */
cvReleaseImage(&img);
![Page 18: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/18.jpg)
Ex 2: Basic Morphologyand Color
Creating basic structuring elements and calling cvErode() and cvDilate().
Using binary images (bit masks) and cvSet() to color regions of an image.
See noteSeg.c.
![Page 19: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/19.jpg)
Constructors of HelperData Structures
Simple ideas wrapped inside a data type:CvPoint cvPoint(int x, int y)
CvSize cvSize(int width, int height)
CvScalar cvScalar(double d0, double d1, double d2, double d3)
CvScalar cvScalarAll(double d)
CvScalar cvRealScalar(double d)
CvRect cvRect(int x, int y, int width, int height)
![Page 20: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/20.jpg)
Ex 3: Horizontal andVertical Projections
Use cvGetRow(), cvGetCol(), and cvSum() to perform very simple projections.
![Page 21: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/21.jpg)
Sort of Object OrientedThe most important data structure is arguably IplImage.
- Page 33 of Learning OpenCV by Bradski and Kaehler
![Page 22: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/22.jpg)
The IplImage and CvMat data structures are just metadata which provide interfaces to the underlying image data.
See projections.c.
Sort of Object Oriented
![Page 23: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/23.jpg)
Ex 4: Custom Morphology
Creating custom structuring elements using cvCreateStructuringElementEx() and an int[].
![Page 24: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/24.jpg)
Warning: Pointer Arithmeticis Imminent!
Examples and discussion can be found in the text (highly recommended reading):● cvMat and IplImage: pp. 31 - 47● cvSeq: pp. 222 - 234
See customStructElem.c.
![Page 25: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/25.jpg)
Questions?
![Page 26: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/26.jpg)
Example
CvSize size = cvSize(600, 400);
IplImage* img = cvCreateImage(
size, IPL_DEPTH_8U, 3
);
cvSet(img, cvScalarAll(0), NULL);
![Page 27: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/27.jpg)
Why isn't every call to these helper functions a memory leak?
![Page 28: OpenCV API - Computer Engineeringalexs/classes/2013_Spring_575/code/06_Open… · OpenCV API A Quick Introduction to the C Interface By David Johnston. Warning: API Version 1.x. Primary](https://reader031.fdocuments.us/reader031/viewer/2022021816/5a7980d17f8b9a5a438c6153/html5/thumbnails/28.jpg)
Esoteric C99 Feature of the Day:Inline Functions
"The keyword inline is a request to the compiler to insert a function's machine code wherever the function is called in the program."
- Page 106 of C in a Nutshell, by Prinz and Crawford
Automatic variables declared in an inline function become automatic variables in the calling function.