OpenCV Flashcards

1
Q

What is Template 2D point class?

A

Point_

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Template 3D point class?

A

Point3_

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Template size (width, height) class?

A

Size_

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Template short vector class?

A

Vec

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is 4-element vector class?

A

Scalar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Rectangle class?

A

Rect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is 2D dense array (used as both a matrix or an image) class?

A

Mat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Multi-dimensional dense array class?

A

MatND

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Multi-dimensional sparse array class?

A

SparseMat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Template smart pointer class?

A

Ptr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Create a matrix with width=320, height=240

A

Mat image(240, 320, CV_8UC3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

(Re)allocate a per-declared matrix

A

image.create(480, 640, CV_8UC3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create a matrix initialized with a constant

A

// 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.;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create a matrix initialized with specified values

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Initialize a random matrix

A
// 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)**);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Access matrix elements: Different ways!

A

// 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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Copy matrix to another one

A

src.copyTo(dst); // src is a Mat object

18
Q

Make deep copy of a matrix

A

m.clone() // m is a Mat object

19
Q

Scale and convert to another datatype

A

src.convertTo(dst, type, scale, shift); // src is a Mat object

20
Q

Change matrix dimensions and/or number of channels without copying data

A

m.reshape(nch, nrows); // m is a Mat object

21
Q

Take a matrix ‘m’ row/column span

A

m. rowRange(Range(i1, i2));
m. colRange(Range(j1, j2));

22
Q

Take a matrix ‘m’ diagonal

A

m.diag(i)

23
Q

Take a submatrix of ‘m’

A

m(Range(i1, i2), Range(j1, j2));
m(roi);

24
Q

Make a bigger matrix from a smaller one ‘m’

A

m.repeat(ny, nx)

25
Q

Ex: Smooth image ROI in-place

A
  • *Mat** imgroi = image(Rect(10, 20, 100, 100));
  • GaussianBlur**(imgroi*, imgroi, Size(5,5), 1.2, 1.2);
26
Q

Simple Matrix Operations

A

add(), subtract(), multiply(), divide(), absdiff(), bitwise_and(), bitwise_or(), bitwise_xor(), max(), min(), compare()

27
Q

Ex: Alpha compositing functions

A

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);
}

28
Q

Various statistics of matrix elements

A

sum(), mean(), meanStdDev(), norm(), countNonZero(), minMaxLoc()

29
Q

Classical math functions

A

exp(), log(), pow(), sqrt(), cartToPolar(), polarToCart()

30
Q

Image Processing: Filtering

A

filter2D(); // Non-separable linear filter
sepFilter2D(); // Separable linear filter
boxFilter();
GaussianBlur();
medianblur();
bilateralFilter();
Sobel();
Scharr();
Laplacian();
erode();

dilate();

31
Q

Example. Filter image in-place with a 3x3 high-pass kernel
(preserve negative responses by shifting the result by 128):

A

filter2D(image, image, image.depth(), (Mat <float>(3,3)<<-1, -1, -1, -1, 9, -1, -1, -1, -1), Point(1,1), 128);

32
Q

Geometrical Transformations

A

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

33
Q

Ex: Decimate image by factor of square root of 2

A

Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));

34
Q

Various Image Transformations

A

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();

35
Q

Histograms

A

calcHist();
calcBackProject();
equalizeHist();
compareHist(); // Compare two histograms

36
Q

Ex: Histogram

A

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;

37
Q

Data I/O

A

XML/YAML storages are collections (possibly nested) of
scalar values, structures and heterogeneous lists.

38
Q

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.)

A
  • *imwrite**(“myimage.jpg”, image);
  • *Mat** image_color_copy = imread(“myimage.jpg”, 1);
  • *Mat** image_grayscale_copy = imread(“myimage.jpg”, 0);
39
Q

Reading video from a file or from a camera

A

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;
}

40
Q

Suppose ‘frame’ is a pointer to IplImage struct with uchar pixel type. How do you access the value of the pixel (x, y)?

A

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];

41
Q

Conversion between (IplImage or CvMat) and cv::Mat

A

* 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
42
Q

List the modules in OpenCV.

A
  • 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.