Skip to content

Commit d4a27fe

Browse files
committed
example 10-01, Exercises_8_1, Readme
1 parent 9ea027c commit d4a27fe

File tree

5 files changed

+224
-13
lines changed

5 files changed

+224
-13
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_executable( example_07-01 example_07-01.cpp )
3232
add_executable( example_08-01 example_08-01.cpp )
3333
add_executable( example_08-02 example_08-02.cpp )
3434
add_executable( example_08-03 example_08-03.cpp )
35+
add_executable( Exercises_8_1 Exercises_8_1.cpp )
3536
add_executable( example_09-01 example_09-01.cpp )
3637
add_executable( example_09-02 example_09-02.cpp )
3738
add_executable( example_09-03 example_09-03.cpp )
@@ -63,6 +64,7 @@ add_executable( example_16-01 example_16-01.cpp )
6364
add_executable( example_17-01 example_17-01.cpp )
6465
add_executable( example_18-01 example_18-01.cpp )
6566
add_executable( example_18-01_from_disk example_18-01_from_disk.cpp )
67+
add_executable( example_19-01 example_19-01.cpp )
6668
add_executable( example_20-01 example_20-01.cpp )
6769
add_executable( example_20-02 example_20-02.cpp )
6870
add_executable( example_21-01 example_21-01.cpp )
@@ -89,6 +91,7 @@ target_link_libraries( example_07-01 ${OpenCV_LIBS} )
8991
target_link_libraries( example_08-01 ${OpenCV_LIBS} )
9092
target_link_libraries( example_08-02 ${OpenCV_LIBS} )
9193
target_link_libraries( example_08-03 ${OpenCV_LIBS} )
94+
target_link_libraries( Exercises_8_1 ${OpenCV_LIBS} )
9295
target_link_libraries( example_09-01 ${OpenCV_LIBS} )
9396
target_link_libraries( example_09-02 ${OpenCV_LIBS} )
9497
target_link_libraries( example_09-03 ${OpenCV_LIBS} )
@@ -122,6 +125,7 @@ target_link_libraries( example_16-01 ${OpenCV_LIBS} )
122125
target_link_libraries( example_17-01 ${OpenCV_LIBS} )
123126
target_link_libraries( example_18-01 ${OpenCV_LIBS} )
124127
target_link_libraries( example_18-01_from_disk ${OpenCV_LIBS} )
128+
target_link_libraries( example_19-01 ${OpenCV_LIBS} )
125129
target_link_libraries( example_20-01 ${OpenCV_LIBS} )
126130
target_link_libraries( example_20-02 ${OpenCV_LIBS} )
127131
target_link_libraries( example_21-01 ${OpenCV_LIBS} )

Exercises_8_1.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Exercises at end of Chapter 8
1+
//Exercises_8_1.cpp Exercises at end of Chapter 8
22
//1
33
#include <opencv2/opencv.hpp>
44
#include <iostream>
@@ -12,7 +12,7 @@ void help(const char **argv) {
1212
<< "This program solves the Exercise 1 at the end of Chapter 8 \n"
1313
<< "Call:\n"
1414
<< argv[0] << " <path/video_name>\n\n"
15-
<< "For example: ./" << argv[0] << " ../bike.avi\n"
15+
<< "For example: ./" << argv[0] << " ../tree.avi\n"
1616
<< endl;
1717
}
1818

@@ -38,19 +38,28 @@ int main( int argc, const char** argv )
3838
b. Write appropriate text labels describing the processing in each of the three
3939
slots.*/
4040
/************************************************************************/
41-
VideoCapture capture(argv[1]);
42-
if(!capture.isOpened())
41+
42+
VideoCapture capture;
43+
if(!capture.open(argv[1])){
44+
cout << "Could not open " << argv[1] << endl;
4345
return 1;
46+
}
4447
double rate=capture.get(CV_CAP_PROP_FPS);
45-
bool stop(false);
4648
Mat MatFrame;
4749
Mat MatGray;
4850
Mat MatCanny;
4951
int delay=1000/rate;
50-
while(!stop)
52+
cout << "rate = " << rate << ", delay = " << delay << endl;
53+
cout << "\nEsq to exit, or let it run out, then any key to release capture and exit.\n" << endl;
54+
int frame_count = 0;
55+
while(1)
5156
{
52-
if(!capture.read(MatFrame))
57+
capture >> MatFrame;
58+
if( !MatFrame.data ) {
59+
cout << "Done with capture" << endl;
5360
break;
61+
}
62+
5463
//(1)
5564
imshow("Raw Video",MatFrame);
5665
//(2)
@@ -75,12 +84,12 @@ int main( int argc, const char** argv )
7584
putText(MatAll,"gray video",Point(50+MatFrame.cols,30),CV_FONT_HERSHEY_DUPLEX,1.0f,color);
7685
putText(MatAll,"canny video",Point(50+2*MatFrame.cols,30),CV_FONT_HERSHEY_DUPLEX,1.0f,color);
7786
imshow("all Video",MatAll);
78-
if(waitKey(delay)>=0)
79-
stop=true;
87+
88+
if ((cv::waitKey(delay) & 255) == 27)
89+
break;
8090
}
81-
capture.release();
8291
waitKey();
83-
getchar();
92+
capture.release();
8493
return 0;
8594

