opencv 要点总结

来源:互联网 发布:剑三编辑器人物数据 编辑:程序博客网 时间:2024/06/16 07:22



OpenCV2.4 Cheat Sheet (C++)

TheOpenCV C++ reference manual is here:

http:// docs. opencv. org . Use Quick Search to find

descriptionsof the particular functions and classes


1.      Key OpenCV Classes

Point_   Template 2D point class

Point3_  Template 3D point class

Size_   Template size (width, height) class

Vec   Template short vector class

Matx   Template small matrix class

Scalar   4-element vector

Rect   Rectangle

Range   Integer value range

Mat    2D or multi-dimensional dense array(can be used to storematrices, images, histograms, feature descriptors, voxelvolumes etc.)

SparseMat  Multi-dimensional sparse array

Ptr   Template smart pointer class


2.      Matrix Basics

ü  Create a matrix    

Mat image(240, 320, CV_8UC3);

ü  [Re]allocate a pre-declared matrix

image.create(480, 640, CV_8UC3);

ü  Create a matrix initialized with a constant

MatA33(3, 3, CV_32F, Scalar(5));

MatB33(3, 3, CV_32F); B33 = Scalar(5);

MatC33 = Mat::ones(3, 3, CV_32F)*5.;

MatD33 = Mat::zeros(3, 3, CV_32F) + 5.;

ü  Create a matrix initialized with specified values

doublea = CV_PI/3;

MatA22 = (Mat_<float>(2, 2) «

cos(a),-sin(a), sin(a), cos(a));

floatB22data[] = {cos(a), -sin(a), sin(a), cos(a)};

MatB22 = Mat(2, 2, CV_32F, B22data).clone();

ü  Initialize a random matrix

randu(image, Scalar(0), Scalar(256)); // uniform dist

randn(image, Scalar(128), Scalar(10)); // Gaussian dist

ü  Convert matrix to/from other structures

(withoutcopying the data)

Matimage_alias = image;

float*Idata=new float[480*640*3];

MatI(480, 640, CV_32FC3, Idata);

vector<Point>iptvec(10);

MatiP(iptvec); // iP – 10x1CV_32SC2 matrix

IplImage*oldC0 = cvCreateImage(cvSize(320,240),16,1);

MatnewC = cvarrToMat(oldC0);

IplImageoldC1 = newC; CvMat oldC2 = newC;

...(with copying the data)

MatnewC2 = cvarrToMat(oldC0).clone();

vector<Point2f>ptvec = Mat_<Point2f>(iP);

ü  Access matrix elements

A33.at<float>(i,j)= A33.at<float>(j,i)+1;

MatdyImage(image.size(), image.type());

for(int y = 1; y< image.rows-1; y++)

{

Vec3b* prevRow = image.ptr<Vec3b>(y-1);

Vec3b* nextRow = image.ptr<Vec3b>(y+1);

for(int x = 0; x < image.cols; x++)

for(int c = 0; c < 3; c++)

dyImage.at<Vec3b>(y,x)[c] =saturate_cast<uchar>(nextRow[x][c]- prevRow[x][c]);

}

Mat_<Vec3b>::iteratorit = image.begin<Vec3b>(),

itEnd =image.end<Vec3b>();

for(; it != itEnd;++it)

(*it)[1] ^= 255;

 

3.      Matrix Manipulations: Copying,Shuffling, Part Access

src.copyTo(dst) Copy matrix to another one

src.convertTo(dst,type,scale,shift) Scale and convert to another datatype

m.clone() Make deep copy of a matrix

m.reshape(nch,nrows) Change matrix dimensions and/or number ofchannels without copying data

m.row(i)m.col(i)  Take a matrix row/column

m.rowRange(Range(i1,i2)) Take a matrix row/column span

m.colRange(Range(j1,j2)) Take a matrix row/column span

m.diag(i)  Take a matrix diagonal

m(Range(i1,i2),Range(j1,j2)),  Take a submatrix

m(roi)   Take a matrix row/column span

m.repeat(ny,nx)   Make a bigger matrix from a smallerone

flip(src,dst,dir)  Reverse the order of matrix rows and/or columns

split(...)Split multi-channel matrix intoseparate channels

merge(...)Make a multi-channel matrix out ofthe separate channels

mixChannels(...)Generalized form of split() andmerge()

randShuffle(...)Randomly shuffle matrix elements

ü  Example 1. Smooth image ROI in-place

Matimgroi = image(Rect(10, 20, 100, 100));

GaussianBlur(imgroi,imgroi, Size(5, 5), 1.2, 1.2);

ü  Example 2. Somewhere in a linear algebra algorithm

m.row(i)+= m.row(j)*alpha;

