Professional Documents
Culture Documents
Opencv 2.4 Cheat Sheet (C++)
Opencv 2.4 Cheat Sheet (C++)
SparseMat
Ptr
src.copyTo(dst)
Copy matrix to another one
src.convertTo(dst,type,scale,shift) Scale and convert to
another datatype
Matrix Basics
m.clone()
Make deep copy of a matrix
Create a matrix
m.reshape(nch,nrows)
Change matrix dimensions and/or numMat image(240, 320, CV_8UC3);
ber of channels without copying data
[Re]allocate a pre-declared matrix
m.row(i),
m.col(i)
Take a matrix row/column
image.create(480, 640, CV_8UC3);
m.rowRange(Range(i1,i2)) Take a matrix row/column span
Create a matrix initialized with a constant
m.colRange(Range(j1,j2))
Mat A33(3, 3, CV_32F, Scalar(5));
m.diag(i)
Take a matrix diagonal
Mat B33(3, 3, CV_32F); B33 = Scalar(5);
m(Range(i1,i2),Range(j1,j2)),Take a submatrix
Mat C33 = Mat::ones(3, 3, CV_32F)*5.;
m(roi)
Mat D33 = Mat::zeros(3, 3, CV_32F) + 5.;
m.repeat(ny,nx)
Make a bigger matrix from a smaller one
Create a matrix initialized with specified values
flip(src,dst,dir)
Reverse the order of matrix rows and/or
double a = CV_PI/3;
columns
Mat A22 = (Mat_<float>(2, 2)
split(...)
Split multi-channel matrix into separate
cos(a), -sin(a), sin(a), cos(a));
channels
float B22data[] = {cos(a), -sin(a), sin(a), cos(a)};
merge(...)
Make a multi-channel matrix out of the
Mat B22 = Mat(2, 2, CV_32F, B22data).clone();
separate channels
Initialize a random matrix
mixChannels(...)
Generalized form of split() and merge()
randu(image, Scalar(0), Scalar(256)); // uniform dist
randShuffle(...)
Randomly shuffle matrix elements
randn(image, Scalar(128), Scalar(10)); // Gaussian dist
Convert matrix to/from other structures
Example 1. Smooth image ROI in-place
(without copying the data)
Mat imgroi = image(Rect(10, 20, 100, 100));
Mat image_alias = image;
GaussianBlur(imgroi, imgroi, Size(5, 5), 1.2, 1.2);
float* Idata=new float[480*640*3];
Example 2. Somewhere in a linear algebra algorithm
Mat I(480, 640, CV_32FC3, Idata);
m.row(i) += m.row(j)*alpha;
vector<Point> iptvec(10);
Example 3. Copy image ROI to another image with conversion
Mat iP(iptvec); // iP 10x1 CV_32SC2 matrix
Rect r(1, 1, 10, 20);
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat dstroi = dst(Rect(0,10,r.width,r.height));
Mat newC = cvarrToMat(oldC0);
src(r).convertTo(dstroi, dstroi.type(), 1, 0);
IplImage oldC1 = newC; CvMat oldC2 = newC;
... (with copying the data)
Mat newC2 = cvarrToMat(oldC0).clone();
vector<Point2f> ptvec = Mat_<Point2f>(iP);
Image Processsing
Filtering
filter2D()
sepFilter2D()
boxFilter(),
GaussianBlur(),
medianBlur(),
bilateralFilter()
Sobel(), Scharr()
Laplacian()
erode(), dilate()
Geometrical Transformations
Resize image
Extract an image patch
Warp image affinely
Warp image perspectively
Generic image warping
call this function several times a second
Optimize maps for a faster remap() ex- Scalars (integers, floating-point numbers, text strings),
in your code.
matrices,
STL
vectors
of
scalars
and
some
other
types
can
be
ecution
createTrackbar(...) Add trackbar (slider) to the specified
written to the file storages using operator
window