SlideShare a Scribd company logo
Getting Started with OpenCV Vadim Pisarevsky (vadim.pisarevsky@intel.com)
Agenda OpenCV facts and overall structure First steps Almighty HighGUI Using OpenCV within your program Now turn on IPP Dynamic structures Save your data Some useful OpenCV tips & tricks Where to get more information
What is OpenCV? OpenCV stands for Open Source Computer Vision Library Being developed at Intel since 1999 Written in C/C++; Contains over 500 functions. Available on Windows, Linux and MacOSX. So far is extensively used in many companies and research centers Home page:  www.intel.com/technology/computing/opencv/
Can you use OpenCV? Extract from license: [Copyright Clause] Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistribution's of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistribution's in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of Intel Corporation may not be used to endorse or promote products derived from this software without specific prior written permission. [Disclaimer] In brief: Sure!
Now the question is: Should you use it?
OpenCV structure CXCORE basic structures and algoritms, XML support, drawing functions CV Image processing and vision algorithms HighGUI GUI, Image and Video I/O We will mostly focus on these two in this presentation
First OpenCV Program 1. #include <cxcore.h> 2. #include <highgui.h> 3. #include <math.h> 4. int main( int argc, char** argv ) { 5.  CvPoint center; 6.  double scale=-3; 7.  IplImage* image = argc==2 ? cvLoadImage(argv[1]) : 0; 8.  if(!image) return -1; 9.  center = cvPoint(image->width/2,image->height/2); 10.  for(int i=0;i<image->height;i++) 11.  for(int j=0;j<image->width;j++) { 12.  double dx=(double)(j-center.x)/center.x; 13.  double dy=(double)(i-center.y)/center.y; 14.  double weight=exp((dx*dx+dy*dy)*scale); 15.  uchar* ptr = &CV_IMAGE_ELEM(image,uchar,i,j*3); 16.  ptr[0] = cvRound(ptr[0]*weight); 17.  ptr[1] = cvRound(ptr[1]*weight); 18.  ptr[2] = cvRound(ptr[2]*weight);  } 19.  cvSaveImage( “copy.png”, image ); 20.  cvNamedWindow( &quot;test&quot;, 1 ); 21.  cvShowImage( &quot;test&quot;, image ); 22.  cvWaitKey(); 23.  return 0; } Radial gradient
How to build and run the program? Obtain the latest version of OpenCV: Get the official release (currently, OpenCV b5 for Windows = OpenCV 0.9.7 for Linux) at  http://www.sourceforge.net/projects/opencvlibrary Get the latest snapshot from CVS: :pserver:anonymous@cvs.sourceforge.net:/cvsroot/opencvlibrary   Install it: On Windows run installer, on Linux use “./configure + make + make install” (see INSTALL document) On Windows: open opencv.dsw, do “build all”, add opencv/bin to the system path; on Linux - see a) Build the sample: Windows: cl /I<opencv_inc> test.cpp /link /libpath:<opencv_lib_path> cxcore.lib cv.lib highgui.lib Create project for VS (see opencv/docs/faq.htm) or use opencv/samples/c/cvsample.dsp as starting point Linux: g++ -o test `pkg-config –cflags` test.cpp `pkg-config –libs opencv` Run it: test lena.jpg or ./test lena.jpg
The Program Quick Review short and clear  program, no need to mess with MFC/GTK/QT/…, cross-platform cvLoadImage()  and  cvSaveImage()  provide the easiest way to save/load images of various formats. cvPoint  and other “constructor” functions make the code shorter and allow 1-line functions call. CV_IMAGE_ELEM()  – pretty fast way to access image pixels cvRound()  is very fast and convenient way to cast float/double to int cvNamedWindow()  creates “smart” window for viewing an image cvShowImage()  shows image in the window cvWaitKey()  delays the execution until key pressed or until the specified timeout is over 1. #include <cxcore.h> 2. #include <highgui.h> 3. #include <math.h> 4. int main( int argc, char** argv ) { 5.  CvPoint center; 6.  double scale=-3; 7.  IplImage* image = argc==2 ?  cvLoadImage (argv[1]) : 0; 8.  if(!image) return -1; 9.  center =  cvPoint (image->width/2,image->height/2); 10.  for(int i=0;i<image->height;i++) 11.  for(int j=0;j<image->width;j++) { 12.  double dx=(double)(j-center.x)/center.x; 13.  double dy=(double)(i-center.y)/center.y; 14.  double weight=exp((dx*dx+dy*dy)*scale); 15.  uchar* ptr = & CV_IMAGE_ELEM (image,uchar,i,j*3); 16.  ptr[0] =  cvRound (ptr[0]*weight); 17.  ptr[1] =  cvRound (ptr[1]*weight); 18.  ptr[2] =  cvRound (ptr[2]*weight);  } 19.  cvSaveImage ( “copy.png”, image ); 20.  cvNamedWindow ( &quot;test&quot;, 1 ); 21.  cvShowImage ( &quot;test&quot;, image ); 22.  cvWaitKey (); 23.  return 0; }
What else HighGUI can do? “ Smart” windows Image I/O, rendering Processing keyboard and other events, timeouts Trackbars Mouse callbacks Video I/O
Windows cvNamedWindow(window_name, fixed_size_flag); creates window accessed by its name. Window handles repaint, resize events. Its position is remembered in registry: cvNamedWindow(“ViewA”,1); cvMoveWindow(“ViewA”,300,100); cvDestroyWindow(“ViewA”); … cvShowImage(window_name, image); copies the image to window buffer, then repaints it when necessary. {8u|16s|32s|32f}{C1|3|4} are supported. only the whole window contents can be modified. Dynamic updates of parts of the window are done using operations on images, drawing functions etc. On Windows native Win32 UI API is used Linux – GTK+ 2 MacOSX – X11 & GTK+ 2; Native Aqua support is planned.
Image I/O IplImage* cvLoadImage(image_path, colorness_flag); loads image from file, converts to color or grayscle, if need, and returns it (or returns NULL). image format is determined by the file contents. cvSaveImage(image_path, image); saves image to file, image format is determined from extension. BMP, JPEG (via libjpeg), PNG (via libpng), TIFF (via libtiff), PPM/PGM formats are supported. IplImage* img = cvLoadImage(“picture.jpeg”,-1); if( img ) cvSaveImage( “picture.png”, img );
Waiting for keys… cvWaitKey(delay=0); waits for key press event for <delay> ms or infinitely, if delay=0. The  only  function in highgui that process message queue => should be called periodically. Thanks to the function many highgui programs have clear sequential structure, rather than event-oriented structure, which is typical for others toolkits To make program continue execution if no user actions is taken, use cvWaitKey(<delay_ms!=0>); and check the return value  // opencv/samples/c/delaunay.c … for(…) { … int c = cvWaitKey(100); if( c >= 0 ) // key_pressed break; } delaunay.c, 240 lines
Trackbars cvCreateTrackbar(trackbar_name,window_name, position_ptr,max_value,callback=0); creates trackbar and attaches it to the window. Value range 0..max_value. When the position is changed, the global variable updated and callback is called, if specified. // opencv/samples/c/morphology.c int dilate_pos=0; // initial position value void Dilate(int pos) { …  cvShowImage( “E&D”, erode_result ); } int main(…){ … cvCreateTrackbar(“Dilate”,”E&D”, &dilate_pos,10,Dilate); … cvWaitKey(0); // check for events & process them ...} morphology.c, 126 lines
Mouse Events cvSetMouseCallback(window_name, callback, userdata=0); sets callback on mouse events (button clicks, cursor moves) for the specified window // opencv/samples/c/lkdemo.c void on_mouse(int event,int x,int y,int flags, void* param) { … } int main(…){ … cvSetMouseCallback(“LkDemo”,on_mouse,0); … cvWaitKey(0); // check for events & process them … } Scroll forward for example  
Video I/O API CvCapture* cvCaptureFromCAM(camera_id=0); initializes capturing from the specified camera CvCapture* cvCaptureFromFile(videofile_path); initializes capturing from the video file. IplImage* cvQueryFrame(capture); retrieves the next video frame (do not alter the result!), or NULL if there is no more frames or an error occured. cvGetCaptureProperty(capture, property_id); cvSetCaptureProperty(capture, property_id, value); retrieves/sets some capturing properties (camera resolution, position within video file etc.) cvReleaseCapture(&capture); do not forget to release the resouces at the end! Used interfaces: Windows: VFW, IEEE1394, MIL, DShow (in progress), Quicktime (in progress) Linux: V4L2, IEEE1394, FFMPEG MacOSX: FFMPEG, Quicktime (in progress)
Video I/O Example // opencv/samples/c/lkdemo.c int main(…){ … CvCapture* capture = <…> ? cvCaptureFromCAM(camera_id) : cvCaptureFromFile(path); if( !capture ) return -1; for(;;) { IplImage* frame=cvQueryFrame(capture); if(!frame) break; // … copy and process image cvShowImage( “LkDemo”, result ); c=cvWaitKey(30); // run at ~20-30fps speed if(c >= 0) { // process key }} cvReleaseCapture(&capture);}  lkdemo.c, 190 lines (needs camera to run)
Using OpenCV in User Apps Case 1. Least squares (real example from OpenCV itself). // was (A – NxN matrix, b – Nx1 right-side vector, x – solution): some_old_least_sq_func_32f( /*  float*  */ A, /*  int  */ N, /*  int  */ N, /*  float*  */ b, /*  float*  */ x); // has been converted to: { CvMat _A=cvMat(N,N,CV_32F,A), _b=cvMat(N,1,CV_32F,b), _x=cvMat(N,1,CV_32F,x); cvSolve( &_A, &_b, &_x, CV_SVD ); } Advantages: error handling size and type checking easier 32f<->64f conversion
Using OpenCV in User Apps (II) Case 2. Film Grain DirectShow filter prototype. // inside DirectShow filter Transform method …  pMediaSample->GetPointer(&pData); AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType(); BITMAPINFOHEADER *bh = &((VIDEOINFOHEADER *)pType->pbFormat)->bmiHeader; IplImage* img = cvCreateImageHeader(cvSize(bh.biWidth,bh.biHeight), 8, 3); cvSetData( img, pData, (bh.biWdith*3 + 3) & -4 ); cvCvtColor( img, img, CV_BGR2YCrCb ); IplImage* Y=cvCreateImage(cvGetSize(img),8,1); cvSplit(img,Y,0,0,0); IplImage* Yf=cvCreateImage(cvGetSize(img),32,1); CvRNG rng=cvRNG(<seed>); cvRandArr(&rng,Yf,cvScalarAll(0),cvScalarAll(GRAIN_MAG),CV_RAND_NORMAL); cvSmooth(Yf,Yf,CV_GAUSSIAN,GRAIN_SIZE,0,GRAIN_SIZE*0.5); cvAcc(Y, Yf); cvConvert(Yf,Y); cvMerge(Y,0,0,0,img); cvCvtColor( img, img, CV_YCrCb2BGR ); cvReleaseImage(&Yf); cvReleaseImage(&Y); cvReleaseImageHeader(&img);…
Using OpenCV in User Apps (III) Case 3. Visualization inside some test program (hypothetical example,yet quite real too). //  was …  /* some image processing code containing bug */ … //  has been converted to  … …  /* at this point we have the results */ #if VISUALIZE #include <highgui.h> #pragma comment(“lib”,”cxcore.lib”) #pragma comment(“lib”,”highgui.lib”) CvMat hdr; cvInitMatHeader (&hdr,200/*height*/,320/*width*/,CV_8UC3, data_ptr); CvMat* vis_copy=cvCloneMat(&hdr); cvNamedWindow(“test”,1); cvCircle (vis_copy, cvCenter(bug_x,bug_y), 30, CV_RGB(255,0,0), 3, CV_AA ); // mark the suspicious area cvShowImage(“test”,vis_copy); cvWaitKey (0 /* or use some delay, e.g. 1000 */); // cvSaveImage( “buggy.png”, vis_copy ); // if need, save for post-mortem analysis //  cvDestroyWindow (“test”); // if need #endif
What can be drawn in image (besides circles)? cxcore provides numerous drawing functions: Lines, Circles, Ellipses, Elliptic Arcs Filled polygons or polygonal contours Text (using one of embedded fonts) Everything can be drawn with different colors, different line width, antialiasing on/off Arbitrary images types are supported (for depth!=8u antializing is off)
Using OpenCV with IPP #include <cv.h> #include <highgui.h> #include <ipp.h> #include <stdio.h> int main(int,char**){ const int M=3; IppiSize msz={M,M}; IppiPoint ma={M/2,M/2}; IplImage* img=cvLoadImage(“lena.jpg”,1); IplImage* med1= cvCreateImage (cvGetSize(img),8,3); IplImage* med2= cvCloneImage (med1); int64 t0 =  cvGetTickCount() ,t1,t2; IppiSize sz = {img->width-M+1,img->height-M+1}; double isz=1./(img->width*img->height); cvSmooth (img,med1,CV_MEDIAN,M); // use IPP via OpenCV t0= cvGetTickCount ()-t0; cvUseOptimized (0); // unload IPP t1 =  cvGetTickCount (); cvSmooth (img,med1,CV_MEDIAN,M); // use C code t1= cvGetTickCount ()-t1; t2= cvGetTickCount (); ippiMedianFilter_8u_C3R ( // use IPP directly & CV_IMAGE_ELEM (img,uchar,M/2,M/2*3), img->widthStep, & CV_IMAGE_ELEM (med1,uchar,M/2,M/2*3), med1->widthStep, sz, msz, ma ); t2= cvGetTickCount ()-t2; printf(“t0=%.2f, t1=%.2f, t2=%.2f\n”, (double)t0*isz,(double)t1*isz,(double)t2*isz); return 0; } Triple 3x3 median filter
How does it works? (Quite Similar to IPP dispatcher mechanism) // cv/src/_cvipp.h … IPCVAPI_EX(CvStatus, icvFilterMedian_8u_C3R, “ippiFilterMedian_8u_C3R”, CV_PLUGINS1(CV_PLUGIN_IPPI), (const void* src, int srcstep, void* dst, int dststep, CvSize roi, CvSize ksize, CvPoint anchor )) … // cv/src/cvswitcher.cpp … #undef IPCVAPI_EX #define IPCVAPI_EX() … static CvPluginFuncInfo cv_ipp_tab[] = { #undef _CV_IPP_H_ /* with redefined IPCVAPI_EX every function declaration turns to the table entry */ #include &quot;_cvipp.h“ #undef _CV_IPP_H_ {0, 0, 0, 0, 0} }; static CvModuleInfo cv_module = { 0, &quot;cv&quot;, &quot;beta 5 (0.9.7)&quot;, cv_ipp_tab }; static int loaded_functions = cvRegisterModule( &cv_module ); … // cv/src/cvsmooth.cpp icvFilterMedian_8u_C3R_t icvFilterMedian_8u_C3R_p = 0; void cvSmooth() { if( icvFilterMedian_8u_C3R_p ) { /* use IPP */ } else { /* use C code */… }
Dynamic Structures (when 2d array is not enough) The sample task: collect the locations of all non-zero pixels in the image. Where to store the locations? Possible solutions: Allocate array of maximum size (sizeof(CvPoint)*width*height – a huge value) Use two-pass algorithm Use list or similar data structure …  (Any other ideas?) // construct sequence of non-zero pixel locations CvSeq*  get_non_zeros( const IplImage* img,  CvMemStorage*  storage ) { CvSeq * seq =  cvCreateSeq ( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage ); for( int i = 0; i < img->height; i++ ) for( int j = 0; j < img->width; j++ ) if( CV_IMAGE_ELEM(img,uchar,i,j) ) { CvPoint pt={j,i};  cvSeqPush ( seq, &pt ); } return seq; }
Memory Storages … Memory storage is a linked list of memory blocks. Data is allocated in the storage by moving “free space” position, new blocks are allocated and added to the list if necessary It’s only possible to free the continous block of data by moving position back, thus both allocation and deallocation are virtually instant(!) Functions to remember:  cvCreateMemStorage(block_size), cvReleaseMemStorage(storage); cvClearMemStorage(storage); cvMemStorageAlloc(storage,size); All OpenCV “dynamic structures” reside in memory storages. The model is very efficient for repetitive processing, where cvClearMemStorage is called on top level between iterations.
Sequences Sequence is a linked list of memory blocks (sounds familiar, eh?). Large sequences may be split into multiple storage blocks, while several small sequences may fit into the single storage block. Sequence is deque: adding/removing elements to/from either end is O(1) operation, inserting/removing elements from the middle is O(N). Accessing arbitrary element is also ~O(1) (when storage block size >> sequence block) Sequences can be sequentially read/written using readers/writers (similar to STL’s iterators) Sequences can not be released, use  cv{Clear|Release}MemStorage () Basic Functions to remember: cvCreateSeq(flags,header_size,element_size,storage), cvSeqPush[Front](seq,element), cvSeqPop[Front](seq,element), cvClearSeq(seq), cvGetSeqElem(seq,index), cvStartReadSeq(seq,reader), cvStartAppendToSeq(seq,writer ).
Sets and Sparse Matrices Set is variant of sequence with “free node list” and it’s active elements do not follow each other – that is, it is convenient way to organize a “heap” of same-size memory blocks. Sparse matrix in OpenCV uses CvSet for storing elements. // Collecting the image colors CvSparseMat*  get_color_map( const IplImage* img ) { int dims[] = { 256, 256, 256 }; CvSparseMat * cmap =  cvCreateSparseMat (3, dims, CV_32SC1); for( int i = 0; i < img->height; i++ ) for( int j = 0; j < img->width; j++ ) {  uchar* ptr=&CV_IMAGE_ELEM(img,uchar,i,j*3); int idx[] = {ptr[0],ptr[1],ptr[2]}; ((int*) cvPtrND (cmap,idx))[0]++; } // print the map CvSparseMatIterator  it; for( CvSparseNode  *node =  cvInitSparseMatIterator ( mat, &iterator ); node != 0; node =  cvGetNextSparseNode ( &iterator )) { int* idx =  CV_NODE_IDX (cmap,node); int count=*(int*) CV_NODE_VAL (cmap,idx); printf( “(b=%d,g=%d,r=%d): %d\n”, idx[0], idx[1], idx[2], count ); } return cmap; }
Save your data (to load it then) Need a config file? Want to save results of your program to pass it to another one, or look at it another day? It all can be easily done with OpenCV persistence-related functions. // somewhere deep in your code… you get 5x5 // matrix that you’d want to save { CvMat A = cvMat( 5, 5, CV_32F, the_matrix_data ); cvSave ( “my_matrix.xml”, &A ); } // to load it then in some other program use … CvMat* A1 = (CvMat*) cvLoad ( “my_matrix.xml” ); <?xml version=&quot;1.0&quot;?> <opencv_storage> <my_matrix type_id=&quot;opencv-matrix&quot;> <rows>5</rows> <cols>5</cols> <dt>f</dt> <data> 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1.</data></my_matrix> </opencv_storage> my_matrix.xml
So what about config file? CvFileStorage * fs =  cvOpenFileStorage (“cfg.xml”, 0,  CV_STORAGE_WRITE ); cvWriteInt ( fs, “frame_count”, 10 ); cvWriteStartWriteStruct ( fs, “frame_size”, CV_NODE_SEQ); cvWriteInt( fs, 0, 320 ); cvWriteint( fs, 0, 200 ); cvEndWriteStruct (fs); cvWrite ( fs, “color_cvt_matrix”, cmatrix ); cvReleaseFileStorage ( &fs ); Writing config file CvFileStorage* fs = cvOpenFileStorage(“cfg.xml”, 0, CV_STORAGE_READ); int frame_count =  cvReadIntByName ( fs, 0, “frame_count”, 10 /* default value */ ); CvSeq* s =  cvGetFileNodeByName (fs,0,”frame_size”)->data.seq; int frame_width =  cvReadInt ( (CvFileNode*)cvGetSeqElem(s,0) ); int frame_height = cvReadInt( (CvFileNode*)cvGetSeqElem(s,1) ); CvMat* color_cvt_matrix = (CvMat*)cvRead(fs,0,”color_cvt_matrix”); cvReleaseFileStorage( &fs ); Reading config file <?xml version=&quot;1.0&quot;?> <opencv_storage> <frame_count>10</frame_count> <frame_size>320 200</frame_size> <color_cvt_matrix type_id=&quot;opencv-matrix&quot;> <rows>3</rows> <cols>3</cols> <dt>f</dt> <data>…</data></color_cvt_matrix> </opencv_storage> cfg.xml
Some OpenCV Tips and Tricks Use short inline “constructor” functions:  cvScalar, cvPoint, cvSize, cvMat  etc. Use  cvRound  and vector math functions ( cvExp, cvPow … ) for faster processing Use  cvStackAlloc  for small buffers Consider  CV_IMPLEMENT_QSORT_EX()  as possible alternative to qsort() & STL sort(). To debug OpenCV program when runtime error dialog error pops up, press “Retry” (works with debug version of libraries) …
Where to get more information? OpenCV Wiki-pages:  http://opencvlibrary.sourceforge.net   Supplied documentation: OpenCV/docs/index.htm, faq.htm The forum:  [email_address]
Thank you! Questions?