ü  Example 3. Copy image ROI to another image with conversion

Rectr(1, 1, 10, 20);

Matdstroi = dst(Rect(0,10,r.width,r.height));

src(r).convertTo(dstroi, dstroi.type(), 1,0);

 

4.      Simple Matrix Operations

OpenCVimplements most common arithmetical, logical and other matrix operations, suchas

add()subtract()multiply()divide()absdiff(),

bitwise_and()bitwise_or()bitwise_xor()max(),

min()compare()

–correspondingly, addition, subtraction, element-wise multiplication ... comparisonof two matrices or a matrix and a scalar.

Example.Alpha compositing function:

voidalphaCompose(const Mat& rgba1,const Mat& rgba2, Mat& rgba_dest)

{

Mat a1(rgba1.size(), rgba1.type()),ra1;

Mat a2(rgba2.size(), rgba2.type());

int mixch[]={3, 0, 3, 1, 3, 2, 3,3};

mixChannels(&rgba1, 1, &a1,1, mixch, 4);

mixChannels(&rgba2, 1, &a2,1, mixch, 4);

subtract(Scalar::all(255), a1, ra1);

bitwise_or(a1, Scalar(0,0,0,255),a1);

bitwise_or(a2, Scalar(0,0,0,255),a2);

multiply(a2, ra1, a2, 1./255);

multiply(a1, rgba1, a1, 1./255);

multiply(a2, rgba2, a2, 1./255);

add(a1, a2, rgba_dest);

}

sum()mean()meanStdDev()norm()countNonZero(),

minMaxLoc(),

–various statistics of matrix elements.

exp()log()pow()sqrt()cartToPolar(),

polarToCart()

– theclassical math functions.

scaleAdd()transpose()gemm()invert()solve(),

determinant()trace()eigen()SVD,

– thealgebraic functions + SVD class.

dft()idft()dct()idct(),

–discrete Fourier and cosine transformations

Forsome operations a more convenient algebraic notation can be used, for example:

Matdelta = (J.t()*J + lambda*

Mat::eye(J.cols,J.cols, J.type()))

.inv(CV_SVD)*(J.t()*err);

implementsthe core of Levenberg-Marquardt optimization algorithm.

 

5.      Image Processsing Filtering

filter2D() Non-separable linear filter

sepFilter2D() Separable linear filter

boxFilter(),

GaussianBlur(),

medianBlur(),

bilateralFilter()

Smooththe image with one of the linear or non-linear filters

Sobel()Scharr() Compute the spatial image derivatives

Laplacian()compute Laplacian

erode()dilate() Morphological operations

Example. Filterimage in-place with a 3x3 high-pass kernel

(preserve negativeresponses by shifting the result by 128):

filter2D(image,image, image.depth(), (Mat_<float>(3,3)«

-1, -1, -1, -1, 9,-1, -1, -1, -1), Point(1,1), 128);

 

6.      Geometrical Transformations

resize() Resize image

getRectSubPix() Extract an image patch

warpAffine() Warp image affinely

warpPerspective() Warp image perspectively

remap() Generic image warping

convertMaps() Optimize maps for a faster remap() execution

Example.Decimate image by factor of

Matdst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));

 

7.      Various Image Transformations

cvtColor() Convert image from one color space to another

threshold(),

adaptivethreshold()

Convertgrayscale image to binary image using a fixed or a variable threshold

floodFill()Find a connected component usingregion growing algorithm

integral()Compute integral image

distanceTransform()build distance map or discreteVoronoi diagram for a binary image.

watershed(),

grabCut()

marker-basedimage segmentation algorithms.See the samples watershed.cpp and grabcut.cpp.

 

8.      Histograms

calcHist()Compute image(s) histogram

calcBackProject()Back-project the histogram

equalizeHist()Normalize image brightness andcontrast

compareHist()Compare two histograms

Example.Compute Hue-Saturation histogram of an image:

Mathsv, H;

cvtColor(image,hsv, CV_BGR2HSV);

intplanes[]={0, 1}, hsize[] = {32, 32};

calcHist(&hsv,1, planes, Mat(), H, 2, hsize, 0);

 

9.      Contours

See contours2.cpp and squares.cpp samples on what are the

contoursand how to use them.

 

10. Data I/O

XML/YAMLstorages are collections(possibly nested) of

scalarvalues, structures and heterogeneous lists.

Writingdata to YAML (or XML)

//Type of the file is determined from the extension

FileStoragefs("test.yml", FileStorage::WRITE);

fs «"i" « 5 « "r" « 3.1 « "str" «"ABCDEFGH";

fs «"mtx" « Mat::eye(3,3,CV_32F);

fs «"mylist" « "[" « CV_PI « "1+1" «