8695
}

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44
This is the example code that accompanies Learning OpenCV 3 by Adrian Kaehler and Gary Bradski (9781491937990).
55

66
~~In progress May 24, 2017~~
7-
**In progress June 13, 2017**
7+
~~In progress June 13, 2017~~
88

9-
This code is in progress, currently at ~~Chapter 15~~ Chapter 21 (_but missing 19_ want to make 18 also read from disk, and add in a few other functions), bear with us, the code is coming, both Authors are busy, as always, building startups. If you want to help, request to join the project. Keep pull requests to one function at a time. It doesn't have to be just examples from the book, it can be other code snippets from the book, **exercises**, new functionality or even a "how to use" opencv_contrib functions.
9+
This code is in progress, currently at ~~Chapter 15~~ Chapter 19 (_~~but missing 19_ want to make 18 also read from disk,~~ and
10+
add in a few other functions), bear with us, the code is coming, both Authors are busy, as always, building startups.
11+
If you want to help, request to join the project. Keep pull requests to one function at a time.
12+
It doesn't have to be just examples from the book, it can be other code snippets from the book,
13+
**exercises**, new functionality or even a "how to use" opencv_contrib functions.
14+
15+
For default suggestions of how the run the code, it assumes you put your build directory under Learning-OpenCV-3_examples directory. Thus, you
16+
17+
cd .../Learning-OpenCV-3_examples/build
18+
cmake ..
19+
make -j
1020

1121
Note, for your interest, included here is an Ubuntu _Docker_ file that
1222
* Shares a directory with the host operating system

birdseye/intrinsics.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<opencv_storage>
3+
<image_width>1600</image_width>
4+
<image_height>1200</image_height>
5+
<camera_matrix type_id="opencv-matrix">
6+
<rows>3</rows>
7+
<cols>3</cols>
8+
<dt>d</dt>
9+
<data>
10+
1.7473845059199218e+03 0. 800. 0. 1.7523330232672765e+03 600. 0. 0.
11+
1.</data></camera_matrix>
12+
<distortion_coefficients type_id="opencv-matrix">
13+
<rows>1</rows>
14+
<cols>5</cols>
15+
<dt>d</dt>
16+
<data>
17+
1.0558825619798969e-01 -1.2250501555283355e+00 0. 0.
18+
4.2302514361517840e+00</data></distortion_coefficients>
19+
</opencv_storage>