More Related Content

PPTX
Eye mouse
PPTX
Human computer interaction-Memory, Reasoning and Problem solving
PDF
Support de cours Conception orientée objets - partie 1.pdf
PPTX
Introduction à l’architecture des ordinateurs
PPT
Cours langage c
PDF
Color based image processing , tracking and automation using matlab
PDF
TP1 Traitement d'images Génie Logiciel avec Matlab
PPTX
Microservices - it's déjà vu all over again
Eye mouse
Human computer interaction-Memory, Reasoning and Problem solving
Support de cours Conception orientée objets - partie 1.pdf
Introduction à l’architecture des ordinateurs
Cours langage c
Color based image processing , tracking and automation using matlab
TP1 Traitement d'images Génie Logiciel avec Matlab
Microservices - it's déjà vu all over again

What's hot (20)

PPTX
COM2304: Digital Image Fundamentals - I
PPTX
Lecture 1 for Digital Image Processing (2nd Edition)
DOCX
Eye ball cursor movement using opencv
PDF
Les Filtres Numeriques
PPTX
KEY FRAME SYSTEM-Ruby Stella mary.pptx
PPT
Introduction aux systèmes d-exploitation (2).ppt
ODP
image compression ppt
PPTX
Cs8092 computer graphics and multimedia unit 3
PPTX
Introduction to Image Compression
PDF
Programmation en C
DOCX
Spammer detection and fake user Identification on Social Networks
PPTX
Web services SOAP et REST
PPTX
Anti aliasing
PDF
Exam 15.02.2022.pdf
PDF
IMAGE PROCESSING - MATHANKUMAR.S - VMKVEC
PPTX
Architecture ordinateur-echange-de-donnees
PPTX
DDA algorithm
PDF
OpenCV Introduction
PPTX
Architecture des ordinateurs : memoires
PPT
Chap3 liaison de données
COM2304: Digital Image Fundamentals - I
Lecture 1 for Digital Image Processing (2nd Edition)
Eye ball cursor movement using opencv
Les Filtres Numeriques
KEY FRAME SYSTEM-Ruby Stella mary.pptx
Introduction aux systèmes d-exploitation (2).ppt
image compression ppt
Cs8092 computer graphics and multimedia unit 3
Introduction to Image Compression
Programmation en C
Spammer detection and fake user Identification on Social Networks
Web services SOAP et REST
Anti aliasing
Exam 15.02.2022.pdf
IMAGE PROCESSING - MATHANKUMAR.S - VMKVEC
Architecture ordinateur-echange-de-donnees
DDA algorithm
OpenCV Introduction
Architecture des ordinateurs : memoires
Chap3 liaison de données
Ad

