OpenCV Tutorial
OpenCV Tutorial
CV
Image processing HighGUI
and vision algorithms GUI, Image and Video I/O
Cvaux
CXCORE Auxiliary
basic structures and algoritms, (experimental)
XML support, drawing functions OpenCv
functions
CvCam
cross-platform
module for
processing video
ML
stream from digital
Machine
Learning video cameras
methods
OpenCV
Utilized Directives
D:\OpenCV\cvaux\include\
D:\OpenCV\cxcore\include\
D:\OpenCV\cv\include\
D:\OpenCV\otherlibs\highgui\
D:\OpenCV\otherlibs\cvcam\include\
Configuring MSVS .net 2005
Utilized Directives
..\..\cvaux\include\
..\..\ cxcore\include\
..\..\cv\include\
..\..\otherlibs\highgui\
..\..\otherlibs\cvcam\include\
Configuring MSVS .net 2005
Set Additional Dependencies
Under the Linker tab select
“Input”
Utilized Dependencies
“..\..\lib\cv.lib“
“..\..\lib\cvaux.lib“
“..\..\lib\cxcore.lib“
“..\..\lib\cvcam.lib“
“..\..\lib\highgui.lib"
Testing MSVS .net 2005
#include <cv.h>
#include <highgui.h>
}
Testing MSVS .net 2005
Now that the environment is
configured it would be a good
idea to test it to make sure that
a program will correctly build
and run.
char *imageDataOrigin
int align
cvRect( int x, int y, int width, int height )
char colorModel[4]
cvLoadImage
Supportted formats:
cvNamedWindow("Edge Image");
cvShowImage("Edge Image", absEdgeImage);
cvWaitKey(0);
cvReleaseImage(&orginalImage);
cvReleaseImage(&edgeImage);
cvDestroyWindow("orginal Image");
cvDestroyWindow("Edge Image");
}
Accessing image elements
• Assume that you need to access the K-th channel of the pixel at the i-row and
j-th column. The row index is in the range [0-height-1] . The column index is
in the range [0-width-1] . The channel index is in the range [0-nchannel-1] .
Direct access
Value =((TYPE *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111
Some other useful data structures
CvMatND
CvMat
OpenCV uses the CvMat* as its Multi Dimentional version of CvMat
general purpose matrix structure. It is
managed in an equivalent style
toIplImage* CvSparseMat
SPARSE N-dimensional array
cvCreateMat( int rows, int cols, int type );
cvReleaseMat( CvMat** mat );
CvScalar
4D vector :double val[4] void cvSet( CvArr* arr, CvScalar value, const CvArr* mask=NULL )
From a file
cvReleaseCapture
the image captured by the device is
Releasing the allocated /released by the capture function.
capture source There is no need to release it explicitly
cvReleaseCapture(&capture);
Motion Analysis and Object
Tracking
• Background subtraction
• Motion templates
• Optical flow
• Active contours
• Estimators
Background subtraction
• describes basic functions that enable building statistical
model of background for its further subtraction.
• Background statistics functions:
9 Average
9 Standard deviation
9 Running average μijt = α ⋅ I ijt + (1 − α ) ⋅ μijt −1 , 0 ≤ α ≤ 1
Motion templates
• To generate motion template images that can be
used to rapidly determine where a motion occurred,
how it occurred, and in which direction it occurred.
• Object silhouette
• Motion history images
• Motion history gradients
• Motion segmentation algorithm
MHG
silhouette MHI
Optical Flow
• Block matching technique
• Horn & Schunck technique
• Lucas & Kanade technique
• Pyramidal LK algorithm
• 6DOF (6 degree of freedom) algorithm
Active Contours
• Snake energy: E = Eint + Eext
• Internal energy: Eint = Econt + Ecurv
• External energy: Eext = Eimg + Econ
• Two external energy types:
Eimg = − I ,
Eimg = − grad ( I ) ,
E = α ⋅ Econt + β ⋅ Ecurv + γ ⋅ Eimg ⇒ min
Estimators
• Kalman filter
• ConDensation filter
Saving a video file
CvVideoWriter *writer = 0;
int isColor = 1;
Initializing a int fps = 25; // or 30
video writer int frameW = 640; // 744 for firewire cameras
int frameH = 480; // 480 for firewire cameras
writer=cvCreateVideoWriter("out.avi",
CV_FOURCC('P','I','M','1'),
fps,cvSize(frameW,frameH),isColor);
Writing frames
to video file
IplImage* img = 0;
int nFrames = 50;
for(i=0;i<nFrames;i++)
{
Is there
Img=cvQueryFrame(capture);
more cvWriteFrame(writer,img);
}
Releasing the
video writer cvReleaseVideoWriter(&writer);
Possible Codecs for saving
Codec fourcc
MPEG-1 CV_FOURCC('P','I','M','1')
motion-jpeg CV_FOURCC('M','J','P','G')