example_19-01.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
//Example 19 - 1. Bird’s - eye view
2+
#include <opencv2/opencv.hpp>
3+
#include <iostream>
4+
using namespace std;
5+
6+
void help(char *argv[]) {
7+
cout << "\nExample 19-01, using homography to get a bird's eye view."
8+
<< "\nThis file relies on you having created an intrinsic file via example_18-01_from_disk"
9+
<< "\n but here, that file is already stored in ../birdseye/intrinsics.xml"
10+
<< "\nCall:"
11+
<< "\n./example_19-01 <chessboard_width> <chessboard_height> <path/camera_calib_filename> <path/chessboard_image>"
12+
<< "\n\nExample:"
13+
<< "\n./example_19-01 0 0 ../birdseye/intrinsics.xml ../birdseye\n"
14+
<< "\nPress 'd' for lower birdseye view, and 'u' for higher (it adjusts the apparent 'Z' height), Esc to exit\n"
15+
<< endl;
16+
}
17+
18+
// args: [board_w] [board_h] [intrinsics.xml] [checker_image]
19+
//
20+
int main(int argc, char *argv[]) {
21+
if (argc != 5) {
22+
cout << "\nERROR: too few parameters\n";
23+
help(argv);
24+
return -1;
25+
}
26+
// Input Parameters:
27+
//
28+
int board_w = atoi(argv[1]);
29+
int board_h = atoi(argv[2]);
30+
int board_n = board_w * board_h;
31+
cv::Size board_sz(board_w, board_h);
32+
cv::FileStorage fs(argv[3], cv::FileStorage::READ);
33+
cv::Mat intrinsic, distortion;
34+
35+
fs["camera_matrix"] >> intrinsic;
36+
fs["distortion_coefficients"] >> distortion;
37+
38+
if (!fs.isOpened() || intrinsic.empty() || distortion.empty()) {
39+
cout << "Error: Couldn't load intrinsic parameters from " << argv[3]
40+
<< endl;
41+
return -1;
42+
}
43+
fs.release();
44+
45+
cv::Mat gray_image, image, image0 = cv::imread(argv[4], 1);
46+
if (image0.empty()) {
47+
cout << "Error: Couldn't load image " << argv[4] << endl;
48+
return -1;
49+
}
50+
51+
// UNDISTORT OUR IMAGE
52+
//
53+
cv::undistort(image0, image, intrinsic, distortion, intrinsic);
54+
cv::cvtColor(image, gray_image, cv::COLOR_BGRA2GRAY);
55+
56+
// GET THE CHECKERBOARD ON THE PLANE
57+
//
58+
vector<cv::Point2f> corners;
59+
bool found = cv::findChessboardCorners( // True if found
60+
image, // Input image
61+
board_sz, // Pattern size
62+
corners, // Results
63+
cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FILTER_QUADS);
64+
if (!found) {
65+
cout << "Couldn't acquire checkerboard on " << argv[4] << ", only found "
66+
<< corners.size() << " of " << board_n << " corners\n";
67+
return -1;
68+
}
69+
70+
// Get Subpixel accuracy on those corners
71+
//
72+
cv::cornerSubPix(
73+
gray_image, // Input image
74+
corners, // Initial guesses, also output
75+
cv::Size(11, 11), // Search window size
76+
cv::Size(-1, -1), // Zero zone (in this case, don't use)
77+
cv::TermCriteria(cv::TermCriteria::EPS | cv::TermCriteria::COUNT, 30,
78+
0.1));
79+
80+
// GET THE IMAGE AND OBJECT POINTS:
81+
// Object points are at (r,c):
82+
// (0,0), (board_w-1,0), (0,board_h-1), (board_w-1,board_h-1)
83+
// That means corners are at: corners[r*board_w + c]
84+
//
85+
cv::Point2f objPts[4], imgPts[4];
86+
objPts[0].x = 0;
87+
objPts[0].y = 0;
88+
objPts[1].x = board_w - 1;
89+
objPts[1].y = 0;
90+
objPts[2].x = 0;
91+
objPts[2].y = board_h - 1;
92+
objPts[3].x = board_w - 1;
93+
objPts[3].y = board_h - 1;
94+
imgPts[0] = corners[0];
95+
imgPts[1] = corners[board_w - 1];
96+
imgPts[2] = corners[(board_h - 1) * board_w];
97+
imgPts[3] = corners[(board_h - 1) * board_w + board_w - 1];
98+
99+
// DRAW THE POINTS in order: B,G,R,YELLOW
100+
//
101+
cv::circle(image, imgPts[0], 9, cv::Scalar(255, 0, 0), 3);
102+
cv::circle(image, imgPts[1], 9, cv::Scalar(0, 255, 0), 3);
103+
cv::circle(image, imgPts[2], 9, cv::Scalar(0, 0, 255), 3);
104+
cv::circle(image, imgPts[3], 9, cv::Scalar(0, 255, 255), 3);
105+
106+
// DRAW THE FOUND CHECKERBOARD
107+
//
108+
cv::drawChessboardCorners(image, board_sz, corners, found);
109+
cv::imshow("Checkers", image);
110+
111+
// FIND THE HOMOGRAPHY
112+
//
113+
cv::Mat H = cv::getPerspectiveTransform(objPts, imgPts);
114+
115+
// LET THE USER ADJUST THE Z HEIGHT OF THE VIEW
116+
//
117+
cout << "\nPress 'd' for lower birdseye view, and 'u' for higher (it adjusts the apparent 'Z' height), Esc to exit" << endl;
118+
double Z = 25;
119+
cv::Mat birds_image;
120+
for (;;) {
121+
// escape key stops
122+
H.at<double>(2, 2) = Z;
123+
// USE HOMOGRAPHY TO REMAP THE VIEW
124+
//
125+
cv::warpPerspective(image, // Source image
126+
birds_image, // Output image
127+
H, // Transformation matrix
128+
image.size(), // Size for output image
129+
cv::WARP_INVERSE_MAP | cv::INTER_LINEAR,
130+
cv::BORDER_CONSTANT, cv::Scalar::all(0) // Fill border with black
131+
);
132+
cv::imshow("Birds_Eye", birds_image);
133+
int key = cv::waitKey() & 255;
134+
if (key == 'u')
135+
Z += 0.5;
136+
if (key == 'd')
137+
Z -= 0.5;
138+
if (key == 27)
139+
break;
140+
}
141+
142+
// SHOW ROTATION AND TRANSLATION VECTORS
143+
//
144+
vector<cv::Point2f> image_points;
145+
vector<cv::Point3f> object_points;
146+
for (int i = 0; i < 4; ++i) {
147+
image_points.push_back(imgPts[i]);
148+
object_points.push_back(cv::Point3f(objPts[i].x, objPts[i].y, 0));
149+
}
150+
cv::Mat rvec, tvec, rmat;
151+
cv::solvePnP(object_points, // 3-d points in object coordinate
152+
image_points, // 2-d points in image coordinates
153+
intrinsic, // Our camera matrix
154+
cv::Mat(), // Since we corrected distortion in the
155+
// beginning,now we have zero distortion
156+
// coefficients
157+
rvec, // Output rotation *vector*.
158+
tvec // Output translation vector.
159+
);
160+
cv::Rodrigues(rvec, rmat);
161+
162+
// PRINT AND EXIT
163+
cout << "rotation matrix: " << rmat << endl;
164+
cout << "translation vector: " << tvec << endl;
165+
cout << "homography matrix: " << H << endl;
166+
cout << "inverted homography matrix: " << H.inv() << endl;
167+
168+
return 1;
169+
}

0 commit comments

Comments
 (0)