OpenCV Flashcards
What is Template 2D point class?
Point_
What is Template 3D point class?
Point3_
What is Template size (width, height) class?
Size_
What is Template short vector class?
Vec
What is 4-element vector class?
Scalar
What is Rectangle class?
Rect
What is 2D dense array (used as both a matrix or an image) class?
Mat
What is Multi-dimensional dense array class?
MatND
What is Multi-dimensional sparse array class?
SparseMat
What is Template smart pointer class?
Ptr
Create a matrix with width=320, height=240
Mat image(240, 320, CV_8UC3);
(Re)allocate a per-declared matrix
image.create(480, 640, CV_8UC3);
Create a matrix initialized with a constant
// CV_32F: R, G, B values are floating numbers between 0 and 1.
Mat A33(3, 3, CV_32F, Scalar(5)); // Scalar is a 4-element vector.
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_(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();
Initialize a random matrix
// uniform dist from 0 to 256 **randu**(image, **Scalar(0)**, **Scalar(256)**);
// Gaussian dist with mean=128, std=10 **randn**(image, **Scalar(128)**, **Scalar(10)**);
Access matrix elements: Different ways!
// split the image into separate color planes
vector<Mat> planes;
split(img_yuv, planes);
// method 1. process Y plane using an iterator MatIterator**\_**\<uchar\> it = planes[0].begin\<uchar\>(), it\_end = planes[0].end\<uchar\>(); for(; it != it\_end; ++it) { double v = **\*it**\*1.7 + rand() **\*it** = **saturate\_cast\<uchar\>**(v\*v/255.); }
// method 2. process the first chroma plane using pre-stored row pointer. // method 3. process the second chroma plane using // individual element access operations for( int y = 0; y \< img\_yuv.rows; y++ ) { uchar\* Uptr = planes[1].ptr\<uchar\>(y); for( int x = 0; x \< img\_yuv.cols; x++ ) { Uptr[x] = saturate\_cast\<uchar\>((Uptr[x]-128)/2 + 128); uchar& Vxy = planes[2].at\<uchar\>(y, x); Vxy = saturate\_cast\<uchar\>((Vxy-128)/2 + 128); } }
merge(planes, img_yuv);
…
Copy matrix to another one
src.copyTo(dst); // src is a Mat object
Make deep copy of a matrix
m.clone() // m is a Mat object
Scale and convert to another datatype
src.convertTo(dst, type, scale, shift); // src is a Mat object
Change matrix dimensions and/or number of channels without copying data
m.reshape(nch, nrows); // m is a Mat object
Take a matrix ‘m’ row/column span
m. rowRange(Range(i1, i2));
m. colRange(Range(j1, j2));
Take a matrix ‘m’ diagonal
m.diag(i)
Take a submatrix of ‘m’
m(Range(i1, i2), Range(j1, j2));
m(roi);
Make a bigger matrix from a smaller one ‘m’
m.repeat(ny, nx)