Viewers also liked (7)

PDF
A basic introduction to open cv for image processing
PPTX
Who is ursula ellis
PPTX
Guide: How to Build OpenCV 3.0.0
PDF
PyCon 2012: Militarizing Your Backyard: Computer Vision and the Squirrel Hordes
PDF
Write good papers
PDF
Introduction to OpenCV (with Java)
PDF
I Rock Therefore I Am. 20 Legendary Quotes from Prince
A basic introduction to open cv for image processing
Who is ursula ellis
Guide: How to Build OpenCV 3.0.0
PyCon 2012: Militarizing Your Backyard: Computer Vision and the Squirrel Hordes
Write good papers
Introduction to OpenCV (with Java)
I Rock Therefore I Am. 20 Legendary Quotes from Prince
Ad

Similar to Open Cv 2005 Q4 Tutorial (20)

PDF
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PDF
Objective-C Runtime overview
PPT
Gdc09 Minigames
PPTX
Getting started with open cv in raspberry pi
KEY
OpenCVの基礎
PPTX
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PPT
Rcp by example
PDF
How to instantiate any view controller for free
PDF
Google's HTML5 Work: what's next?
ODP
Power ai image-pipeline
PPT
Threaded Programming
PDF
SPU gameplay
PDF
Intro to HTML5
PPTX
Moving from Jenkins 1 to 2 declarative pipeline adventures
PPT
Qt Programming on TI Processors
PDF
Forge - DevCon 2016: Visual Reporting with Connected Design Data
PPTX
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
PPT
Basic of Applet
PDF
426 lecture 4: AR Developer Tools
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
Objective-C Runtime overview
Gdc09 Minigames
Getting started with open cv in raspberry pi
OpenCVの基礎
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Rcp by example
How to instantiate any view controller for free
Google's HTML5 Work: what's next?
Power ai image-pipeline
Threaded Programming
SPU gameplay
Intro to HTML5
Moving from Jenkins 1 to 2 declarative pipeline adventures
Qt Programming on TI Processors
Forge - DevCon 2016: Visual Reporting with Connected Design Data
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Basic of Applet
426 lecture 4: AR Developer Tools

