Skip to content

Commit 1654dfe

Browse files
sturkmen72vpisarev
authored andcommitted
Update samples (opencv#10333)
* Update samples * Update calib3d.hpp * Update calib3d.hpp * Update calib3d.hpp * Update calib3d.hpp
1 parent d3a124c commit 1654dfe

36 files changed

+285
-224
lines changed

doc/js_tutorials/js_assets/js_houghlines_HoughLines.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2>Hough Lines Example</h2>
4141
<script src="utils.js" type="text/javascript"></script>
4242
<script id="codeSnippet" type="text/code-snippet">
4343
let src = cv.imread('canvasInput');
44-
let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8U);
44+
let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3);
4545
let lines = new cv.Mat();
4646
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
4747
cv.Canny(src, src, 50, 200, 3);

doc/js_tutorials/js_assets/js_houghlines_HoughLinesP.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2>Image HoughLinesP Example</h2>
4141
<script src="utils.js" type="text/javascript"></script>
4242
<script id="codeSnippet" type="text/code-snippet">
4343
let src = cv.imread('canvasInput');
44-
let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8U);
44+
let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3);
4545
let lines = new cv.Mat();
4646
let color = new cv.Scalar(255, 0, 0);
4747
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);

doc/tutorials/features2d/homography/homography.markdown

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,31 @@ This can be tested easily using a chessboard object and `findChessboardCorners()
8989

9090
The first thing consists to detect the chessboard corners, the chessboard size (`patternSize`), here `9x6`, is required:
9191

92-
@snippet tutorial_homography_ex1_pose_from_homography.cpp find-chessboard-corners
92+
@snippet pose_from_homography.cpp find-chessboard-corners
9393

9494
![](images/homography_pose_chessboard_corners.jpg)
9595

9696
The object points expressed in the object frame can be computed easily knowing the size of a chessboard square:
9797

98-
@snippet tutorial_homography_ex1_pose_from_homography.cpp compute-chessboard-object-points
98+
@snippet pose_from_homography.cpp compute-chessboard-object-points
9999

100100
The coordinate `Z=0` must be removed for the homography estimation part:
101101

102-
@snippet tutorial_homography_ex1_pose_from_homography.cpp compute-object-points
102+
@snippet pose_from_homography.cpp compute-object-points
103103

104104
The image points expressed in the normalized camera can be computed from the corner points and by applying a reverse perspective transformation using the camera intrinsics and the distortion coefficients:
105105

106-
@snippet tutorial_homography_ex1_pose_from_homography.cpp load-intrinsics
106+
@snippet pose_from_homography.cpp load-intrinsics
107107

108-
@snippet tutorial_homography_ex1_pose_from_homography.cpp compute-image-points
108+
@snippet pose_from_homography.cpp compute-image-points
109109

110110
The homography can then be estimated with:
111111

112-
@snippet tutorial_homography_ex1_pose_from_homography.cpp estimate-homography
112+
@snippet pose_from_homography.cpp estimate-homography
113113

114114
A quick solution to retrieve the pose from the homography matrix is (see \ref pose_ar "5"):
115115

116-
@snippet tutorial_homography_ex1_pose_from_homography.cpp pose-from-homography
116+
@snippet pose_from_homography.cpp pose-from-homography
117117

118118
\f[
119119
\begin{align*}
@@ -164,23 +164,23 @@ The following image shows the source image (left) and the chessboard view that w
164164

165165
The first step consists to detect the chessboard corners in the source and desired images:
166166

167-
@snippet tutorial_homography_ex2_perspective_correction.cpp find-corners
167+
@snippet perspective_correction.cpp find-corners
168168

169169
The homography is estimated easily with:
170170

171-
@snippet tutorial_homography_ex2_perspective_correction.cpp estimate-homography
171+
@snippet perspective_correction.cpp estimate-homography
172172

173173
To warp the source chessboard view into the desired chessboard view, we use @ref cv::warpPerspective
174174

175-
@snippet tutorial_homography_ex2_perspective_correction.cpp warp-chessboard
175+
@snippet perspective_correction.cpp warp-chessboard
176176

177177
The result image is:
178178

179179
![](images/homography_perspective_correction_chessboard_warp.jpg)
180180

181181
To compute the coordinates of the source corners transformed by the homography:
182182

183-
@snippet tutorial_homography_ex2_perspective_correction.cpp compute-transformed-corners
183+
@snippet perspective_correction.cpp compute-transformed-corners
184184

185185
To check the correctness of the calculation, the matching lines are displayed:
186186

@@ -295,13 +295,13 @@ To transform a 3D point expressed in the camera 1 frame to the camera 2 frame:
295295

296296
In this example, we will compute the camera displacement between two camera poses with respect to the chessboard object. The first step consists to compute the camera poses for the two images:
297297

298-
@snippet tutorial_homography_ex3_homography_from_camera_displacement.cpp compute-poses
298+
@snippet homography_from_camera_displacement.cpp compute-poses
299299

300300
![](images/homography_camera_displacement_poses.jpg)
301301

302302
The camera displacement can be computed from the camera poses using the formulas above:
303303

304-
@snippet tutorial_homography_ex3_homography_from_camera_displacement.cpp compute-c2Mc1
304+
@snippet homography_from_camera_displacement.cpp compute-c2Mc1
305305

306306
The homography related to a specific plane computed from the camera displacement is:
307307

@@ -320,27 +320,27 @@ the translation vector between the two camera frames.
320320

321321
Here the normal vector `n` is the plane normal expressed in the camera frame 1 and can be computed as the cross product of 2 vectors (using 3 non collinear points that lie on the plane) or in our case directly with:
322322

323-
@snippet tutorial_homography_ex3_homography_from_camera_displacement.cpp compute-plane-normal-at-camera-pose-1
323+
@snippet homography_from_camera_displacement.cpp compute-plane-normal-at-camera-pose-1
324324

325325
The distance `d` can be computed as the dot product between the plane normal and a point on the plane or by computing the [plane equation](http://mathworld.wolfram.com/Plane.html) and using the D coefficient:
326326

327-
@snippet tutorial_homography_ex3_homography_from_camera_displacement.cpp compute-plane-distance-to-the-camera-frame-1
327+
@snippet homography_from_camera_displacement.cpp compute-plane-distance-to-the-camera-frame-1
328328

329329
The projective homography matrix \f$ \textbf{G} \f$ can be computed from the Euclidean homography \f$ \textbf{H} \f$ using the intrinsic matrix \f$ \textbf{K} \f$ (see @cite Malis), here assuming the same camera between the two plane views:
330330

331331
\f[
332332
\textbf{G} = \gamma \textbf{K} \textbf{H} \textbf{K}^{-1}
333333
\f]
334334

335-
@snippet tutorial_homography_ex3_homography_from_camera_displacement.cpp compute-homography
335+
@snippet homography_from_camera_displacement.cpp compute-homography
336336

337337
In our case, the Z-axis of the chessboard goes inside the object whereas in the homography figure it goes outside. This is just a matter of sign:
338338

339339
\f[
340340
^{2}\textrm{H}_{1} = \hspace{0.2em} ^{2}\textrm{R}_{1} + \hspace{0.1em} \frac{^{2}\textrm{t}_{1} \cdot n^T}{d}
341341
\f]
342342

343-
@snippet tutorial_homography_ex3_homography_from_camera_displacement.cpp compute-homography-from-camera-displacement
343+
@snippet homography_from_camera_displacement.cpp compute-homography-from-camera-displacement
344344

345345
We will now compare the projective homography computed from the camera displacement with the one estimated with @ref cv::findHomography
346346

@@ -368,11 +368,11 @@ Visually, it is hard to distinguish a difference between the result image from t
368368
OpenCV 3 contains the function @ref cv::decomposeHomographyMat which allows to decompose the homography matrix to a set of rotations, translations and plane normals.
369369
First we will decompose the homography matrix computed from the camera displacement:
370370

371-
@snippet tutorial_homography_ex4_decompose_homography.cpp compute-homography-from-camera-displacement
371+
@snippet decompose_homography.cpp compute-homography-from-camera-displacement
372372

373373
The results of @ref cv::decomposeHomographyMat are:
374374

375-
@snippet tutorial_homography_ex4_decompose_homography.cpp decompose-homography-from-camera-displacement
375+
@snippet decompose_homography.cpp decompose-homography-from-camera-displacement
376376

377377
```
378378
Solution 0:

