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)
Ex: Smooth image ROI in-place
- *Mat** imgroi = image(Rect(10, 20, 100, 100));
- GaussianBlur**(imgroi*, imgroi, Size(5,5), 1.2, 1.2);
Simple Matrix Operations
add(), subtract(), multiply(), divide(), absdiff(), bitwise_and(), bitwise_or(), bitwise_xor(), max(), min(), compare()
Ex: Alpha compositing functions
void alphaCompose(const Mat& rgba1, const Mat& rgba2, Mat& rgba_dest)
{
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);
}
Various statistics of matrix elements
sum(), mean(), meanStdDev(), norm(), countNonZero(), minMaxLoc()
Classical math functions
exp(), log(), pow(), sqrt(), cartToPolar(), polarToCart()
Image Processing: Filtering
filter2D(); // Non-separable linear filter
sepFilter2D(); // Separable linear filter
boxFilter();
GaussianBlur();
medianblur();
bilateralFilter();
Sobel();
Scharr();
Laplacian();
erode();
dilate();
Example. Filter image in-place with a 3x3 high-pass kernel
(preserve negative responses by shifting the result by 128):
filter2D(image, image, image.depth(), (Mat <float>(3,3)<<-1, -1, -1, -1, 9, -1, -1, -1, -1), Point(1,1), 128);
Geometrical Transformations
resize(); // Resize image
getRectSubPix(); // Extract an image patch
warpAffine(); // Warp image affinely
warpPerspective(); // Warp image perspectively
remap(); // Generic image warping
convertMaps(); // Optimize maps for a faster remap() execution
Ex: Decimate image by factor of square root of 2
Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));
Various Image Transformations
cvtColor(); // Convert image from one color space to another
threshold(); // Convert grayscale image to binary image
adaptivethreshold(); // Using a fixed or a variable threshold
floodFill(); // Find a connected component using region growing algorithm
integral(); // Compute integral image
distanceTransform(); // build distance map or discrete Voronoi diagram for a binary image<
watershed(); // marker-based image segmentation algorithms
grabCut();
Histograms
calcHist();
calcBackProject();
equalizeHist();
compareHist(); // Compare two histograms
Ex: Histogram
Mat hsv, H;
MatND tempH;
cvtColor(image, hsv, CV BGR2HSV);
int planes[]={0, 1}, hsize[] = {32, 32};
calcHist(&hsv, 1, planes, Mat(), tempH, 2, hsize, 0);
H = tempH;
Data I/O
XML/YAML storages are collections (possibly nested) of
scalar values, structures and heterogeneous lists.
Writing and reading raster images
(The functions can read/write images in the following formats: BMP (.bmp), JPEG (.jpg, .jpeg), TIFF (.tif, .ti), PNG
(.png), PBM/PGM/PPM (.p?m), Sun Raster (.sr), JPEG 2000 (.jp2). Every format supports 8-bit, 1- or
3-channel images. Some formats (PNG, JPEG 2000) support 16 bits per channel.)
- *imwrite**(“myimage.jpg”, image);
- *Mat** image_color_copy = imread(“myimage.jpg”, 1);
- *Mat** image_grayscale_copy = imread(“myimage.jpg”, 0);
Reading video from a file or from a camera
VideoCapture cap;
if(argc > 1) cap.open(string(argv[1])); else cap.open(0);
Mat frame; namedWindow(“video”, 1);
for(;;) {
cap >> frame;
if(!frame.data) break;
imshow(“video”, frame);
if(waitKey(30) >= 0) break;
}
Suppose ‘frame’ is a pointer to IplImage struct with uchar pixel type. How do you access the value of the pixel (x, y)?
pixel_value_B = ((uchar *) (frame->imageData+y*frame->widthStep))[x*frame->nChannels+0];
pixel_value_G = ((uchar *) (frame->imageData+y*frame->widthStep))[x*frame->nChannels+1];
pixel_value_R = ((uchar *) (frame->imageData+y*frame->widthStep))[x*frame->nChannels+2];
Conversion between (IplImage or CvMat) and cv::Mat
* From OpenCV 2.*, cv::Mat replaces the CvMat and IplImage, but it’s easy to convert between the old and the new data structures. For more on this, please see this.
// Assuming somewhere **IplImage \*iplimg;** exists // and has been allocated and **cv::Mat Mimg** has been defined **Mat** imgMat(iplimg); //Construct an Mat image "img" out of an IplImage Mimg = iplimg; //Or just set the header of pre existing cv::Mat Ming // to iplimg's data (no copying is done)
//Convert to IplImage or CvMat, no data copying
- *IplImage** ipl_img = img;
- *CvMat** cvmat = img; // convert cv::Mat -> CvMat
List the modules in OpenCV.
- core - a compact module defining basic data structures, including the dense multi-dimensional array Mat and basic functions used by all other modules.
- imgproc - an image processing module that includes linear and non-linear image filtering, geometrical image transformations (resize, affine and perspective warping, generic table-based remapping), color space conversion, histograms, and so on.
- video - a video analysis module that includes motion estimation, background subtraction, and object tracking algorithms.
- calib3d - basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence algorithms, and elements of 3D reconstruction.
- features2d - salient feature detectors, descriptors, and descriptor matchers.
- objdetect - detection of objects and instances of the predefined classes (for example, faces, eyes, mugs, people, cars, and so on).
- highgui - an easy-to-use interface to video capturing, image and video codecs, as well as simple UI capabilities.
- gpu - GPU-accelerated algorithms from different OpenCV modules.
- … some other helper modules, such as FLANN and Google test wrappers, Python bindings, and others.