The OpenCV 2.4 Cheat Sheet provides a comprehensive overview of key functions, classes, and operations in C++ for image processing, matrix manipulations, and object detection. It includes examples for various tasks such as filtering, geometrical transformations, and reading/writing data. The document also references additional resources for further exploration of OpenCV functionalities.
The OpenCV 2.4 Cheat Sheet provides a comprehensive overview of key functions, classes, and operations in C++ for image processing, matrix manipulations, and object detection. It includes examples for various tasks such as filtering, geometrical transformations, and reading/writing data. The document also references additional resources for further exploration of OpenCV functionalities.
for(int y = 1; y < image.rows-1; y++) { bitwise_and(), bitwise_or(), bitwise_xor(), max(), The OpenCV C++ reference manual is here: Vec3b* prevRow = image.ptr<Vec3b>(y-1); min(), compare() http: // docs. opencv. org . Use Quick Search to find Vec3b* nextRow = image.ptr<Vec3b>(y+1); – correspondingly, addition, subtraction, element-wise descriptions of the particular functions and classes for(int x = 0; x < image.cols; x++) multiplication ... comparison of two matrices or a for(int c = 0; c < 3; c++) matrix and a scalar. Key OpenCV Classes dyImage.at<Vec3b>(y,x)[c] = Point_ Template 2D point class saturate_cast<uchar>( Example. Alpha compositing function: Point3_ Template 3D point class nextRow[x][c] - prevRow[x][c]); void alphaCompose(const Mat& rgba1, Size_ Template size (width, height) class } const Mat& rgba2, Mat& rgba_dest) Vec Template short vector class Mat_<Vec3b>::iterator it = image.begin<Vec3b>(), { Matx Template small matrix class itEnd = image.end<Vec3b>(); Mat a1(rgba1.size(), rgba1.type()), ra1; Scalar 4-element vector for(; it != itEnd; ++it) Mat a2(rgba2.size(), rgba2.type()); Rect Rectangle (*it)[1] ^= 255; int mixch[]={3, 0, 3, 1, 3, 2, 3, 3}; Range Integer value range mixChannels(&rgba1, 1, &a1, 1, mixch, 4); Mat 2D or multi-dimensional dense array mixChannels(&rgba2, 1, &a2, 1, mixch, 4); (can be used to store matrices, images, subtract(Scalar::all(255), a1, ra1); histograms, feature descriptors, voxel Matrix Manipulations: Copying, bitwise_or(a1, Scalar(0,0,0,255), a1); volumes etc.) Shuffling, Part Access bitwise_or(a2, Scalar(0,0,0,255), a2); SparseMat Multi-dimensional sparse array multiply(a2, ra1, a2, 1./255); src.copyTo(dst) Copy matrix to another one Ptr Template smart pointer class multiply(a1, rgba1, a1, 1./255); src.convertTo(dst,type,scale,shift) Scale and convert to multiply(a2, rgba2, a2, 1./255); Matrix Basics another datatype add(a1, a2, rgba_dest); Create a matrix m.clone() Make deep copy of a matrix } Mat image(240, 320, CV_8UC3); m.reshape(nch,nrows) Change matrix dimensions and/or num- [Re]allocate a pre-declared matrix ber of channels without copying data • sum(), mean(), meanStdDev(), norm(), countNonZero(), image.create(480, 640, CV_8UC3); m.row(i), m.col(i) Take a matrix row/column minMaxLoc(), Create a matrix initialized with a constant m.rowRange(Range(i1,i2)) Take a matrix row/column span m.colRange(Range(j1,j2)) – various statistics of matrix elements. Mat A33(3, 3, CV_32F, Scalar(5)); Mat B33(3, 3, CV_32F); B33 = Scalar(5); m.diag(i) Take a matrix diagonal • exp(), log(), pow(), sqrt(), cartToPolar(), Mat C33 = Mat::ones(3, 3, CV_32F)*5.; m(Range(i1,i2),Range(j1,j2)),Take a submatrix polarToCart() Mat D33 = Mat::zeros(3, 3, CV_32F) + 5.; m(roi) – the classical math functions. Create a matrix initialized with specified values m.repeat(ny,nx) Make a bigger matrix from a smaller one flip(src,dst,dir) Reverse the order of matrix rows and/or • scaleAdd(), transpose(), gemm(), invert(), solve(), double a = CV_PI/3; columns determinant(), trace(), eigen(), SVD, Mat A22 = (Mat_<float>(2, 2) « cos(a), -sin(a), sin(a), cos(a)); split(...) Split multi-channel matrix into separate – the algebraic functions + SVD class. float B22data[] = {cos(a), -sin(a), sin(a), cos(a)}; channels merge(...) Make a multi-channel matrix out of the • dft(), idft(), dct(), idct(), Mat B22 = Mat(2, 2, CV_32F, B22data).clone(); separate channels – discrete Fourier and cosine transformations Initialize a random matrix randu(image, Scalar(0), Scalar(256)); // uniform dist mixChannels(...) Generalized form of split() and merge() For some operations a more convenient algebraic notation can randn(image, Scalar(128), Scalar(10)); // Gaussian dist randShuffle(...) Randomly shuffle matrix elements be used, for example: Convert matrix to/from other structures (without copying the data) Example 1. Smooth image ROI in-place Mat delta = (J.t()*J + lambda* Mat image_alias = image; Mat imgroi = image(Rect(10, 20, 100, 100)); Mat::eye(J.cols, J.cols, J.type())) float* Idata=new float[480*640*3]; GaussianBlur(imgroi, imgroi, Size(5, 5), 1.2, 1.2); .inv(CV_SVD)*(J.t()*err); Mat I(480, 640, CV_32FC3, Idata); Example 2. Somewhere in a linear algebra algorithm vector<Point> iptvec(10); m.row(i) += m.row(j)*alpha; implements the core of Levenberg-Marquardt optimization Mat iP(iptvec); // iP – 10x1 CV_32SC2 matrix Example 3. Copy image ROI to another image with conversion algorithm. IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1); Rect r(1, 1, 10, 20); Mat newC = cvarrToMat(oldC0); Mat dstroi = dst(Rect(0,10,r.width,r.height)); Image Processsing IplImage oldC1 = newC; CvMat oldC2 = newC; src(r).convertTo(dstroi, dstroi.type(), 1, 0); ... (with copying the data) Filtering Mat newC2 = cvarrToMat(oldC0).clone(); filter2D() Non-separable linear filter vector<Point2f> ptvec = Mat_<Point2f>(iP); sepFilter2D() Separable linear filter Simple Matrix Operations boxFilter(), Smooth the image with one of the linear Access matrix elements OpenCV implements most common arithmetical, logical and GaussianBlur(), or non-linear filters A33.at<float>(i,j) = A33.at<float>(j,i)+1; other matrix operations, such as medianBlur(), bilateralFilter() Sobel(), Scharr() Compute the spatial image derivatives ∂2I ∂2I 1 Laplacian() compute Laplacian: ∆I = ∂x 2 + ∂y 2 Example. Filter image in-place with a 3x3 high-pass kernel FileStorage fs("test.yml", FileStorage::WRITE); cap » frame; if(!frame.data) break; (preserve negative responses by shifting the result by 128): fs « "i" « 5 « "r" « 3.1 « "str" « "ABCDEFGH"; imshow("video", frame); if(waitKey(30) >= 0) break; filter2D(image, image, image.depth(), (Mat_<float>(3,3)« fs « "mtx" « Mat::eye(3,3,CV_32F); } -1, -1, -1, -1, 9, -1, -1, -1, -1), Point(1,1), 128); fs « "mylist" « "[" « CV_PI « "1+1" « "{:" « "month" « 12 « "day" « 31 « "year" Simple GUI (highgui module) « 1969 « "}" « "]"; namedWindow(winname,flags) Create named highgui window Geometrical Transformations fs « "mystruct" « "{" « "x" « 1 « "y" « 2 « destroyWindow(winname) Destroy the specified window resize() Resize image "width" « 100 « "height" « 200 « "lbp" « "[:"; imshow(winname, mtx) Show image in the window getRectSubPix() Extract an image patch const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1}; waitKey(delay) Wait for a key press during the speci- warpAffine() Warp image affinely fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0]))); fied time interval (or forever). Process warpPerspective() Warp image perspectively fs « "]" « "}"; events while waiting. Do not forget to remap() Generic image warping call this function several times a second convertMaps() Optimize maps for a faster remap() ex- Scalars (integers, floating-point numbers, text strings), in your code. ecution matrices, STL vectors of scalars and some other types can be createTrackbar(...) Add trackbar (slider) to the specified written to the file storages using « operator √ window Example. Decimate image by factor of 2: setMouseCallback(...) Set the callback on mouse clicks and Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2)); Reading the data back // Type of the file is determined from the content movements in the specified window FileStorage fs("test.yml", FileStorage::READ); See camshiftdemo.cpp and other OpenCV samples on how to Various Image Transformations int i1 = (int)fs["i"]; double r1 = (double)fs["r"]; use the GUI functions. cvtColor() Convert image from one color space to string str1 = (string)fs["str"]; another Mat M; fs["mtx"] » M; Camera Calibration, Pose Estimation threshold(), Convert grayscale image to binary image FileNode tl = fs["mylist"]; and Depth Estimation adaptivethreshold() using a fixed or a variable threshold CV_Assert(tl.type() == FileNode::SEQ && tl.size() == 3); calibrateCamera() Calibrate camera from several views of floodFill() Find a connected component using re- double tl0 = (double)tl[0]; string tl1 = (string)tl[1]; a calibration pattern. gion growing algorithm int m = (int)tl[2]["month"], d = (int)tl[2]["day"]; findChessboardCorners() Find feature points on the checker- integral() Compute integral image int year = (int)tl[2]["year"]; board calibration pattern. distanceTransform() build distance map or discrete Voronoi FileNode tm = fs["mystruct"]; solvePnP() Find the object pose from the known diagram for a binary image. Rect r; r.x = (int)tm["x"], r.y = (int)tm["y"]; projections of its feature points. watershed(), marker-based image segmentation algo- r.width = (int)tm["width"], r.height = (int)tm["height"]; stereoCalibrate() Calibrate stereo camera. grabCut() rithms. See the samples watershed.cpp int lbp_val = 0; stereoRectify() Compute the rectification transforms for and grabcut.cpp. FileNodeIterator it = tm["lbp"].begin(); a calibrated stereo camera. for(int k = 0; k < 8; k++, ++it) initUndistortRectifyMap() Compute rectification map (for Histograms lbp_val |= ((int)*it) « k; remap()) for each stereo camera head. calcHist() Compute image(s) histogram StereoBM, StereoSGBM The stereo correspondence engines to be calcBackProject() Back-project the histogram run on rectified stereo pairs. Scalars are read using the corresponding FileNode’s cast reprojectImageTo3D() Convert disparity map to 3D point equalizeHist() Normalize image brightness and con- operators. Matrices and some other types are read using » cloud. trast operator. Lists can be read using FileNodeIterator’s. findHomography() Find best-fit perspective transformation compareHist() Compare two histograms between two 2D point sets. Writing and reading raster images To calibrate a camera, you can use calibration.cpp or Example. Compute Hue-Saturation histogram of an image: imwrite("myimage.jpg", image); stereo_calib.cpp samples. To get the disparity maps and the Mat hsv, H; Mat image_color_copy = imread("myimage.jpg", 1); point clouds, use stereo_match.cpp sample. cvtColor(image, hsv, CV_BGR2HSV); Mat image_grayscale_copy = imread("myimage.jpg", 0); int planes[]={0, 1}, hsize[] = {32, 32}; calcHist(&hsv, 1, planes, Mat(), H, 2, hsize, 0); Object Detection matchTemplate Compute proximity map for given tem- The functions can read/write images in the following formats: plate. BMP (.bmp), JPEG (.jpg, .jpeg), TIFF (.tif, .tiff ), Contours CascadeClassifier Viola’s Cascade of Boosted classifiers us- PNG (.png), PBM/PGM/PPM (.p?m), Sun Raster See contours2.cpp and squares.cpp samples on what are the ing Haar or LBP features. Suits for de- (.sr), JPEG 2000 (.jp2). Every format supports 8-bit, 1- contours and how to use them. tecting faces, facial features and some or 3-channel images. Some formats (PNG, JPEG 2000) other objects without diverse textures. support 16 bits per channel. Data I/O See facedetect.cpp XML/YAML storages are collections (possibly nested) of Reading video from a file or from a camera HOGDescriptor N. Dalal’s object detector using scalar values, structures and heterogeneous lists. VideoCapture cap; Histogram-of-Oriented-Gradients if(argc > 1) cap.open(string(argv[1])); else cap.open(0); (HOG) features. Suits for detect- Writing data to YAML (or XML) Mat frame; namedWindow("video", 1); ing people, cars and other objects // Type of the file is determined from the extension for(;;) { with well-defined silhouettes. See peopledetect.cpp