modules/calib3d/include/opencv2/calib3d.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ optimization procedures like calibrateCamera, stereoCalibrate, or solvePnP .
306306
*/
307307
CV_EXPORTS_W void Rodrigues( InputArray src, OutputArray dst, OutputArray jacobian = noArray() );
308308

309+
/** @example pose_from_homography.cpp
310+
An example program about pose estimation from coplanar points
311+
312+
Check @ref tutorial_homography "the corresponding tutorial" for more details
313+
*/
314+
309315
/** @brief Finds a perspective transformation between two planes.
310316
311317
@param srcPoints Coordinates of the points in the original plane, a matrix of the type CV_32FC2
@@ -364,12 +370,6 @@ cannot be estimated, an empty one will be returned.
364370
@sa
365371
getAffineTransform, estimateAffine2D, estimateAffinePartial2D, getPerspectiveTransform, warpPerspective,
366372
perspectiveTransform
367-
368-
369-
@note
370-
- A example on calculating a homography for image matching can be found at
371-
opencv_source_code/samples/cpp/video_homography.cpp
372-
373373
*/
374374
CV_EXPORTS_W Mat findHomography( InputArray srcPoints, InputArray dstPoints,
375375
int method = 0, double ransacReprojThreshold = 3,
@@ -525,6 +525,12 @@ CV_EXPORTS_W void projectPoints( InputArray objectPoints,
525525
OutputArray jacobian = noArray(),
526526
double aspectRatio = 0 );
527527

528+
/** @example homography_from_camera_displacement.cpp
529+
An example program about homography from the camera displacement
530+
531+
Check @ref tutorial_homography "the corresponding tutorial" for more details
532+
*/
533+
528534
/** @brief Finds an object pose from 3D-2D point correspondences.
529535
530536
@param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or

samples/cpp/dbt_face_detection.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <opencv2/objdetect.hpp>
99

1010
#include <stdio.h>
11-
#include <string>
12-
#include <vector>
1311

1412
using namespace std;
1513
using namespace cv;
@@ -84,7 +82,7 @@ int main(int , char** )
8482
do
8583
{
8684
VideoStream >> ReferenceFrame;
87-
cvtColor(ReferenceFrame, GrayFrame, COLOR_RGB2GRAY);
85+
cvtColor(ReferenceFrame, GrayFrame, COLOR_BGR2GRAY);
8886
Detector.process(GrayFrame);
8987
Detector.getObjects(Faces);
9088

samples/cpp/tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ using namespace std;
1515
/**
1616
* @function main
1717
*/
18-
int main( int, char** argv )
18+
int main( int argc, char** argv )
1919
{
2020
Mat src, dst;
2121

2222
const char* source_window = "Source image";
2323
const char* equalized_window = "Equalized Image";
2424

2525
/// Load image
26-
src = imread( argv[1], IMREAD_COLOR );
27-
26+
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
27+
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
2828
if( src.empty() )
29-
{ cout<<"Usage: ./EqualizeHist_Demo <path_to_image>"<<endl;
30-
return -1;
31-
}
29+
{
30+
cout << "Could not open or find the image!\n" << endl;
31+
cout << "Usage: " << argv[0] << " <Input image>" << endl;
32+
return -1;
33+
}
3234

3335
/// Convert to grayscale
3436
cvtColor( src, src, COLOR_BGR2GRAY );

samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66

77
#include "opencv2/imgproc.hpp"
8-
#include "opencv2/imgcodecs.hpp"
98
#include "opencv2/highgui.hpp"
9+
#include <iostream>
1010

1111
using namespace cv;
12+
using namespace std;
1213

1314
/// Global variables
1415
Mat src, erosion_dst, dilation_dst;
@@ -27,13 +28,17 @@ void Dilation( int, void* );
2728
/**
2829
* @function main
2930
*/
30-
int main( int, char** argv )
31+
int main( int argc, char** argv )
3132
{
3233
/// Load an image
33-
src = imread( argv[1], IMREAD_COLOR );
34-
34+
CommandLineParser parser( argc, argv, "{@input | ../data/chicky_512.png | input image}" );
35+
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
3536
if( src.empty() )
36-
{ return -1; }
37+
{
38+
cout << "Could not open or find the image!\n" << endl;
39+
cout << "Usage: " << argv[0] << " <Input image>" << endl;
40+
return -1;
41+
}
3742

3843
/// Create windows
3944
namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );

samples/cpp/tutorial_code/ImgProc/Morphology_2.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "opencv2/imgproc.hpp"
88
#include "opencv2/imgcodecs.hpp"
99
#include "opencv2/highgui.hpp"
10+
#include <iostream>
1011

1112
using namespace cv;
1213

@@ -32,15 +33,14 @@ void Morphology_Operations( int, void* );
3233
int main( int argc, char** argv )
3334
{
3435
//![load]
35-
String imageName("../data/baboon.jpg"); // by default
36-
if (argc > 1)
36+
CommandLineParser parser( argc, argv, "{@input | ../data/baboon.jpg | input image}" );
37+
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
38+
if (src.empty())
3739
{
38-
imageName = argv[1];
40+
std::cout << "Could not open or find the image!\n" << std::endl;
41+
std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl;
42+
return -1;
3943
}
40-
src = imread(imageName, IMREAD_COLOR); // Load an image
41-
42-
if( src.empty() )
43-
{ return -1; }
4444
//![load]
4545

4646
//![window]

samples/cpp/tutorial_code/ImgProc/morph_lines_detection/Morphology_3.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ void show_wait_destroy(const char* winname, cv::Mat img);
1111
using namespace std;
1212
using namespace cv;
1313

14-
int main(int, char** argv)
14+
int main(int argc, char** argv)
1515
{
1616
//! [load_image]
17-
// Load the image
18-
Mat src = imread(argv[1]);
19-
20-
// Check if image is loaded fine
21-
if(src.empty()){
22-
printf(" Error opening image\n");
23-
printf(" Program Arguments: [image_path]\n");
17+
CommandLineParser parser(argc, argv, "{@input | ../data/notes.png | input image}");
18+
Mat src = imread(parser.get<String>("@input"), IMREAD_COLOR);
19+
if (src.empty())
20+
{
21+
cout << "Could not open or find the image!\n" << endl;
22+
cout << "Usage: " << argv[0] << " <Input image>" << endl;
2423
return -1;
2524
}
2625

samples/cpp/tutorial_code/ImgTrans/CannyDetector_Demo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
#include "opencv2/imgproc.hpp"
8-
#include "opencv2/imgcodecs.hpp"
98
#include "opencv2/highgui.hpp"
9+
#include <iostream>
1010

1111
using namespace cv;
1212

@@ -56,13 +56,18 @@ static void CannyThreshold(int, void*)
5656
/**
5757
* @function main
5858
*/
59-
int main( int, char** argv )
59+
int main( int argc, char** argv )
6060
{
6161
//![load]
62-
src = imread( argv[1], IMREAD_COLOR ); // Load an image
62+
CommandLineParser parser( argc, argv, "{@input | ../data/fruits.jpg | input image}" );
63+
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR ); // Load an image
6364

6465
if( src.empty() )
65-
{ return -1; }
66+
{
67+
std::cout << "Could not open or find the image!\n" << std::endl;
68+
std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl;
69+
return -1;
70+
}
6671
//![load]
6772

6873
//![create_mat]

0 commit comments

Comments
 (0)