Josiah Yoder
Email: <yoder2 AT SPAMFREE purdue DOT edu>
I have used OpenCV in my own research area, which is multi-camera computer vision. I think one of the best features in OpenCV is the face detection algorithm and the associated classifier training application.
I created this account because I wish to help make the OpenCV documentation more friendly for first-time users.
My homepage at http://web.ics.purdue.edu/~yoder2/
Stuff to be added (Useful even with C++-style OpenCV2.3.1)
Documentation (long-term)
- Figure out proper header for each function and include in the documentation.
Mat: Is the << constructor row-first or column-first?
"See alsos" for my favorite functions
cvConvert: See also:
cvCvtColor -- To covert from one color scheme to another. For example, to convert a color image to grayscale, use cvCvtColor(cimg,gimg,CV_BGR2GRAY). (Note that it is not CV_BGR2Gray)
cvCopy: See also:
- cvClone -- To allocate the memory and create an exact copy of an image
- cvConvert -- To convert from one image type to another with a different depth
- cvConvertScale -- Same as cvConvert, only has additional argument to change pixel intensities
- cvResize -- To copy to a smaller image, or a sub-image of another image. (To select the sub-image, use cvSetImageROI)
cvGEMM: Generalized Matrix Multiplication
- cvAddWeighted: Generalized Element-wise Addition
cvMul: Elementwise multiplication
- cvMatMul: Matrix multiplication
cvScale: Scales intensities
- cvResize -- Scales geometry
cvZero: See also:
- cvSet -- To set to a color other than black
Stuff to be Added (C-style)
"Hello World" in OpenCV
Create and show a blank image:
#include <opencv/cv.h>
#include <opencv/highgui.h>
- int main (int argc, char ** argv) {
IplImage * image = cvCreateImage(cvSize(40,100),IPL_DEPTH_8U,3);
- cvZero(image);
- cvNamedWindow("main",CV_WINDOW_AUTOSIZE);
- cvShowImage("main",image);
- cvWaitKey(0);
- return 0;
- }
Accessing pixels in OpenCV
I use cvmGet and cvmSet when my image is compatible. Otherwise, I use cvGet2D or cvSet2D.
Try this code in the "Hello World" example above:
#include <iostream>
- using namespace std;
- ...
- cvSet2D(image,10,20,CV_RGB(255,0,0));
cout << cvGet2D(image,10,20).val[0] << " ";
cout << cvGet2D(image,10,20).val[1] << " ";
cout << cvGet2D(image,10,20).val[2] << endl;
Notice how the CV_RGB puts the bytes in the reverse (BGR) order used by OpenCV.
Concerned about speed? Consider this quote:
- Rules of Optimization: Rule 1: Don’t do it. Rule 2 (for experts only): Don’t do it yet. – M.A. Jackson.
OpenCV structures
OpenCV has several small, useful structures which appear often. Examples are the structures CvScalar, CvRectangle, and CvPoint. Whenever a function calls for one of these, you can declare it inline if you wish, by using the lower-case "pseudo-constructors:" cvScalar(x), cvRectangle(x,y,w,h), and cvPoint(x,y). For a colored pixel, you can use CV_RGB(r,g,b), which creates a CvScalar object with the red, blue, and green in the reverse (BGR) order used by default in OpenCV.
CvSeq's
CvSeq seems useful, but not sure how to use it with your own data? The source code for cvHaarDetectObjects has a nice example. To initialize, just use
CvMemStorage* temp_storage = 0;
cvCreateSeq( 0, sizeof(CvSeq), sizeof(MyType), temp_storage);
To clean up, use
cvReleaseMemStorage( &temp_storage );
There is no cvReleaseSeq. To set the length of the sequence to zero, you can use cvClearSeq. But this does not free the memory. It seems valuable to quote the OpenCV source here:
Clears memory storage. This is the only way(!!!) (besides cvRestoreMemStoragePos) to reuse memory allocated for the storage - cvClearSeq,cvClearSet do not free any memory. A child storage returns all the blocks to the parent when it is cleared
CVAPI(void) cvClearMemStorage( CvMemStorage* storage );
When using cvPartitionFunction, you do not need to initialize the index sequence. If you do initialize it, it will be leaked inside of the storage. In other words, that memory will no longer be available for use until that CvStorage object is released.
Advice I learned the hard way: Only put structures onto a CvSeq which are "complete" in the sense that they don't contain pointers to other data. If you put on a local variable, its contents will mysteriously change. If you put on a "new" variable, it will be memory-leaked. This took me about 3 to 4 hours to figure out.
External Links
The IU version of the OpenCV function documentation: http://www.cs.indiana.edu/cgi-pub/oleykin/website/OpenCVHelp/
Gady Agam's Introduction to programming with OpenCV: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html