Open Source Computer Vision
OpenCV (Open Source Computer Vision Library) is a opensource library of programming functions mainly aimed at real time computer vision. This Library is cross-Platform.
It’s main color format is BGRX, which is the bytes of RGB reversed and with a 4th byte of padding for efficiency, but generally referred to as RGB for simplicity.
Variable type , begins by capitalized ( IplImage, CvSize, CvCapture , …)
Functions, begins by lowercase letter ( cvReleaseImage, cvNamedWindow, …)
OpenCV::Documents
Books http://opencv.org/books.html
Tutorials http://docs.opencv.org/doc/tutorials/tutorials.html introduction, modules,…
http://docs.opencv.org/opencv_tutorials.pdf * * * * *
PDF Tutorials C:\OpenCV2.4.9\opencv\build\doc
OpenCV Modules
cv : Main OpenCV functions.
cvaux : Auxiliary (experimental) OpenCV functions.
core : Data structures and linear algebra support.
imgproc :
highgui : GUI functions.
video :
calib3d : camera calibration and 3D reconstruction
features2D :
objdetect :
ml :
gpu : GPU – accelerated algorithms
cv::Mat
cv
all the openCV classes and functions are placed into the cv namespace.
Mat
– The basic image structure in openCV.
– overcomes the old IplImage problems.
– automatic memory management.
– Two data parts : matrix header ( contains the information about the matrix ), a pointer to the matrix containing the pixel values.
Code
// read an image
cv::Mat image = cv::imread (“image.jpg”) ;
// creat an image window named “Hello CV”
cv:nameWindow (“Hello CV”) ;
// Show the image on window
cv::imshow (“Hello CV”,image) ;
Code Overview
Variable type , begins by capitalized ( IplImage, CvSize, CvCapture , …)
Functions, begins by lowercase letter ( cvReleaseImage, cvNamedWindow, …)
Create Image
IplImage* image1 ;
IplImage image1 = cvCreateImage ( cvGetSizeImage (frame),8,1 ) ;
IplImage* canny ;
canny=cvCreateImage ( cvSize(image1->width,image1->height),IPL_DEPTH_8U,1 ) ;
// ( Image size, depth variable (Unsigned 8 bit integer) , image1->nChannels variable)
CvSizesz={600,100} ;
frame=cvCreateImage(sz,IPL_DEPTH_8U,3) ;
cvReleaseImage (image1); // release memory, erase variable
Structure CvCapture / video capturing and writing
The structure CvCapture is used only as a parameter for video capturing functions.
CvCapture* cvCaptureFromCAM (int index) ;
CvCapture* cvCaptureFromFile(const char* filename);
CvCapture*capture=cvCreateCameraCapture(int index=1); // Camera index to use
CvCapture* capture = CvCreateCameraCapture(CV_CAP_ANY);
“Allocates and initialized the CvCapture structure for reading a video stream from the camera. Currently two camera interfaces can be used on Windows: Video for Windows (VFW) and Matrox Imaging Library (MIL); and two on Linux: V4L and FireWire (IEEE1394)“
cvReleaseCapture(&capture) ;
– int cvGrabFrame (CvCapture*capture)
– IplImage* cvRetrieveFrame (CvCapture*capture)
– IplImage* cvQueryFrame ( CvCapture* capture)
– double cvGetCaptureProperty( CvCapture* capture, int property_id)
– int cvSetCaptureProperty(CvCapture*capture, int property_id, double value)
– typedef struct CvCapture*CvVideoWriter()
CvVideoWriter* cvCreateVideoWriter(const char* filename, int fourcc, double fps,CvSizeframe_size, int is_color=1)
– int cvWriteFrame(CvCapture* writer, const IplImage* image)
“Which codecs and file formats are supported depends on the back end library. On Windows HighGui uses Video for Windows (VfW), on Linux ffmpeg is used and on Mac OS X the back end is QuickTime.”
Open Window / Show Image
cvNamedWindow (“window1”,1);
cvNamedWindow (“window1”, CV_WINDOW_AUTOSIZE);
cvShowImage (“window1”,image1) ; //Convert automatically BGR OpenCV format, to RGB
cvDestroyWindow (“window1”) ;
cvDestroyAllWindows () ;
cvMoveWindow(“window1”,100,100); // origin left top corner
cvResizeWindow(constchar*name,intwidth,intheight);
Put Text
IplImage* image1 ;
CvFontfont;
cvInitFont ( &font,CV_FONT_HERSHEY_SIMPLEX,1.0,1.0,0,2,CV_AA ) ;
cvPutText (image1,“Press ESC key”, cvPoint(10,50),&font,CV_RGB(155,155,15)) ;
//cvPutText(frame, “Hello World!”, cvPoint(10, 130), &font, cvScalar(15, 255, 255, 0));
cvShowImage(“pressESC”,image1);
//no error if not declared cvNamedWindow ( “pressESC”, 1 );
Wait Key
cvWaitKey(0); // If value =< 0 , waits until key is pressed
cvWaitKey(int delay=1000) ; // wait for 1000 ms , return ASCII code key or -1 if time outdated
While(cvWaitKey(10)!=27) ; // 27 = ASCII code of ESCape key
CvLoadImage / CvSaveImage
Supported Formats :
– Windows bitmaps – BMP, DIB
– JPEG – JPEG, JPG, JPE
– Portable Network Graphics – PNG
– Portable image format – PBM, PGM, PPM
– Sun rasters – SR, RAS
– TIFF – TIFF, TIF
Save or Load, extension of the file defines the format.
Example 1 – Capture image from camera and show it in a window
#include<cv.h>
#include<highgui.h>
int main(void)
{
IplImage*frame; //Createsavariabletostoreframes
CvCapture*capture = cvCreateCameraCapture(CV_CAP_ANY) ; //Creates a variable to define the camera and initialises it
cvNamedWindow (“Camera”, CV_WINDOW_AUTOSIZE) ; //Creates an OpenCV Window to show images
while (cvWaitKey(10) != 27) // Creates an infinite loop and stops if ESC key was pressed
{
frame = cvQueryFrame(capture) ; // Captures a frame from the camera and stores it in ‘frame’
if ( !frame ) break ; // If there is no frame jumps out of the loop
cvShowImage (“Camera”,frame) ; // Shows captured frame
}
cvReleaseCapture (&capture) ; // Releases camera
cvDestroyWindow (“Camera”) ; // Destroy the Open CVWindow
}
//Terminates program
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.