"{:"« "month" « 12 « "day" « 31 « "year"

« 1969« "}" « "]";

fs «"mystruct" « "{" « "x" « 1 « "y" « 2 «

"width"« 100 « "height" « 200 « "lbp" « "[:";

constuchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};

fs.writeRaw("u",arr, (int)(sizeof(arr)/sizeof(arr[0])));

fs «"]" « "}";

Scalars(integers, floating-point numbers, text strings),

matrices,STL vectors of scalars and some other types can be

writtento the file storages using « operator

 

11. Reading the data back

//Type of the file is determined from the content

FileStoragefs("test.yml", FileStorage::READ);

int i1= (int)fs["i"]; double r1 = (double)fs["r"];

stringstr1 = (string)fs["str"];

Mat M;fs["mtx"] » M;

FileNodetl = fs["mylist"];

CV_Assert(tl.type()== FileNode::SEQ && tl.size() == 3);

doubletl0 = (double)tl[0]; string tl1 = (string)tl[1];

int m= (int)tl[2]["month"], d = (int)tl[2]["day"];

intyear = (int)tl[2]["year"];

FileNodetm = fs["mystruct"];

Rectr; r.x = (int)tm["x"], r.y = (int)tm["y"];

r.width= (int)tm["width"], r.height = (int)tm["height"];

intlbp_val = 0;

FileNodeIteratorit = tm["lbp"].begin();

for(intk = 0; k < 8; k++, ++it)

lbp_val|= ((int)*it) « k;

Scalarsare read using the corresponding FileNode’s cast

operators.Matrices and some other types are read using »

operator.Lists can be read using FileNodeIterator’s.

 

12. Writing and reading raster images

imwrite("myimage.jpg", image);

Matimage_color_copy = imread("myimage.jpg", 1);

Matimage_grayscale_copy = imread("myimage.jpg",0);

Thefunctions can read/write images in the following formats:

BMP(.bmp), JPEG (.jpg, .jpeg), TIFF (.tif, .tiff),

PNG(.png), PBM/PGM/PPM (.p?m), Sun Raster

(.sr),JPEG 2000 (.jp2).Every format supports 8-bit, 1-

or3-channel images. Some formats (PNG, JPEG 2000)

support16 bits per channel.

 

13. Reading video from a file or from a camera

VideoCapture cap;

if(argc > 1)cap.open(string(argv[1])); else cap.open(0);

Mat frame;namedWindow("video", 1);

for(;;)

 {

cap » frame; if(!frame.data) break;

imshow("video", frame); if(waitKey(30)>= 0) break;

}

 

14. Simple GUI (highgui module)

namedWindow(winname,flags) Create named highgui window

destroyWindow(winname) Destroy the specified window

imshow(winname,mtx)  Show image in the window

waitKey(delay) Wait for a key press during the specified timeinterval (or forever). Process events while waiting. Do not forget to call this function several times a second in your code.

createTrackbar(...)Add trackbar (slider) to thespecified  window

setMouseCallback(...)Set the callback on mouse clicks andmovements in the specified window

See camshiftdemo.cpp and other OpenCV samples on how to use the GUI functions.

 

15. Camera Calibration, Pose Estimation and Depth Estimation

calibrateCamera()Calibrate camera from several viewsof a calibration pattern.

findChessboardCorners()Find feature points on thecheckerboard calibration pattern.

solvePnP()Find the object pose from the known projectionsof its feature points.

stereoCalibrate()Calibrate stereo camera.

stereoRectify()Compute the rectification transformsfor a calibrated stereo camera.

initUndistortRectifyMap()Compute rectification map (for remap()) for each stereo camera head.

StereoBMStereoSGBM The stereo correspondence engines to be runon rectified stereo pairs.

reprojectImageTo3D()Convert disparity map to 3D point cloud.

findHomography()Find best-fit perspectivetransformation between two 2D point sets. To calibrate a camera, you can use calibration.cpp or stereo_calib.cpp samples. To get the disparity maps and the pointclouds, use stereo_match.cppsample.

16. Object Detection

matchTemplateCompute proximity map for giventemplate.

CascadeClassifierViola’s Cascade of Boostedclassifiers using Haar or LBP features. Suits for detecting faces, facialfeatures and some other objects without diverse textures. See facedetect.cpp HOGDescriptor N. Dalal’s object detector using Histogram  of-Oriented-Gradients (HOG) features. Suitsfor detecting people, cars and other objects with well-defined silhouettes. Seepeopledetect.cpp

OpenCV2.4 Cheat Sheet (C++)

TheOpenCV C++ reference manual is here:

http:// docs. opencv. org . Use Quick Search to find

descriptionsof the particular functions and classes




原创粉丝点击