More from antiw (9)

PDF
Cvpr2010 open source vision software, intro and training part viii point clou...
PDF
Cvpr2010 open source vision software, intro and training part vii point cloud...
PDF
graphical models for the Internet
PDF
Recent Advances in Computer Vision
PDF
15 pieces of advice i wish my ph d advisor had given me
PDF
Randy pauschtimemanagement2007
PDF
Write a research paper howto - good presentation
PDF
15 pieces of advice i wish my ph d advisor had given me
PDF
Note beamer
Cvpr2010 open source vision software, intro and training part viii point clou...
Cvpr2010 open source vision software, intro and training part vii point cloud...
graphical models for the Internet
Recent Advances in Computer Vision
15 pieces of advice i wish my ph d advisor had given me
Randy pauschtimemanagement2007
Write a research paper howto - good presentation
15 pieces of advice i wish my ph d advisor had given me
Note beamer

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
NewMind AI Monthly Chronicles - July 2025
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx

Open Cv 2005 Q4 Tutorial

  • 1. Getting Started with OpenCV Vadim Pisarevsky (vadim.pisarevsky@intel.com)
  • 2. Agenda OpenCV facts and overall structure First steps Almighty HighGUI Using OpenCV within your program Now turn on IPP Dynamic structures Save your data Some useful OpenCV tips & tricks Where to get more information
  • 3. What is OpenCV? OpenCV stands for Open Source Computer Vision Library Being developed at Intel since 1999 Written in C/C++; Contains over 500 functions. Available on Windows, Linux and MacOSX. So far is extensively used in many companies and research centers Home page: www.intel.com/technology/computing/opencv/
  • 4. Can you use OpenCV? Extract from license: [Copyright Clause] Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistribution's of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistribution's in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of Intel Corporation may not be used to endorse or promote products derived from this software without specific prior written permission. [Disclaimer] In brief: Sure!
  • 5. Now the question is: Should you use it?
  • 6. OpenCV structure CXCORE basic structures and algoritms, XML support, drawing functions CV Image processing and vision algorithms HighGUI GUI, Image and Video I/O We will mostly focus on these two in this presentation
  • 7. First OpenCV Program 1. #include <cxcore.h> 2. #include <highgui.h> 3. #include <math.h> 4. int main( int argc, char** argv ) { 5. CvPoint center; 6. double scale=-3; 7. IplImage* image = argc==2 ? cvLoadImage(argv[1]) : 0; 8. if(!image) return -1; 9. center = cvPoint(image->width/2,image->height/2); 10. for(int i=0;i<image->height;i++) 11. for(int j=0;j<image->width;j++) { 12. double dx=(double)(j-center.x)/center.x; 13. double dy=(double)(i-center.y)/center.y; 14. double weight=exp((dx*dx+dy*dy)*scale); 15. uchar* ptr = &CV_IMAGE_ELEM(image,uchar,i,j*3); 16. ptr[0] = cvRound(ptr[0]*weight); 17. ptr[1] = cvRound(ptr[1]*weight); 18. ptr[2] = cvRound(ptr[2]*weight); } 19. cvSaveImage( “copy.png”, image ); 20. cvNamedWindow( &quot;test&quot;, 1 ); 21. cvShowImage( &quot;test&quot;, image ); 22. cvWaitKey(); 23. return 0; } Radial gradient
  • 8. How to build and run the program? Obtain the latest version of OpenCV: Get the official release (currently, OpenCV b5 for Windows = OpenCV 0.9.7 for Linux) at http://www.sourceforge.net/projects/opencvlibrary Get the latest snapshot from CVS: :pserver:anonymous@cvs.sourceforge.net:/cvsroot/opencvlibrary Install it: On Windows run installer, on Linux use “./configure + make + make install” (see INSTALL document) On Windows: open opencv.dsw, do “build all”, add opencv/bin to the system path; on Linux - see a) Build the sample: Windows: cl /I<opencv_inc> test.cpp /link /libpath:<opencv_lib_path> cxcore.lib cv.lib highgui.lib Create project for VS (see opencv/docs/faq.htm) or use opencv/samples/c/cvsample.dsp as starting point Linux: g++ -o test `pkg-config –cflags` test.cpp `pkg-config –libs opencv` Run it: test lena.jpg or ./test lena.jpg
  • 9. The Program Quick Review short and clear program, no need to mess with MFC/GTK/QT/…, cross-platform cvLoadImage() and cvSaveImage() provide the easiest way to save/load images of various formats. cvPoint and other “constructor” functions make the code shorter and allow 1-line functions call. CV_IMAGE_ELEM() – pretty fast way to access image pixels cvRound() is very fast and convenient way to cast float/double to int cvNamedWindow() creates “smart” window for viewing an image cvShowImage() shows image in the window cvWaitKey() delays the execution until key pressed or until the specified timeout is over 1. #include <cxcore.h> 2. #include <highgui.h> 3. #include <math.h> 4. int main( int argc, char** argv ) { 5. CvPoint center; 6. double scale=-3; 7. IplImage* image = argc==2 ? cvLoadImage (argv[1]) : 0; 8. if(!image) return -1; 9. center = cvPoint (image->width/2,image->height/2); 10. for(int i=0;i<image->height;i++) 11. for(int j=0;j<image->width;j++) { 12. double dx=(double)(j-center.x)/center.x; 13. double dy=(double)(i-center.y)/center.y; 14. double weight=exp((dx*dx+dy*dy)*scale); 15. uchar* ptr = & CV_IMAGE_ELEM (image,uchar,i,j*3); 16. ptr[0] = cvRound (ptr[0]*weight); 17. ptr[1] = cvRound (ptr[1]*weight); 18. ptr[2] = cvRound (ptr[2]*weight); } 19. cvSaveImage ( “copy.png”, image ); 20. cvNamedWindow ( &quot;test&quot;, 1 ); 21. cvShowImage ( &quot;test&quot;, image ); 22. cvWaitKey (); 23. return 0; }
  • 10. What else HighGUI can do? “ Smart” windows Image I/O, rendering Processing keyboard and other events, timeouts Trackbars Mouse callbacks Video I/O
  • 11. Windows cvNamedWindow(window_name, fixed_size_flag); creates window accessed by its name. Window handles repaint, resize events. Its position is remembered in registry: cvNamedWindow(“ViewA”,1); cvMoveWindow(“ViewA”,300,100); cvDestroyWindow(“ViewA”); … cvShowImage(window_name, image); copies the image to window buffer, then repaints it when necessary. {8u|16s|32s|32f}{C1|3|4} are supported. only the whole window contents can be modified. Dynamic updates of parts of the window are done using operations on images, drawing functions etc. On Windows native Win32 UI API is used Linux – GTK+ 2 MacOSX – X11 & GTK+ 2; Native Aqua support is planned.
  • 12. Image I/O IplImage* cvLoadImage(image_path, colorness_flag); loads image from file, converts to color or grayscle, if need, and returns it (or returns NULL). image format is determined by the file contents. cvSaveImage(image_path, image); saves image to file, image format is determined from extension. BMP, JPEG (via libjpeg), PNG (via libpng), TIFF (via libtiff), PPM/PGM formats are supported. IplImage* img = cvLoadImage(“picture.jpeg”,-1); if( img ) cvSaveImage( “picture.png”, img );
  • 13. Waiting for keys… cvWaitKey(delay=0); waits for key press event for <delay> ms or infinitely, if delay=0. The only function in highgui that process message queue => should be called periodically. Thanks to the function many highgui programs have clear sequential structure, rather than event-oriented structure, which is typical for others toolkits To make program continue execution if no user actions is taken, use cvWaitKey(<delay_ms!=0>); and check the return value // opencv/samples/c/delaunay.c … for(…) { … int c = cvWaitKey(100); if( c >= 0 ) // key_pressed break; } delaunay.c, 240 lines
  • 14. Trackbars cvCreateTrackbar(trackbar_name,window_name, position_ptr,max_value,callback=0); creates trackbar and attaches it to the window. Value range 0..max_value. When the position is changed, the global variable updated and callback is called, if specified. // opencv/samples/c/morphology.c int dilate_pos=0; // initial position value void Dilate(int pos) { … cvShowImage( “E&D”, erode_result ); } int main(…){ … cvCreateTrackbar(“Dilate”,”E&D”, &dilate_pos,10,Dilate); … cvWaitKey(0); // check for events & process them ...} morphology.c, 126 lines
  • 15. Mouse Events cvSetMouseCallback(window_name, callback, userdata=0); sets callback on mouse events (button clicks, cursor moves) for the specified window // opencv/samples/c/lkdemo.c void on_mouse(int event,int x,int y,int flags, void* param) { … } int main(…){ … cvSetMouseCallback(“LkDemo”,on_mouse,0); … cvWaitKey(0); // check for events & process them … } Scroll forward for example 
  • 16. Video I/O API CvCapture* cvCaptureFromCAM(camera_id=0); initializes capturing from the specified camera CvCapture* cvCaptureFromFile(videofile_path); initializes capturing from the video file. IplImage* cvQueryFrame(capture); retrieves the next video frame (do not alter the result!), or NULL if there is no more frames or an error occured. cvGetCaptureProperty(capture, property_id); cvSetCaptureProperty(capture, property_id, value); retrieves/sets some capturing properties (camera resolution, position within video file etc.) cvReleaseCapture(&capture); do not forget to release the resouces at the end! Used interfaces: Windows: VFW, IEEE1394, MIL, DShow (in progress), Quicktime (in progress) Linux: V4L2, IEEE1394, FFMPEG MacOSX: FFMPEG, Quicktime (in progress)
  • 17. Video I/O Example // opencv/samples/c/lkdemo.c int main(…){ … CvCapture* capture = <…> ? cvCaptureFromCAM(camera_id) : cvCaptureFromFile(path); if( !capture ) return -1; for(;;) { IplImage* frame=cvQueryFrame(capture); if(!frame) break; // … copy and process image cvShowImage( “LkDemo”, result ); c=cvWaitKey(30); // run at ~20-30fps speed if(c >= 0) { // process key }} cvReleaseCapture(&capture);} lkdemo.c, 190 lines (needs camera to run)
  • 18. Using OpenCV in User Apps Case 1. Least squares (real example from OpenCV itself). // was (A – NxN matrix, b – Nx1 right-side vector, x – solution): some_old_least_sq_func_32f( /* float* */ A, /* int */ N, /* int */ N, /* float* */ b, /* float* */ x); // has been converted to: { CvMat _A=cvMat(N,N,CV_32F,A), _b=cvMat(N,1,CV_32F,b), _x=cvMat(N,1,CV_32F,x); cvSolve( &_A, &_b, &_x, CV_SVD ); } Advantages: error handling size and type checking easier 32f<->64f conversion
  • 19. Using OpenCV in User Apps (II) Case 2. Film Grain DirectShow filter prototype. // inside DirectShow filter Transform method … pMediaSample->GetPointer(&pData); AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType(); BITMAPINFOHEADER *bh = &((VIDEOINFOHEADER *)pType->pbFormat)->bmiHeader; IplImage* img = cvCreateImageHeader(cvSize(bh.biWidth,bh.biHeight), 8, 3); cvSetData( img, pData, (bh.biWdith*3 + 3) & -4 ); cvCvtColor( img, img, CV_BGR2YCrCb ); IplImage* Y=cvCreateImage(cvGetSize(img),8,1); cvSplit(img,Y,0,0,0); IplImage* Yf=cvCreateImage(cvGetSize(img),32,1); CvRNG rng=cvRNG(<seed>); cvRandArr(&rng,Yf,cvScalarAll(0),cvScalarAll(GRAIN_MAG),CV_RAND_NORMAL); cvSmooth(Yf,Yf,CV_GAUSSIAN,GRAIN_SIZE,0,GRAIN_SIZE*0.5); cvAcc(Y, Yf); cvConvert(Yf,Y); cvMerge(Y,0,0,0,img); cvCvtColor( img, img, CV_YCrCb2BGR ); cvReleaseImage(&Yf); cvReleaseImage(&Y); cvReleaseImageHeader(&img);…
  • 20. Using OpenCV in User Apps (III) Case 3. Visualization inside some test program (hypothetical example,yet quite real too). // was … /* some image processing code containing bug */ … // has been converted to … … /* at this point we have the results */ #if VISUALIZE #include <highgui.h> #pragma comment(“lib”,”cxcore.lib”) #pragma comment(“lib”,”highgui.lib”) CvMat hdr; cvInitMatHeader (&hdr,200/*height*/,320/*width*/,CV_8UC3, data_ptr); CvMat* vis_copy=cvCloneMat(&hdr); cvNamedWindow(“test”,1); cvCircle (vis_copy, cvCenter(bug_x,bug_y), 30, CV_RGB(255,0,0), 3, CV_AA ); // mark the suspicious area cvShowImage(“test”,vis_copy); cvWaitKey (0 /* or use some delay, e.g. 1000 */); // cvSaveImage( “buggy.png”, vis_copy ); // if need, save for post-mortem analysis // cvDestroyWindow (“test”); // if need #endif
  • 21. What can be drawn in image (besides circles)? cxcore provides numerous drawing functions: Lines, Circles, Ellipses, Elliptic Arcs Filled polygons or polygonal contours Text (using one of embedded fonts) Everything can be drawn with different colors, different line width, antialiasing on/off Arbitrary images types are supported (for depth!=8u antializing is off)
  • 22. Using OpenCV with IPP #include <cv.h> #include <highgui.h> #include <ipp.h> #include <stdio.h> int main(int,char**){ const int M=3; IppiSize msz={M,M}; IppiPoint ma={M/2,M/2}; IplImage* img=cvLoadImage(“lena.jpg”,1); IplImage* med1= cvCreateImage (cvGetSize(img),8,3); IplImage* med2= cvCloneImage (med1); int64 t0 = cvGetTickCount() ,t1,t2; IppiSize sz = {img->width-M+1,img->height-M+1}; double isz=1./(img->width*img->height); cvSmooth (img,med1,CV_MEDIAN,M); // use IPP via OpenCV t0= cvGetTickCount ()-t0; cvUseOptimized (0); // unload IPP t1 = cvGetTickCount (); cvSmooth (img,med1,CV_MEDIAN,M); // use C code t1= cvGetTickCount ()-t1; t2= cvGetTickCount (); ippiMedianFilter_8u_C3R ( // use IPP directly & CV_IMAGE_ELEM (img,uchar,M/2,M/2*3), img->widthStep, & CV_IMAGE_ELEM (med1,uchar,M/2,M/2*3), med1->widthStep, sz, msz, ma ); t2= cvGetTickCount ()-t2; printf(“t0=%.2f, t1=%.2f, t2=%.2f\n”, (double)t0*isz,(double)t1*isz,(double)t2*isz); return 0; } Triple 3x3 median filter
  • 23. How does it works? (Quite Similar to IPP dispatcher mechanism) // cv/src/_cvipp.h … IPCVAPI_EX(CvStatus, icvFilterMedian_8u_C3R, “ippiFilterMedian_8u_C3R”, CV_PLUGINS1(CV_PLUGIN_IPPI), (const void* src, int srcstep, void* dst, int dststep, CvSize roi, CvSize ksize, CvPoint anchor )) … // cv/src/cvswitcher.cpp … #undef IPCVAPI_EX #define IPCVAPI_EX() … static CvPluginFuncInfo cv_ipp_tab[] = { #undef _CV_IPP_H_ /* with redefined IPCVAPI_EX every function declaration turns to the table entry */ #include &quot;_cvipp.h“ #undef _CV_IPP_H_ {0, 0, 0, 0, 0} }; static CvModuleInfo cv_module = { 0, &quot;cv&quot;, &quot;beta 5 (0.9.7)&quot;, cv_ipp_tab }; static int loaded_functions = cvRegisterModule( &cv_module ); … // cv/src/cvsmooth.cpp icvFilterMedian_8u_C3R_t icvFilterMedian_8u_C3R_p = 0; void cvSmooth() { if( icvFilterMedian_8u_C3R_p ) { /* use IPP */ } else { /* use C code */… }
  • 24. Dynamic Structures (when 2d array is not enough) The sample task: collect the locations of all non-zero pixels in the image. Where to store the locations? Possible solutions: Allocate array of maximum size (sizeof(CvPoint)*width*height – a huge value) Use two-pass algorithm Use list or similar data structure … (Any other ideas?) // construct sequence of non-zero pixel locations CvSeq* get_non_zeros( const IplImage* img, CvMemStorage* storage ) { CvSeq * seq = cvCreateSeq ( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage ); for( int i = 0; i < img->height; i++ ) for( int j = 0; j < img->width; j++ ) if( CV_IMAGE_ELEM(img,uchar,i,j) ) { CvPoint pt={j,i}; cvSeqPush ( seq, &pt ); } return seq; }
  • 25. Memory Storages … Memory storage is a linked list of memory blocks. Data is allocated in the storage by moving “free space” position, new blocks are allocated and added to the list if necessary It’s only possible to free the continous block of data by moving position back, thus both allocation and deallocation are virtually instant(!) Functions to remember: cvCreateMemStorage(block_size), cvReleaseMemStorage(storage); cvClearMemStorage(storage); cvMemStorageAlloc(storage,size); All OpenCV “dynamic structures” reside in memory storages. The model is very efficient for repetitive processing, where cvClearMemStorage is called on top level between iterations.
  • 26. Sequences Sequence is a linked list of memory blocks (sounds familiar, eh?). Large sequences may be split into multiple storage blocks, while several small sequences may fit into the single storage block. Sequence is deque: adding/removing elements to/from either end is O(1) operation, inserting/removing elements from the middle is O(N). Accessing arbitrary element is also ~O(1) (when storage block size >> sequence block) Sequences can be sequentially read/written using readers/writers (similar to STL’s iterators) Sequences can not be released, use cv{Clear|Release}MemStorage () Basic Functions to remember: cvCreateSeq(flags,header_size,element_size,storage), cvSeqPush[Front](seq,element), cvSeqPop[Front](seq,element), cvClearSeq(seq), cvGetSeqElem(seq,index), cvStartReadSeq(seq,reader), cvStartAppendToSeq(seq,writer ).
  • 27. Sets and Sparse Matrices Set is variant of sequence with “free node list” and it’s active elements do not follow each other – that is, it is convenient way to organize a “heap” of same-size memory blocks. Sparse matrix in OpenCV uses CvSet for storing elements. // Collecting the image colors CvSparseMat* get_color_map( const IplImage* img ) { int dims[] = { 256, 256, 256 }; CvSparseMat * cmap = cvCreateSparseMat (3, dims, CV_32SC1); for( int i = 0; i < img->height; i++ ) for( int j = 0; j < img->width; j++ ) { uchar* ptr=&CV_IMAGE_ELEM(img,uchar,i,j*3); int idx[] = {ptr[0],ptr[1],ptr[2]}; ((int*) cvPtrND (cmap,idx))[0]++; } // print the map CvSparseMatIterator it; for( CvSparseNode *node = cvInitSparseMatIterator ( mat, &iterator ); node != 0; node = cvGetNextSparseNode ( &iterator )) { int* idx = CV_NODE_IDX (cmap,node); int count=*(int*) CV_NODE_VAL (cmap,idx); printf( “(b=%d,g=%d,r=%d): %d\n”, idx[0], idx[1], idx[2], count ); } return cmap; }
  • 28. Save your data (to load it then) Need a config file? Want to save results of your program to pass it to another one, or look at it another day? It all can be easily done with OpenCV persistence-related functions. // somewhere deep in your code… you get 5x5 // matrix that you’d want to save { CvMat A = cvMat( 5, 5, CV_32F, the_matrix_data ); cvSave ( “my_matrix.xml”, &A ); } // to load it then in some other program use … CvMat* A1 = (CvMat*) cvLoad ( “my_matrix.xml” ); <?xml version=&quot;1.0&quot;?> <opencv_storage> <my_matrix type_id=&quot;opencv-matrix&quot;> <rows>5</rows> <cols>5</cols> <dt>f</dt> <data> 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1.</data></my_matrix> </opencv_storage> my_matrix.xml
  • 29. So what about config file? CvFileStorage * fs = cvOpenFileStorage (“cfg.xml”, 0, CV_STORAGE_WRITE ); cvWriteInt ( fs, “frame_count”, 10 ); cvWriteStartWriteStruct ( fs, “frame_size”, CV_NODE_SEQ); cvWriteInt( fs, 0, 320 ); cvWriteint( fs, 0, 200 ); cvEndWriteStruct (fs); cvWrite ( fs, “color_cvt_matrix”, cmatrix ); cvReleaseFileStorage ( &fs ); Writing config file CvFileStorage* fs = cvOpenFileStorage(“cfg.xml”, 0, CV_STORAGE_READ); int frame_count = cvReadIntByName ( fs, 0, “frame_count”, 10 /* default value */ ); CvSeq* s = cvGetFileNodeByName (fs,0,”frame_size”)->data.seq; int frame_width = cvReadInt ( (CvFileNode*)cvGetSeqElem(s,0) ); int frame_height = cvReadInt( (CvFileNode*)cvGetSeqElem(s,1) ); CvMat* color_cvt_matrix = (CvMat*)cvRead(fs,0,”color_cvt_matrix”); cvReleaseFileStorage( &fs ); Reading config file <?xml version=&quot;1.0&quot;?> <opencv_storage> <frame_count>10</frame_count> <frame_size>320 200</frame_size> <color_cvt_matrix type_id=&quot;opencv-matrix&quot;> <rows>3</rows> <cols>3</cols> <dt>f</dt> <data>…</data></color_cvt_matrix> </opencv_storage> cfg.xml
  • 30. Some OpenCV Tips and Tricks Use short inline “constructor” functions: cvScalar, cvPoint, cvSize, cvMat etc. Use cvRound and vector math functions ( cvExp, cvPow … ) for faster processing Use cvStackAlloc for small buffers Consider CV_IMPLEMENT_QSORT_EX() as possible alternative to qsort() & STL sort(). To debug OpenCV program when runtime error dialog error pops up, press “Retry” (works with debug version of libraries) …
  • 31. Where to get more information? OpenCV Wiki-pages: http://opencvlibrary.sourceforge.net Supplied documentation: OpenCV/docs/index.htm, faq.htm The forum: [email_address]