You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i already made some contributions related to documentation and i want to keep working on it. I want to work according a right plan so if some tasks that users can do are identified clearly i will choose a task and work on it. after completing a task choose another and so on.
my suggestion is gathering feedbacks from users in documentation pages
for example see msdn.microsoft.com
there is "Is this page helpful? Yes No"
Reacted by Fangjun Kuang, Hagege Ruben, Pablo Aguado, Christoph Rackwitz and Saurabh Khanduja
the end goal would be to have only one entry point for the tutorials with the possibility to switch between the different programming languages as in this tutorial
similarly to the Python documentation, be able to browse directly the Github source code of a function from the OpenCV documentation
browsing a specific version of the documentation should lead to the correct tagged source code
not sure if it is feasible with Doxygen? There is something called SOURCE_BROWSER but I don't know if it completely appropriate
offline documentation should also have a link to the source code somehow?
The first point should be relatively easy as it should be mostly some copy-pasting. The hard part should be to provide the code in C++, Java and in Python but it can be done progressively.
The second point is more a personal request as I have sometimes some difficulties to locate the source code of a function and the Github search option returns too many results.
Reacted by Pablo Aguado, Alexander Alekhin, Jiri Hörner, mehlukas, _iPhoenix_, Anirudh NJ and Saurabh Khanduja
I have tried to improve the documentation (list of PRs) by adding Java and Python code to some C++ tutorials.
Here some suggestions / guidelines I propose to follow. Discussion and proposition are welcome.
Write code that follows good practices
C++:
drop parameters with already good default values, for example imread(filename, IMREAD_COLOR) should be replaced by imread(filename) in my opinion
avoid using pointers, global variables if not needed
Java:
try to follow naming and coding convention specific to Java (e.g. CamelCase naming)
formatting can be easily and automatically done using for instance the Eclipse IDE
Python
try to write pythonic code (e.g. index slicing, itertools?)
for efficiency, try to use directly Numpy function instead of rewrite it using for loops
in all case, try to declare variables the closest to their usage. It helps the comprehension of the program.
try to use CommandLineParser in C++ and argparse in Python for command lines option
Tutorial content
try to write in a way consistent with the other tutorials
a good outline for a tutorial should be in my opinion: (introduction +) theory / code / explanation / references (if any)
in my opinion, explanation of the code is not necessary since the tutorial code should be made self-explanatory
I would favor first a good tutorial code (especially in Java and Python where there are less examples), then the theory if the topic needs it
tutorial code should be embedded with @snippet doxygen command instead of @code command. This allows the code to be automatically verified for syntax error and to be run easily.
Suggestions / improvements
I have noticed that somehow with Numpy documentation the first referenced link by google is always from the latest documentation version
I wonder if something could be done, maybe in the search engine optimization parameters? This would avoid user to look at outdated documentation, since I regularly see people linking to the outdated 3.0-beta documentation.
I think an improvement could be to add accepted mat type in function inputs, especially for the imgproc module?
also, it would be great to have a note if a function is optimized using OpenCL or IPP
normalization / consistency in the path of the tutorial code. Currently, I sometimes find quite difficult to find the corresponding code for a given tutorial (e.g. some imgproc tutorials are in ImgTrans directory).
Reacted by Jiri Hörner, Maksim Shabunin, mehlukas, Ben, _iPhoenix_ and Saurabh Khanduja
2.4 OpenCV branch still exist and the corresponding 2.4 documentation is still relevant. But most of the time, people that arrive on 2.4 doc are newcomers that just google something about OpenCV.
For tutorials improvement, a good thing would be to add references to the corresponding chapters of the Szeliski book.
Good theory with practical examples would be the best for the OpenCV tutorials. For instance, I have found this course.
Also, there are C++, Java and Python code in lot of OpenCV tutorials. Something like Python notebook could also be explored for interactive code.
import cv2
import inspect
with open("cv2_code_examples.txt", "w", encoding="utf-8") as f:
for name in dir(cv2):
obj = getattr(cv2, name)
if inspect.isbuiltin(obj) or inspect.isfunction(obj):
doc = inspect.getdoc(obj)
if doc:
lines = doc.splitlines()
inside_code_block = False
code_lines = []
for line in lines:
stripped = line.strip()
if "@code" in stripped:
inside_code_block = True
code_lines.append(line)
elif "@endcode" in stripped:
code_lines.append(line)
inside_code_block = False
elif inside_code_block:
code_lines.append(line)
if code_lines:
f.write(f"Function: {name}\n")
f.write("\n".join(code_lines) + "\n\n")
using the code above the @code snippets in python documentation etracted. it will be better moving all codes to a .cpp and using @include
Function: add
. @code{.cpp}
. dst = src1 + src2;
. dst += src1; // equivalent to add(dst, src1, dst);
. @endcode
Function: addWeighted
. @code{.cpp}
. dst = src1*alpha + src2*beta + gamma;
. @endcode
Function: borderInterpolate
. @code{.cpp}
. float val = img.at<float>(borderInterpolate(100, img.rows, cv::BORDER_REFLECT_101),
. borderInterpolate(-5, img.cols, cv::BORDER_WRAP));
. @endcode
Function: compare
. @code{.cpp}
. Mat dst1 = src1 >= src2;
. Mat dst2 = src1 < 8;
. ...
. @endcode
Function: contourArea
. @code
. vector<Point> contour;
. contour.push_back(Point2f(0, 0));
. contour.push_back(Point2f(10, 0));
. contour.push_back(Point2f(10, 10));
. contour.push_back(Point2f(5, 4));
.
. double area0 = contourArea(contour);
. vector<Point> approx;
. approxPolyDP(contour, approx, 5, true);
. double area1 = contourArea(approx);
.
. cout << "area0 =" << area0 << endl <<
. "area1 =" << area1 << endl <<
. "approx poly vertices" << approx.size() << endl;
. @endcode
Function: convertScaleAbs
. @code{.cpp}
. Mat_<float> A(30,30);
. randu(A, Scalar(-100), Scalar(100));
. Mat_<float> B = A*5 + 3;
. B = abs(B);
. // Mat_<float> B = abs(A*5+3) will also do the job,
. // but it will allocate a temporary matrix
. @endcode
Function: copyMakeBorder
. @code{.cpp}
. // let border be the same in all directions
. int border=2;
. // constructs a larger image to fit both the image and the border
. Mat gray_buf(rgb.rows + border*2, rgb.cols + border*2, rgb.depth());
. // select the middle part of it w/o copying data
. Mat gray(gray_canvas, Rect(border, border, rgb.cols, rgb.rows));
. // convert image from RGB to grayscale
. cvtColor(rgb, gray, COLOR_RGB2GRAY);
. // form a border in-place
. copyMakeBorder(gray, gray_buf, border, border,
. border, border, BORDER_REPLICATE);
. // now do some custom filtering ...
. ...
. @endcode
Function: createHanningWindow
. @code
. // create hanning window of size 100x100 and type CV_32F
. Mat hann;
. createHanningWindow(hann, Size(100, 100), CV_32F);
. @endcode
Function: cvtColor
. @code
. img *= 1./255;
. cvtColor(img, img, COLOR_BGR2Luv);
. @endcode
Function: dct
. @code
. size_t getOptimalDCTSize(size_t N) { return 2*getOptimalDFTSize((N+1)/2); }
. N1 = getOptimalDCTSize(N);
. @endcode
Function: dft
. @code
. void convolveDFT(InputArray A, InputArray B, OutputArray C)
. {
. // reallocate the output array if needed
. C.create(abs(A.rows - B.rows)+1, abs(A.cols - B.cols)+1, A.type());
. Size dftSize;
. // calculate the size of DFT transform
. dftSize.width = getOptimalDFTSize(A.cols + B.cols - 1);
. dftSize.height = getOptimalDFTSize(A.rows + B.rows - 1);
.
. // allocate temporary buffers and initialize them with 0's
. Mat tempA(dftSize, A.type(), Scalar::all(0));
. Mat tempB(dftSize, B.type(), Scalar::all(0));
.
. // copy A and B to the top-left corners of tempA and tempB, respectively
. Mat roiA(tempA, Rect(0,0,A.cols,A.rows));
. A.copyTo(roiA);
. Mat roiB(tempB, Rect(0,0,B.cols,B.rows));
. B.copyTo(roiB);
.
. // now transform the padded A & B in-place;
. // use "nonzeroRows" hint for faster processing
. dft(tempA, tempA, 0, A.rows);
. dft(tempB, tempB, 0, B.rows);
.
. // multiply the spectrums;
. // the function handles packed spectrum representations well
. mulSpectrums(tempA, tempB, tempA);
.
. // transform the product back from the frequency domain.
. // Even though all the result rows will be non-zero,
. // you need only the first C.rows of them, and thus you
. // pass nonzeroRows == C.rows
. dft(tempA, tempA, DFT_INVERSE + DFT_SCALE, C.rows);
.
. // now copy the result back to C.
. tempA(Rect(0, 0, C.cols, C.rows)).copyTo(C);
.
. // all the temporary buffers will be deallocated automatically
. }
. @endcode
Function: eigen
. @code
. src*eigenvectors.row(i).t() = eigenvalues.at<srcType>(i)*eigenvectors.row(i).t()
. @endcode
Function: eigenNonSymmetric
. @code
. src*eigenvectors.row(i).t() = eigenvalues.at<srcType>(i)*eigenvectors.row(i).t()
. @endcode
Function: findChessboardCorners
. @code
. Size patternsize(8,6); //interior number of corners
. Mat gray = ....; //source image
. vector<Point2f> corners; //this will be filled by the detected corners
.
. //CALIB_CB_FAST_CHECK saves a lot of time on images
. //that do not contain any chessboard corners
. bool patternfound = findChessboardCorners(gray, patternsize, corners,
. CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE
. + CALIB_CB_FAST_CHECK);
.
. if(patternfound)
. cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1),
. TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
.
. drawChessboardCorners(img, patternsize, Mat(corners), patternfound);
. @endcode
Function: findCirclesGrid
. @code
. Size patternsize(7,7); //number of centers
. Mat gray = ...; //source image
. vector<Point2f> centers; //this will be filled by the detected centers
.
. bool patternfound = findCirclesGrid(gray, patternsize, centers);
.
. drawChessboardCorners(img, patternsize, Mat(centers), patternfound);
. @endcode
Function: findFundamentalMat
. @code
. // Example. Estimation of fundamental matrix using the RANSAC algorithm
. int point_count = 100;
. vector<Point2f> points1(point_count);
. vector<Point2f> points2(point_count);
.
. // initialize the points here ...
. for( int i = 0; i < point_count; i++ )
. {
. points1[i] = ...;
. points2[i] = ...;
. }
.
. Mat fundamental_matrix =
. findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99);
. @endcode
Function: findNonZero
. @code{.cpp}
. cv::Mat binaryImage; // input, binary image
. cv::Mat locations; // output, locations of non-zero pixels
. cv::findNonZero(binaryImage, locations);
.
. // access pixel coordinates
. Point pnt = locations.at<Point>(i);
. @endcode
. @code{.cpp}
. cv::Mat binaryImage; // input, binary image
. vector<Point> locations; // output, locations of non-zero pixels
. cv::findNonZero(binaryImage, locations);
.
. // access pixel coordinates
. Point pnt = locations[i];
. @endcode
Function: gemm
. @code{.cpp}
. dst = alpha*src1.t()*src2 + beta*src3.t();
. @endcode
Function: getTextSize
. @code
. String text = "Funny text inside the box";
. int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
. double fontScale = 2;
. int thickness = 3;
.
. Mat img(600, 800, CV_8UC3, Scalar::all(0));
.
. int baseline=0;
. Size textSize = getTextSize(text, fontFace,
. fontScale, thickness, &baseline);
. baseline += thickness;
.
. // center the text
. Point textOrg((img.cols - textSize.width)/2,
. (img.rows + textSize.height)/2);
.
. // draw the box
. rectangle(img, textOrg + Point(0, baseline),
. textOrg + Point(textSize.width, -textSize.height),
. Scalar(0,0,255));
. // ... and the baseline first
. line(img, textOrg + Point(0, thickness),
. textOrg + Point(textSize.width, thickness),
. Scalar(0, 0, 255));
.
. // then put the text itself
. putText(img, text, textOrg, fontFace, fontScale,
. Scalar::all(255), thickness, 8);
. @endcode
Function: getTickFrequency
. @code
. double t = (double)getTickCount();
. // do something ...
. t = ((double)getTickCount() - t)/getTickFrequency();
. @endcode
Function: hconcat
. @code{.cpp}
. std::vector<cv::Mat> matrices = { cv::Mat(4, 1, CV_8UC1, cv::Scalar(1)),
. cv::Mat(4, 1, CV_8UC1, cv::Scalar(2)),
. cv::Mat(4, 1, CV_8UC1, cv::Scalar(3)),};
.
. cv::Mat out;
. cv::hconcat( matrices, out );
. //out:
. //[1, 2, 3;
. // 1, 2, 3;
. // 1, 2, 3;
. // 1, 2, 3]
. @endcode
Function: normalize
. @code{.cpp}
. vector<double> positiveData = { 2.0, 8.0, 10.0 };
. vector<double> normalizedData_l1, normalizedData_l2, normalizedData_inf, normalizedData_minmax;
.
. // Norm to probability (total count)
. // sum(numbers) = 20.0
. // 2.0 0.1 (2.0/20.0)
. // 8.0 0.4 (8.0/20.0)
. // 10.0 0.5 (10.0/20.0)
. normalize(positiveData, normalizedData_l1, 1.0, 0.0, NORM_L1);
.
. // Norm to unit vector: ||positiveData|| = 1.0
. // 2.0 0.15
. // 8.0 0.62
. // 10.0 0.77
. normalize(positiveData, normalizedData_l2, 1.0, 0.0, NORM_L2);
.
. // Norm to max element
. // 2.0 0.2 (2.0/10.0)
. // 8.0 0.8 (8.0/10.0)
. // 10.0 1.0 (10.0/10.0)
. normalize(positiveData, normalizedData_inf, 1.0, 0.0, NORM_INF);
.
. // Norm to range [0.0;1.0]
. // 2.0 0.0 (shift to left border)
. // 8.0 0.75 (6.0/8.0)
. // 10.0 1.0 (shift to right border)
. normalize(positiveData, normalizedData_minmax, 1.0, 0.0, NORM_MINMAX);
. @endcode
Function: pow
. @code{.cpp}
. Mat mask = src < 0;
. pow(src, 1./5, dst);
. subtract(Scalar::all(0), dst, dst, mask);
. @endcode
Function: preCornerDetect
. @code
. Mat corners, dilated_corners;
. preCornerDetect(image, corners, 3);
. // dilation with 3x3 rectangular structuring element
. dilate(corners, dilated_corners, Mat(), 1);
. Mat corner_mask = corners == dilated_corners;
. @endcode
Function: recoverPose
. @code
. // Example. Estimation of fundamental matrix using the RANSAC algorithm
. int point_count = 100;
. vector<Point2f> points1(point_count);
. vector<Point2f> points2(point_count);
.
. // initialize the points here ...
. for( int i = 0; i < point_count; i++ )
. {
. points1[i] = ...;
. points2[i] = ...;
. }
.
. // Input: camera calibration of both cameras, for example using intrinsic chessboard calibration.
. Mat cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2;
.
. // Output: Essential matrix, relative rotation and relative translation.
. Mat E, R, t, mask;
.
. recoverPose(points1, points2, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, E, R, t, mask);
. @endcode
. @code
. // Example. Estimation of fundamental matrix using the RANSAC algorithm
. int point_count = 100;
. vector<Point2f> points1(point_count);
. vector<Point2f> points2(point_count);
.
. // initialize the points here ...
. for( int i = 0; i < point_count; i++ )
. {
. points1[i] = ...;
. points2[i] = ...;
. }
.
. // cametra matrix with both focal lengths = 1, and principal point = (0, 0)
. Mat cameraMatrix = Mat::eye(3, 3, CV_64F);
.
. Mat E, R, t, mask;
.
. E = findEssentialMat(points1, points2, cameraMatrix, RANSAC, 0.999, 1.0, mask);
. recoverPose(E, points1, points2, cameraMatrix, R, t, mask);
. @endcode
Function: resize
. @code
. // explicitly specify dsize=dst.size(); fx and fy will be computed from that.
. resize(src, dst, dst.size(), 0, 0, interpolation);
. @endcode
. @code
. // specify fx and fy and let the function compute the destination image size.
. resize(src, dst, Size(), 0.5, 0.5, interpolation);
. @endcode
Function: scaleAdd
. @code{.cpp}
. Mat A(3, 3, CV_64F);
. ...
. A.row(0) = A.row(1)*2 + A.row(2);
. @endcode
Function: setIdentity
. @code
. Mat A = Mat::eye(4, 3, CV_32F)*5;
. // A will be set to [[5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0]]
. @endcode
Function: sortIdx
. @code
. Mat A = Mat::eye(3,3,CV_32F), B;
. sortIdx(A, B, SORT_EVERY_ROW + SORT_ASCENDING);
. // B will probably contain
. // (because of equal elements in A some permutations are possible):
. // [[1, 2, 0], [0, 2, 1], [0, 1, 2]]
. @endcode
Function: spatialGradient
. @code
. Sobel( src, dx, CV_16SC1, 1, 0, 3 );
. Sobel( src, dy, CV_16SC1, 0, 1, 3 );
. @endcode
Function: subtract
. @code{.cpp}
. dst = src1 - src2;
. dst -= src1; // equivalent to subtract(dst, src1, dst);
. @endcode
Function: vconcat
. @code{.cpp}
. std::vector<cv::Mat> matrices = { cv::Mat(1, 4, CV_8UC1, cv::Scalar(1)),
. cv::Mat(1, 4, CV_8UC1, cv::Scalar(2)),
. cv::Mat(1, 4, CV_8UC1, cv::Scalar(3)),};
.
. cv::Mat out;
. cv::vconcat( matrices, out );
. //out:
. //[1, 1, 1, 1;
. // 2, 2, 2, 2;
. // 3, 3, 3, 3]
. @endcode
Activity
catree commentedon Sep 17, 2017
I have some suggestions:
progressively start to merge the contents of the Python tutorials into the C++ tutorials:
similarly to the Python documentation, be able to browse directly the Github source code of a function from the OpenCV documentation
The first point should be relatively easy as it should be mostly some copy-pasting. The hard part should be to provide the code in C++, Java and in Python but it can be done progressively.
The second point is more a personal request as I have sometimes some difficulties to locate the source code of a function and the Github search option returns too many results.
catree commentedon Jul 11, 2018
I have tried to improve the documentation (list of PRs) by adding Java and Python code to some C++ tutorials.
Here some suggestions / guidelines I propose to follow. Discussion and proposition are welcome.
Write code that follows good practices
imread(filename, IMREAD_COLOR)should be replaced byimread(filename)in my opinionCommandLineParserin C++ andargparsein Python for command lines optionTutorial content
@snippetdoxygen command instead of@codecommand. This allows the code to be automatically verified for syntax error and to be run easily.Suggestions / improvements
catree commentedon May 3, 2020
Related issue to close or redirect the obsolete and no more maintained Python tutorial website.
At the same time, mention about 2.4 OpenCV documentation being obsolete should be added. Here some examples for other libraries:
2.4 OpenCV branch still exist and the corresponding 2.4 documentation is still relevant. But most of the time, people that arrive on 2.4 doc are newcomers that just google something about OpenCV.
For tutorials improvement, a good thing would be to add references to the corresponding chapters of the Szeliski book.
Good theory with practical examples would be the best for the OpenCV tutorials. For instance, I have found this course.
Also, there are C++, Java and Python code in lot of OpenCV tutorials. Something like Python notebook could also be explored for interactive code.
sturkmen72 commentedon Jul 16, 2025
using the code above the @code snippets in python documentation etracted. it will be better moving all codes to a .cpp and using @include
crackwitz commentedon Jul 16, 2025
I doubt this is the right place to discuss it but... is there a structured place in the docs to document for various APIs
?