Você está na página 1de 2

OpenCV 2.1 Cheat Sheet (C++) for(int y = 1; y < image.

rows-1; y++) { – correspondingly, addition, subtraction, element-wise


Vec3b* prevRow = image.ptr<Vec3b>(y-1); multiplication ... comparison of two matrices or a
The OpenCV C++ reference manual is here: Vec3b* nextRow = image.ptr<Vec3b>(y+1); matrix and a scalar.
http: // opencv. willowgarage. com/ documentation/ cpp/ . for(int x = 0; y < image.cols; x++)
Use Quick Search to find descriptions of the particular for(int c = 0; c < 3; c++) Example. Alpha compositing function:
functions and classes dyImage.at<Vec3b>(y,x)[c] = void alphaCompose(const Mat& rgba1,
saturate cast<uchar>( const Mat& rgba2, Mat& rgba dest)
Key OpenCV Classes nextRow[x][c] - prevRow[x][c]); {
Point Template 2D point class } Mat a1(rgba1.size(), rgba1.type()), ra1;
Point3 Template 3D point class Mat <Vec3b>::iterator it = image.begin<Vec3b>(), Mat a2(rgba2.size(), rgba2.type());
Size Template size (width, height) class itEnd = image.end<Vec3b>(); int mixch[]={3, 0, 3, 1, 3, 2, 3, 3};
Vec Template short vector class for(; it != itEnd; ++it) mixChannels(&rgba1, 1, &a1, 1, mixch, 4);
Scalar 4-element vector (*it)[1] ^= 255; mixChannels(&rgba2, 1, &a2, 1, mixch, 4);
Rect Rectangle subtract(Scalar::all(255), a1, ra1);
Range Integer value range bitwise or(a1, Scalar(0,0,0,255), a1);
Mat 2D dense array (used as both a matrix Matrix Manipulations: Copying, bitwise or(a2, Scalar(0,0,0,255), a2);
or an image) multiply(a2, ra1, a2, 1./255);
MatND Multi-dimensional dense array
Shuffling, Part Access multiply(a1, rgba1, a1, 1./255);
SparseMat Multi-dimensional sparse array src.copyTo(dst) Copy matrix to another one multiply(a2, rgba2, a2, 1./255);
Ptr Template smart pointer class src.convertTo(dst,type,scale,shift) Scale and convert to add(a1, a2, rgba dest);
another datatype }
Matrix Basics m.clone() Make deep copy of a matrix
m.reshape(nch,nrows) Change matrix dimensions and/or num- • sum(), mean(), meanStdDev(), norm(), countNonZero(),
Create a matrix
ber of channels without copying data minMaxLoc(),
Mat image(240, 320, CV 8UC3);
[Re]allocate a pre-declared matrix m.row(i), m.col(i) Take a matrix row/column – various statistics of matrix elements.
image.create(480, 640, CV 8UC3); m.rowRange(Range(i1,i2)) Take a matrix row/column span
• exp(), log(), pow(), sqrt(), cartToPolar(),
Create a matrix initialized with a constant m.colRange(Range(j1,j2))
polarToCart()
Mat A33(3, 3, CV 32F, Scalar(5)); m.diag(i) Take a matrix diagonal
m(Range(i1,i2),Range(j1,j2)),Take a submatrix – the classical math functions.
Mat B33(3, 3, CV 32F); B33 = Scalar(5);
Mat C33 = Mat::ones(3, 3, CV 32F)*5.; m(roi) • scaleAdd(), transpose(), gemm(), invert(), solve(),
Mat D33 = Mat::zeros(3, 3, CV 32F) + 5.; m.repeat(ny,nx) Make a bigger matrix from a smaller one determinant(), trace() eigen(), SVD,
Create a matrix initialized with specified values flip(src,dst,dir) Reverse the order of matrix rows and/or – the algebraic functions + SVD class.
double a = CV PI/3; columns
Mat A22 = (Mat <float>(2, 2) << split(...) Split multi-channel matrix into separate • dft(), idft(), dct(), idct(),
cos(a), -sin(a), sin(a), cos(a)); channels – discrete Fourier and cosine transformations
float B22data[] = {cos(a), -sin(a), sin(a), cos(a)}; merge(...) Make a multi-channel matrix out of the
separate channels For some operations a more convenient algebraic notation can
Mat B22 = Mat(2, 2, CV 32F, B22data).clone(); be used, for example:
Initialize a random matrix mixChannels(...) Generalized form of split() and merge()
randu(image, Scalar(0), Scalar(256)); // uniform dist randShuffle(...) Randomly shuffle matrix elements
Mat delta = (J.t()*J + lambda*
randn(image, Scalar(128), Scalar(10)); // Gaussian dist Example 1. Smooth image ROI in-place Mat::eye(J.cols, J.cols, J.type()))
Convert matrix to/from other structures Mat imgroi = image(Rect(10, 20, 100, 100)); .inv(CV SVD)*(J.t()*err);
(without copying the data) GaussianBlur(imgroi, imgroi, Size(5, 5), 1.2, 1.2);
Mat image alias = image; Example 2. Somewhere in a linear algebra algorithm implements the core of Levenberg-Marquardt optimization
float* Idata=new float[480*640*3]; m.row(i) += m.row(j)*alpha; algorithm.
Mat I(480, 640, CV 32FC3, Idata); Example 3. Copy image ROI to another image with conversion
vector<Point> iptvec(10); Rect r(1, 1, 10, 20); Image Processsing
Mat iP(iptvec); // iP – 10x1 CV 32SC2 matrix
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat dstroi = dst(Rect(0,10,r.width,r.height)); Filtering
src(r).convertTo(dstroi, dstroi.type(), 1, 0); filter2D() Non-separable linear filter
Mat newC = cvarrToMat(oldC0);
IplImage oldC1 = newC; CvMat oldC2 = newC; sepFilter2D() Separable linear filter
... (with copying the data) boxFilter(), Smooth the image with one of the linear
Mat newC2 = cvarrToMat(oldC0).clone();
Simple Matrix Operations GaussianBlur(), or non-linear filters
vector<Point2f> ptvec = Mat <Point2f>(iP); OpenCV implements most common arithmetical, logical and medianBlur(),
other matrix operations, such as bilateralFilter()
Access matrix elements • add(), subtract(), multiply(), divide(), absdiff(), Sobel(), Scharr() Compute the spatial image derivatives
∂2I ∂2I
A33.at<float>(i,j) = A33.at<float>(j,i)+1; bitwise and(), bitwise or(), bitwise xor(), max(), Laplacian() compute Laplacian: ∆I = ∂x 2 + ∂y 2
Mat dyImage(image.size(), image.type()); min(), compare() erode(), dilate() Erode or dilate the image

1
Example. Filter image in-place with a 3x3 high-pass kernel FileStorage fs("test.yml", FileStorage::WRITE); Simple GUI (highgui module)
(preserve negative responses by shifting the result by 128): fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH";
filter2D(image, image, image.depth(), (Mat <float>(3,3)<< fs << "mtx" << Mat::eye(3,3,CV 32F); namedWindow(winname,flags) Create named highgui window
-1, -1, -1, -1, 9, -1, -1, -1, -1), Point(1,1), 128); fs << "mylist" << "[" << CV PI << "1+1" << destroyWindow(winname) Destroy the specified window
"{:" << "month" << 12 << "day" << 31 << "year" imshow(winname, mtx) Show image in the window
<< 1969 << "}" << "]"; waitKey(delay) Wait for a key press during the speci-
Geometrical Transformations fs << "mystruct" << "{" << "x" << 1 << "y" << 2 << fied time interval (or forever). Process
resize() Resize image "width" << 100 << "height" << 200 << "lbp" << "[:"; events while waiting. Do not forget to
getRectSubPix() Extract an image patch const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1}; call this function several times a second
warpAffine() Warp image affinely fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0]))); in your code.
warpPerspective() Warp image perspectively fs << "]" << "}"; createTrackbar(...) Add trackbar (slider) to the specified
remap() Generic image warping window
Scalars (integers, floating-point numbers, text strings), setMouseCallback(...) Set the callback on mouse clicks and
convertMaps() Optimize maps for a faster remap() ex-
matrices, STL vectors of scalars and some other types can be movements in the specified window
ecution
written to the file storages using << operator
√ See camshiftdemo.c and other OpenCV samples on how to use
Example. Decimate image by factor of 2: Reading the data back the GUI functions.
Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));// Type of the file is determined from the content
FileStorage fs("test.yml", FileStorage::READ);
Various Image Transformations int i1 = (int)fs["i"]; double r1 = (double)fs["r"];
Camera Calibration, Pose Estimation
cvtColor() Convert image from one color space to string str1 = (string)fs["str"]; and Depth Estimation
another Mat M; fs["mtx"] >> M;
calibrateCamera() Calibrate camera from several views of
threshold(), Convert grayscale image to binary image FileNode tl = fs["mylist"]; a calibration pattern.
adaptivethreshold() using a fixed or a variable threshold CV Assert(tl.type() == FileNode::SEQ && tl.size() == 3);
findChessboardCorners() Find feature points on the checker-
floodFill() Find a connected component using re- double tl0 = (double)tl[0]; string tl1 = (string)tl[1]; board calibration pattern.
gion growing algorithm int m = (int)tl[2]["month"], d = (int)tl[2]["day"];
int year = (int)tl[2]["year"]; solvePnP() Find the object pose from the known
integral() Compute integral image projections of its feature points.
distanceTransform() build distance map or discrete Voronoi FileNode tm = fs["mystruct"]; stereoCalibrate() Calibrate stereo camera.
diagram for a binary image. Rect r; r.x = (int)tm["x"], r.y = (int)tm["y"];
r.width = (int)tm["width"], r.height = (int)tm["height"]; stereoRectify() Compute the rectification transforms for
watershed(), marker-based image segmentation algo- a calibrated stereo camera.
grabCut() rithms. See the samples watershed.cpp int lbp val = 0; initUndistortRectifyMap() Compute rectification map (for
and grabcut.cpp. FileNodeIterator it = tm["lbp"].begin();
for(int k = 0; k < 8; k++, ++it) remap()) for each stereo camera head.
lbp val |= ((int)*it) << k; StereoBM, StereoSGBM The stereo correspondence engines to be
Histograms run on rectified stereo pairs.
calcHist() Compute image(s) histogram reprojectImageTo3D() Convert disparity map to 3D point
Scalars are read using the corresponding FileNode’s cast cloud.
calcBackProject() Back-project the histogram
operators. Matrices and some other types are read using >> findHomography() Find best-fit perspective transformation
equalizeHist() Normalize image brightness and con-
operator. Lists can be read using FileNodeIterator’s. between two 2D point sets.
trast
compareHist() Compare two histograms Writing and reading raster images To calibrate a camera, you can use calibration.cpp or
imwriteimwrite("myimage.jpg", image); stereo calib.cpp samples. To get the disparity maps and the
Example. Compute Hue-Saturation histogram of an image: Mat image color copy = imread("myimage.jpg", 1); point clouds, use stereo match.cpp sample.
Mat hsv, H; MatND tempH; Mat image grayscale copy = imread("myimage.jpg", 0);
cvtColor(image, hsv, CV BGR2HSV);
int planes[]={0, 1}, hsize[] = {32, 32}; Object Detection
calcHist(&hsv, 1, planes, Mat(), tempH, 2, hsize, 0); The functions can read/write images in the following formats:
BMP (.bmp), JPEG (.jpg, .jpeg), TIFF (.tif, .tiff ), PNG matchTemplate Compute proximity map for given tem-
H = tempH; plate.
(.png), PBM/PGM/PPM (.p?m), Sun Raster (.sr),
JPEG 2000 (.jp2). Every format supports 8-bit, 1- or CascadeClassifier Viola’s Cascade of Boosted classifiers us-
Contours ing Haar or LBP features. Suits for de-
3-channel images. Some formats (PNG, JPEG 2000) support
See contours.cpp and squares.c samples on what are the tecting faces, facial features and some
16 bits per channel.
contours and how to use them. other objects without diverse textures.
Reading video from a file or from a camera See facedetect.cpp
Data I/O VideoCapture cap; HOGDescriptor N. Dalal’s object detector using
XML/YAML storages are collections (possibly nested) of if(argc > 1) cap.open(string(argv[1])); else cap.open(0); Histogram-of-Oriented-Gradients
scalar values, structures and heterogeneous lists. Mat frame; namedWindow("video", 1); (HOG) features. Suits for detect-
for(;;) { ing people, cars and other objects
Writing data to YAML (or XML) cap >> frame; if(!frame.data) break; with well-defined silhouettes. See
// Type of the file is determined from the extension imshow("video", frame); if(waitKey(30) >= 0) break; peopledetect.cpp
}

Você também pode gostar