Mat dyImage(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]);
}
•
– correspondingly, addition, subtraction, element-wise
multiplication ... comparison of two matrices or a
matrix
OpenCV 2.4 Cheat Sheet (C++)
e manual is here:
Use Quick Search to find
descriptions of the particular functions and classes
OpenCV Classes
Example. function:
void alphaCompose(const Mat& rgba1,
const Mat& rgba2, Mat& rgba_dest)
{
Template 2D point class
Template 3D point class
Template size (width, height) class
Template short vector class
Template small matrix class
4-element vector
Rectangle
Integer value range
2D or multi-dimensional dense array
(can be used to store matrices, images,
histograms, feature descriptors, voxel
volumes etc.)
Mat_<Vec3b>::iterator it = image.begin<Vec3b>(),
itEnd = image.end<Vec3b>();
for(; it != itEnd; ++it)
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);
(*it)[1] ^= 255;
Matrix Manipulations: Copying,
art Access
Multi-dimensional sparse array
Template smart pointer class
one
Scale and convert to
another datatype
Make deep copy of a matrix
Matrix Basics
}
Create a matrix
Change matrix dimensions and/or num-
Mat image(240, 320, CV_8UC3);
•
ber of channels without copying data
[Re]allomatrix
a matrix row/column
640, CV_8UC3);
Take a matrix row/column span
Create a matrix initialized with a constant
Mat A33(3, 3, CV_32F, Scalar(5));
Mat B33(3, 3, CV_32F); B33 = Scalar(5);
Mat C33 = Mat::ones(3, 3, CV_32F)*5.;
Mat D33 = Mat::zeros(3, 3, CV_32F) + 5.;
Create a matrix initialized with specified values
double a = CV_PI/3;
Mat A22 = (Mat_<float>(2, 2) «
cos(a), -sin(a), sin(a), cos(a));
float B22data[] = {cos(a), -sin(a), sin(a), cos(a)};
Mat B22 = Mat(2, 2, CV_32F, B22data).clone();
a random matrix
•
ake a matrix diagonal
,Take a submatrix
•
Make a bigger matrix from a smaller one
Reverse the order of matrix rows and/or
columns
D class.
•
Split multi-channel matrix into separate
channels
Make a multi-channel matrix out of the
separate channels
– discrete Fourier and cosine
For some operations a more convenient can
be used, for example:
Generalized form of split() and merge()
Randomly shuffle matrix elements
Mat delta = (J.t()*J + lambda*
Mat::eye(J.cols, J.cols, J.type()))
.inv(CV_SVD)*(J.t()*err);
Scalar(0), Scalar(256)); // uniform dist
Scalar(128), Scalar(10)); // Gaussian dist
Convert matrix to/from other structures
(without copying the data)
Example 1. Smooth image ROI in-place
implements the core of Levenberg-Marquardt optimization
algorithm.
Mat imgroi = 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;
Mat image_alias = image;
float* Idata=new float[480*640*3];
Mat I(480, 640, CV_32FC3, Idata);
Image Processsing
vector<Point> iptvec(10);
Example 3. Copy image ROI to another image with conversion
Mat iP(iptvec); // iP – 10x1 CV_32SC2 matrix
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat newC = cvarrToMat(oldC0);
Non-separable linear filter
Separable linear filter
Smooth the image with one of the linear
or non-linear filters
Rect r(1, 1, 10, 20);
Mat dstroi = dst(Rect(0,10,r.width,r.height));
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);
Simple Matrix Operations
Compute the spatial image derivatives
compute Laplacian: ∆I =
2
2
∂
I
∂ I
Access matrix elements
OpenCV implements most common arithmetical, logical and
other matrix operations, such as
+
2
∂x ∂y
2
A33.at<float>(i,j) = A33.at<float>(j,i)+1;
Morphological operations
1
全部评论(0)