Tutorials
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% %
|
||||
% C++ %
|
||||
% %
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
\ifCpp
|
||||
\section{Camera calibration}
|
||||
The goal of this tutorial is to learn how to calibrate a camera given a set of chessboard images.
|
||||
|
||||
\texttt{Test data}: use images in your data/chess folder.
|
||||
|
||||
Compile opencv with samples by setting BUILD\_EXAMPLES to ON in cmake configuration.
|
||||
|
||||
Go to bin folder and use \texttt{imagelist\_creator} to create an xml/yaml list of your images. Then, run \texttt{calibration} sample to get camera parameters. Use square size equal to 3cm.
|
||||
|
||||
\section{Pose estimation}
|
||||
Now, let us write a code that detects a chessboard in a new image and finds its distance from the camera. You can apply the same method to any object with knwon 3d geometry that you can detect in an image.
|
||||
|
||||
\texttt{Test data}: use chess\_test*.jpg images from your data folder.
|
||||
|
||||
Create an empty console project. Load a test image:
|
||||
\begin{lstlisting}
|
||||
Mat img = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
|
||||
\end{lstlisting}
|
||||
|
||||
Detect a chessboard in this image using findChessboard function.
|
||||
\begin{lstlisting}
|
||||
bool found = findChessboardCorners( img, boardSize, ptvec, CV_CALIB_CB_ADAPTIVE_THRESH );
|
||||
\end{lstlisting}
|
||||
|
||||
Now, write a function that generates a \texttt{vector<Point3f>} array of 3d coordinates of a chessboard in any coordinate system. For simplicity, let us choose a system such that one of the chessboard corners is in the origin and the board is in the plane \(z = 0\).
|
||||
|
||||
Read camera parameters from xml/yaml file:
|
||||
\begin{lstlisting}
|
||||
FileStorage fs(filename, FileStorage::READ);
|
||||
Mat intrinsics, distortion;
|
||||
fs["camera_matrix"] >> intrinsics;
|
||||
fs["distortion_coefficients"] >> distortion;
|
||||
\end{lstlisting}
|
||||
|
||||
Now we are ready to find chessboard pose by running solvePnP:
|
||||
\begin{lstlisting}
|
||||
vector<Point3f> boardPoints;
|
||||
// fill the array
|
||||
...
|
||||
|
||||
solvePnP(Mat(boardPoints), Mat(foundBoardCorners), cameraMatrix,
|
||||
distCoeffs, rvec, tvec, false);
|
||||
\end{lstlisting}
|
||||
|
||||
Calculate reprojection error like it is done in \texttt{calibration} sample (see textttt{opencv/samples/cpp/calibration.cpp}, function \texttt{computeReprojectionErrors}).
|
||||
|
||||
How to calculate the distance from the camera origin to any of the corners?
|
||||
\fi
|
||||
@@ -0,0 +1,67 @@
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% %
|
||||
% C++ %
|
||||
% %
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
\ifCpp
|
||||
\section{Detection of planar objects}
|
||||
The goal of this tutorial is to learn how to use features2d and calib3d modules for detecting known planar objects in scenes.
|
||||
|
||||
\texttt{Test data}: use images in your data folder, for instance, box.png and box\_in\_scene.png.
|
||||
|
||||
Create a new console project. Read two input images. Example:
|
||||
\begin{lstlisting}
|
||||
Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
|
||||
\end{lstlisting}
|
||||
|
||||
Detect keypoints in both images. Example:
|
||||
\begin{lstlisting}
|
||||
// detecting keypoints
|
||||
FastFeatureDetector detector(15);
|
||||
vector<KeyPoint> keypoints1;
|
||||
detector.detect(img1, keypoints1);
|
||||
\end{lstlisting}
|
||||
|
||||
Compute descriptors for each of the keypoints. Example:
|
||||
\begin{lstlisting}
|
||||
// computing descriptors
|
||||
SurfDescriptorExtractor extractor;
|
||||
Mat descriptors1;
|
||||
extractor.compute(img1, keypoints1, descriptors1);
|
||||
\end{lstlisting}
|
||||
|
||||
Now, find the closest matches between descriptors from the first image to the second:
|
||||
\begin{lstlisting}
|
||||
// matching descriptors
|
||||
BruteForceMatcher<L2<float> > matcher;
|
||||
vector<DMatch> matches;
|
||||
matcher.match(descriptors1, descriptors2, matches);
|
||||
\end{lstlisting}
|
||||
|
||||
Visualize the results:
|
||||
\begin{lstlisting}
|
||||
// drawing the results
|
||||
namedWindow("matches", 1);
|
||||
Mat img_matches;
|
||||
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
|
||||
imshow("matches", img_matches);
|
||||
waitKey(0);
|
||||
\end{lstlisting}
|
||||
|
||||
Find the homography transformation between two sets of points:
|
||||
\begin{lstlisting}
|
||||
vector<Point2f> points1, points2;
|
||||
// fill the arrays with the points
|
||||
....
|
||||
Mat H = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);
|
||||
\end{lstlisting}
|
||||
|
||||
Create a set of inlier matches and draw them. Use perspectiveTransform function to map points with homography:
|
||||
\begin{lstlisting}
|
||||
Mat points1Projected;
|
||||
perspectiveTransform(Mat(points1), points1Projected, H);
|
||||
\end{lstlisting}
|
||||
Use drawMatches for drawing inliers.
|
||||
\fi
|
||||
@@ -0,0 +1,12 @@
|
||||
\chapter{Prerequisites}
|
||||
\renewcommand{\curModule}{Prerequisites}
|
||||
\input{tutorials/prerequisites}
|
||||
|
||||
|
||||
\chapter{Features2d}
|
||||
\renewcommand{\curModule}{Features2d}
|
||||
\input{tutorials/features2d}
|
||||
|
||||
\chapter{Calib3d}
|
||||
\renewcommand{\curModule}{Calib3d}
|
||||
\input{tutorials/calib3d}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% %
|
||||
% C++ %
|
||||
% %
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
\ifCpp
|
||||
\section{Prerequisites}
|
||||
Download the latest release of opencv from \url{http://sourceforge.net/projects/opencvlibrary/}. You will need cmake and your favorite compiler environment in order to build opencv from sources. Please refer to the installation guide \url{http://opencv.willowgarage.com/wiki/InstallGuide} for detailed instructions.
|
||||
|
||||
\fi
|
||||
Reference in New Issue
Block a user