Vous êtes sur la page 1sur 2

OpenCV 2.1 Cheat Sheet (C++) Mat dyImage(image.size(), image.type()); Mat a2(rgba2.size(), rgba2.

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

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

Vous aimerez peut-être aussi