Distance
1. OpenCV overview and function Introduction
OpenCV is an Intel open-source computer vision library. It consists of a series of C functions and a small number of C ++ classes, implementing many common algorithms for image processing and computer vision. OpenCV has a cross-platform medium and high-level API that includes more than 300 C functions. It does not rely on other external libraries-although some external libraries can also be used.
OpenCV is FREE for non-commercial and commercial applications. (For details, refer to license ). Code: http://www.sourceforge.net/projects/opencvlibrary
OpenCV provides transparent interfaces for Intel Integrated Performance Primitives (IPP. This means that if you have an IPP library optimized for a specific processor, OpenCV will automatically load these libraries at runtime. For more information about IPP, see: http://www.intel.com/software/products/ipp/index.htm
It has the following features:
1) Open C/C ++ source code
2) optimization code developed based on Intel processor Instruction Sets
3) Unified Structure and Function Definition
4) powerful image and matrix computing capabilities
5) convenient and flexible user interfaces
6) supports both MS-WINDOWS and LINUX platforms.
As an open-source project for basic computer vision, image processing, and pattern recognition, OPENCV can be directly applied in many fields and used as an ideal tool for the second development.
OpenCV functions:
OpenCV contains the following parts:
Cxcore: basic functions (basic operations for various data types ).
Cv: image processing and computer vision (image processing, structure analysis, motion analysis, object tracking, pattern recognition, camera calibration)
Ml: The machine learning module. Currently, the main content is classifier.
Cvaux: some experimental functions (ViewMorphing, 3D tracking, PCA, HMM)
Highgui: user interaction (GUI, image video I/O, system call function)
Ii. OpenCV Installation
OpenCV2.0 is just released. To install OpenCV2.0 in VC 2008 Express, see:
Http://www.opencv.org.cn/index.php/VC_2008_Express%E4%B8%8B%E5% AE %89%E8%A3%85OpenCV2.0
III. Basic Knowledge:
1. opencv data type conversion operation summary
(1) data format conversion in images or Arrays:
CvConvert (image, image_temp );
CvConvertScale (const CvArr * src, CvArr * dst, double scale CV_DEFAULT (1), double shift CV_DEFAULT (0 ));
CvScale (src, dst );
// Converts CvArr (IplImage or CvMat,...) to CvMat.
CvGetMat (const CvArr * arr, CvMat * header, int * coi CV_DEFAULT (NULL), int allowND CV_DEFAULT (0 ));
CvCopy (const CvArr * src, CvArr * dst, const CvArr * mask); // extract unregulated Images
(2) convert a multi-channel image into an array of data
CvGetMat (const CvArr * array, CvMat * mat, int * pCOI, int allowND)
CvCopy (img, mat );
// Converts CvArr (IplImage or CvMat,...) to CvMat.
CvGetMat (const CvArr * arr, CvMat * header, int * coi CV_DEFAULT (NULL), int allowND CV_DEFAULT (0 ));
(3) convert data in the array into multi-channel Images
CvCopy (const CvArr * src, CvArr * dst, const CvArr * mask = NULL );
CvGetMat (const CvArr * arr, CvMat * header, int * coi CV_DEFAULT (NULL), int allowND CV_DEFAULT (0 ));
2. Discovery of the binarization functions cvAdaptiveThreshold and cvThreshold
Adaptive binarization is used to calculate the average gray scale of the pixel neighborhood to determine the binarization value. If the entire area is almost the same in gray scale, the appropriate results cannot be given. The reason why the image looks like edge detection is because the size of the window is small, you can try it later.
CvAdaptiveThreshold (src, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 21); // The window is set to 21.
There is no universal binarization method. The specific problem is analyzed and the adaptive binarization method is very effective for text and bar code with uneven illumination. Select the window size to consider the size of the detected object. The threshold value in adaptive threshold is determined by the selected neighborhood. If the selected neighborhood is very small (such as 3 × 3 ), obviously, the "degree of adaptability" of the threshold is very high, which shows the edge detection effect in the result image. If the value of the neighboring area is relatively large (such as 31 × 31), the "adaptive degree" of the threshold is relatively low, which shows a binarization effect in the result image.
3. Use gabor and AdaBoost (MultiBoost) for Target Detection Image Recognition
Http://www.opencv.org.cn/forum/viewtopic.php? F = 10 & t = 7790
4. Video Tracking Methods
What I know about the tracking method is KLMAN filter. particle filter. camshift. meanshift.
Http://www.codesoso.com/code/mean_shift.aspx Based on Mean Shift
Http://arslan-ai.spaces.live.com/blog/cns! CAE7EF891A2218BA! 123. entry
5. How to access image pixels
(Coordinates start from 0 and are relative to the image origin. The image origin is either in the upper left corner (img-> origin = IPL_ORIGIN_TL) or in the lower left corner (img-> origin = IPL_ORIGIN_BL ))
Assume that there is an 8-bit 1-channel image I (IplImage * img ):
---------------------------------------------------------------------
I (x, y )~ (Uchar *) (img-> imageData + img-> widthStep * y) [x]
---------------------------------------------------------------------
Assume that there is an 8-bit 3-channel image I (IplImage * img ):
---------------------------------------------------------------------
I (x, y) blue ~ (Uchar *) (img-> imageData + img-> widthStep * y) [x * 3]
I (x, y) green ~ (Uchar *) (img-> imageData + img-> widthStep * y) [x * 3 + 1]
I (x, y) red ~ (Uchar *) (img-> imageData + img-> widthStep * y) [x * 3 + 2]
------------------------------------------------------------------------------
For example, if you increase the brightness of a vertex (100,100) by 30, you can do this:
------------------------------------------------------------------------------
CvPoint pt ={ 100,100 };
(Uchar *) (img-> imageData + img-> widthStep * pt. y) [pt. x * 3] + = 30;
(Uchar *) (img-> imageData + img-> widthStep * pt. y) [pt. x * 3 + 1] + = 30;
(Uchar *) (img-> imageData + img-> widthStep * pt. y) [pt. x * 3 + 2] + = 30;
-----------------------------------------------------------------------------
Or more efficiently:
-----------------------------------------------------------------------------
CvPoint pt ={ 100,100 };
Uchar * temp_ptr = & (uchar *) (img-> imageData + img-> widthStep * pt. y) [pt. x * 3];
Temp_ptr [0] + = 30;
Temp_ptr [1] + = 30;
Temp_ptr [2] + = 30;
-----------------------------------------------------------------------------
Assume that there are 32-bit floating point numbers and 1-channel image I (IplImage * img ):
-----------------------------------------------------------------------------
I (x, y )~ (Float *) (img-> imageData + img-> widthStep * y) [x]
-----------------------------------------------------------------------------
Now, in general, assume that there are N-channels and the type is T:
-----------------------------------------------------------------------------
I (x, y) c ~ (T *) (img-> imageData + img-> widthStep * y) [x * N + c]
-----------------------------------------------------------------------------
You can use macro CV_IMAGE_ELEM (image_header, elemtype, y, x_Nc)
-----------------------------------------------------------------------------
I (x, y) c ~ CV_IMAGE_ELEM (img, T, y, x * N + c)
-----------------------------------------------------------------------------
There are also functions for various images (including 4-channel images) and matrices (cvGet2D, cvSet2D), but they are very slow.
6. How to access matrix elements?
The method is similar (the example below is for columns and rows starting from 0)
Real Number Matrix M (CvMat * mat) with 32-bit floating point number ):
----------------------------------------------------------------------------
M (I, j )~ (Float *) (mat-> data. ptr + mat-> step * I) [j]
----------------------------------------------------------------------------
The complex matrix M (CvMat * mat) with a 64-bit floating point number ):
----------------------------------------------------------------------------
Re M (I, j )~ (Double *) (mat-> data. ptr + mat-> step * I) [j * 2]
Im M (I, j )~ (Double *) (mat-> data. ptr + mat-> step * I) [j * 2 + 1]
----------------------------------------------------------------------------
For a single-channel matrix, the macro CV_MAT_ELEM (matrix, elemtype, row, col), for example, 32-bit
Real Number Matrix of floating point:
M (I, j )~ CV_MAT_ELEM (mat, float, I, j ),
For example, here is the initialization of a 3x3 unit matrix:
CV_MAT_ELEM (mat, float, 0, 0) = 1.f;
CV_MAT_ELEM (mat, float, 0, 1) = 0.f;
CV_MAT_ELEM (mat, float, 0, 2) = 0.f;
CV_MAT_ELEM (mat, float, 1, 0) = 0.f;
CV_MAT_ELEM (mat, float, 1, 1) = 1.f;
CV_MAT_ELEM (mat, float, 1, 2) = 0.f;
CV_MAT_ELEM (mat, float, 2, 0) = 0.f;
CV_MAT_ELEM (mat, float, 2, 1) = 0.f;
CV_MAT_ELEM (mat, float, 2, 2) = 1.f;
7. How to process my own data in OpenCV
You have 300x200 32-bit floating point number image/array, that is, an array with 60000 elements.
----------------------------------------------------------------------------
Int cols = 300, rows = 200;
Float * myarr = new float [rows * cols];
// Step 1: Initialize the CvMat Header
CvMat mat = cvMat (rows, cols,
CV_32FC1, // 32-bit floating point single channel type
Myarr // User Data Pointer (data is not copied)
);
// Step 2: Use the cv function, for example, calculate the l2 (Frobenius) modulo.
Double norm = cvNorm (& mat, 0, CV_L2 );
...
Delete myarr;
Other cases are described in the reference manual. See cvCreateMatHeader, cvInitMatHeader, cvCreateImageHeader, cvSetData, etc.
8. How to read and display images
----------------------------------------------------------------------------
/* Usage: prog <image_name> */
# Include "cv. h"
# Include "highgui. h"
Int main (int argc, char ** argv)
{
IplImage * img;
If (argc = 2 & (img = cvLoadImage (argv [1], 1 ))! = 0)
{
CvNamedWindow ("Image view", 1 );
CvShowImage ("Image view", img );
CvWaitKey (0); // It is very important to include an event processing cycle.
CvDestroyWindow ("Image view ");
CvReleaseImage (& img );
Return 0;
}
Return-1;
}
9. Image Channels
Describe a pixel. If it is grayscale, you only need a numerical value to describe it, which is a single channel. If a pixel has three colors of RGB, it is three channels. The 4-channel is usually RGBA, which may be used in some processing. 2-channel images are not common and are usually used in program processing. For example, Fourier transformation may be used. One channel is a real number, and one channel is a virtual number, which is mainly convenient for programming.
10. HBITMAP converts IplImage and IplImage to DIB.
// HBITMAP converts IplImage
IplImage * hBitmap2Ipl (HBITMAP hBmp)
{
BITMAP bmp;
: GetObject (hBmp, sizeof (BITMAP), & bmp );
Int nChannels = bmp. bmBitsPixel = 1? 1: bmp. bmBitsPixel/8;
Int depth = bmp. bmBitsPixel = 1? IPL_DEPTH_1U: IPL_DEPTH_8U;
IplImage * img = cvCreateImageHeader (cvSize (bmp. bmWidth, bmp. bmHeight)
, Depth, nChannels );
Img-> imageData =
(Char *) malloc (bmp. bmHeight * bmp. bmWidth * nChannels * sizeof (char ));
Memcpy (img-> imageData, (char *) (bmp. bmBits), bmp. bmHeight * bmp. bmWidth * nChannels );
Return img;
}
Void createDIB (IplImage * & pict ){
IplImage * Red = cvCreateImage (cvSize (IMAGE_WIDTH, IMAGE_HEIGHT ),
IPL_DEPTH_8U, 1 );
IplImage * Green = cvCreateImage (cvSize (IMAGE_WIDTH, IMAGE_HEIGHT ),
IPL_DEPTH_8U, 1 );
IplImage * Blue = cvCreateImage (cvSize (IMAGE_WIDTH, IMAGE_HEIGHT ),
IPL_DEPTH_8U, 1 );
CvSetImageCOI (pict, 3 );
CvCopy (pict, Red );
CvSetImageCOI (pict, 2 );
CvCopy (pict, Green );
CvSetImageCOI (pict, 1 );
CvCopy (pict, Blue );
// Initialize the BMP display buffer
Bmi = (BITMAPINFO *) buffer;
Bmih = & (bmi-> bmiHeader );
Memset (bmih, 0, sizeof (* bmih ));
Bmih-> biSize = sizeof (BITMAPINFOHEADER );
Bmih-> biWidth = IMAGE_WIDTH;
Bmih-> biHeight = IMAGE_HEIGHT; //-IMAGE_HEIGHT;
Bmih-> biPlanes = 1;
Bmih-> biCompression = BI_RGB;
Bmih-> biBitCount = 24;
Palette = bmi-> bmiColors;
For (int I = 0; I <256; I ++ ){
Palette [I]. rgbBlue = palette [I]. rgbGreen = palette [I]. rgbRed =
(BYTE) I;
Palette [I]. rgbReserved = 0;
}
CvReleaseImage (& Red );
CvReleaseImage (& Green );
CvReleaseImage (& Blue );
}
// HBITMAP converts DIB
HBITMAP plIamgeToDIB (IplImage * pImg, int Size)
{
HDC hDC =: CreateCompatibleDC (0 );
BYTE tmp [sizeof (BITMAPINFO) + 255*4];
BITMAPINFO * bmi = (BITMAPINFO *) tmp;
HBITMAP hBmp;
Int I;
Memset (bmi, 0, sizeof (BITMAPINFO ));
Bmi-> bmiHeader. biSize = sizeof (BITMAPINFOHEADER );
Bmi-> bmiHeader. biWidth = pImg-> width;
Bmi-> bmiHeader. biHeight =-pImg-> height;
Bmi-> bmiHeader. biPlanes = Size;
Bmi-> bmiHeader. biBitCount = pImg-> nChannels * pImg-> depth;
Bmi-> bmiHeader. biCompression = BI_RGB;
Bmi-> bmiHeader. biSizeImage = pImg-> width * pImg-> height * 1;
Bmi-> bmiHeader. biClrImportant = 0;
Switch (pImg-> nChannels * pImg-> depth)
{
Case 8:
For (I = 0; I <256; I ++)
{
Bmi-> bmiColors [I]. rgbBlue = I;
Bmi-> bmiColors [I]. rgbGreen = I;
Bmi-> bmiColors [I]. rgbRed = I;
}
Break;
Case 32:
Case 24:
(DWORD *) bmi-> bmiColors) [0] = 0x00FF0000;/* red mask */
(DWORD *) bmi-> bmiColors) [1] = 0x0000FF00;/* green mask */
(DWORD *) bmi-> bmiColors) [2] = 0x000000FF;/* blue mask */
Break;
}
HBmp =: CreateDIBSection (hDC, bmi, DIB_RGB_COLORS, NULL, 0, 0 );
SetDIBits (hDC, hBmp, 0, pImg-> height, pImg-> imageData, bmi, DIB_RGB_COLORS );
: DeleteDC (hDC );
Return hBmp;
}
11. Image Segmentation
Make Watershed Image Segmentation: cvWatershed
Meanshift Image Segmentation: PyrMeanShiftFiltering
Use a pyramid to achieve image segmentation: cvPyrSegmentation
Http://blog.csdn.net/gnuhpc/archive/2009/06/21/4286186.aspx
Dajin algorithm threshold segmentation: http://hi.baidu.com/lazycat3611/blog/item/491febde06bc605d94ee37e8.html
Maximum Entropy Threshold Segmentation Algorithm: http://www.aiseminar.cn/html/00/t-700.html
12. Edge Detection
Cvkan: edge detection is performed using the Canny algorithm.
CvLaplace: laplace Edge Detection
Http://www.mvonline.com.cn/bbs/simple/index.php? T2421.html
CvSobel: Sobel Edge Detection
CvCornerHarris: Harris Edge Detection
13. Matching
CvCalcEMD2: calculates the minimum working distance between two weighted point sets.
CvMatchShapes: compare two shapes
CvMatchTemplate: Compare templates and overlapping image regions
Based on opencv sift Image Matching Algorithm vc ++ Source: http://codechina.net/source/620393
14. Classifier
Boosted classifier: There are four types of boosting techniques: Discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost.
The HAAR classifier is based on the haar wavelet operation.
Neural Network Classifier
SVM classifier. SVM is a classifier. The original SVM is a classifier of two types. A classifier of multiple classes can be combined by or 1: n. Native use of core functions supports classification of high-dimensional data. In a geometric sense, it is to find the vectors that best represent classification features (Support Vector SV), and then find a line to maximize the Margin of classification. LibSVM is a good implementation.
Http://blog.csdn.net/byxdaz/archive/2009/11/28/4893935.aspx
15. How to Use OpenCV to train your Classifier
Http://blog.csdn.net/byxdaz/archive/2009/11/30/4907211.aspx
16. Moving Target Tracking and Detection
CamShift:
MeanShift:
Http://blog.csdn.net/xauatnwpu/archive/2009/10/29/4743058.aspx
17. Target Detection
Target: http://wenjuanhe.blog.163.com/blog/static/745017252009102101728454/
Code Analysis for Face Detection:
Http://wenjuanhe.blog.163.com/blog/static/74501725200910391512151/
Rapid Target Detection of cascade promotion classifier based on Haar-like features:
Http://wenjuanhe.blog.163.com/blog/static/7450172520091039180911/
18. straight lines, circles, and rectangles
Detection Line: cvHoughLines, cvHoughLines2
Check circle: cvHoughCircles
Check rectangle: there is no corresponding function in opencv. The following code can detect the rectangle by first finding a straight line and then finding the four lines parallel to the vertical line.
Http://blog.csdn.net/byxdaz/archive/2009/12/01/4912136.aspx
4. Book recommendations:
Most of the "opencv tutorial basics" is the translation of the OpenCV help manual, with less original content.
Learning OpenCV is a simple introduction to the principles behind OpenCV functions. It is more vivid and practical than classroom teaching materials and provides practical functions.
Http://download.csdn.net/source/1860888
V. related materials:
Project home: http://sf.net/projects/opencvlibrary
Mail List: http://groups.yahoo.com/group/OpenCV
Chinese site: http://www.opencv.org.cn
Chinese Forum: http://www.opencv.org.cn/forum
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/byxdaz/archive/2009/11/30/4909452.aspx