Skip to content

Commit 7222a2b

Browse files
Working code from Chapter 2.
Please note that this code does not exactly match the code in the book in all cases. Some small changes have been made here and there to improve readability or quality.
1 parent 4da9e25 commit 7222a2b

13 files changed

+444
-0
lines changed

CMakeLists.txt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
cmake_minimum_required(VERSION 2.8)
3+
4+
project( examples )
5+
6+
find_package( OpenCV REQUIRED )
7+
8+
include_directories( ${OpenCV_INCLUDE_DIRS} )
9+
10+
add_executable( example_02-01 example_02-01.cpp )
11+
add_executable( example_02-02 example_02-02.cpp )
12+
add_executable( example_02-03 example_02-03.cpp )
13+
add_executable( example_02-04 example_02-04.cpp )
14+
add_executable( example_02-05 example_02-05.cpp )
15+
add_executable( example_02-06 example_02-06.cpp )
16+
add_executable( example_02-07 example_02-07.cpp )
17+
add_executable( example_02-08 example_02-08.cpp )
18+
add_executable( example_02-09 example_02-09.cpp )
19+
add_executable( example_02-10 example_02-10.cpp )
20+
add_executable( example_02-11 example_02-11.cpp )
21+
22+
target_link_libraries( example_02-01 ${OpenCV_LIBS} )
23+
#target_link_libraries( example_02-01 opencv_core opencv_highgui opencv_imgcodecs )
24+
target_link_libraries( example_02-02 ${OpenCV_LIBS} )
25+
target_link_libraries( example_02-03 ${OpenCV_LIBS} )
26+
target_link_libraries( example_02-04 ${OpenCV_LIBS} )
27+
target_link_libraries( example_02-05 ${OpenCV_LIBS} )
28+
target_link_libraries( example_02-06 ${OpenCV_LIBS} )
29+
target_link_libraries( example_02-07 ${OpenCV_LIBS} )
30+
target_link_libraries( example_02-08 ${OpenCV_LIBS} )
31+
target_link_libraries( example_02-09 ${OpenCV_LIBS} )
32+
target_link_libraries( example_02-10 ${OpenCV_LIBS} )
33+
target_link_libraries( example_02-11 ${OpenCV_LIBS} )
34+
35+
36+
37+

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Learning OpenCV 3
33

44
This is the example code that accompanies Learning OpenCV 3 by Adrian Kaehler and Gary Bradski (9781491937990).
55

6+
New code is added regularly. We apologize that all of the code from the
7+
book is not up yet, but it will be uploaded as soon as possible. Please
8+
bear with us, the authors and the staff are very busy people and we are
9+
doing our best.
10+
611
Click the Download Zip button to the right to download example code.
712

813
Visit the catalog page [here](http://shop.oreilly.com/product/0636920044765.do).

example_02-01.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//Include file for every supported OpenCV function
2+
#include <opencv2/opencv.hpp>
3+
4+
int main( int argc, char** argv ) {
5+
6+
cv::Mat img = cv::imread( argv[1], -1 );
7+
8+
if( img.empty() ) return -1;
9+
10+
cv::namedWindow( "Example 2-1", cv::WINDOW_AUTOSIZE );
11+
cv::imshow( "Example 2-1", img );
12+
cv::waitKey( 0 );
13+
cv::destroyWindow( "Example 2-1" );
14+
15+
return 0;
16+
}

example_02-02.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "opencv2/highgui/highgui.hpp"
2+
3+
using namespace cv;
4+
5+
int main( int argc, char** argv ) {
6+
7+
Mat img = imread( argv[1], -1 );
8+
9+
if( img.empty() ) return -1;
10+
11+
namedWindow( "Example 2-2", cv::WINDOW_AUTOSIZE );
12+
13+
imshow( "Example 2-2", img );
14+
15+
waitKey( 0 );
16+
17+
destroyWindow( "Example 2-2" );
18+
}

example_02-03.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "opencv2/highgui/highgui.hpp"
2+
#include "opencv2/imgproc/imgproc.hpp"
3+
#include <iostream>
4+
5+
using namespace std;
6+
7+
int main( int argc, char** argv ) {
8+
9+
cv::namedWindow( "Example 2-3", cv::WINDOW_AUTOSIZE );
10+
11+
cv::VideoCapture cap;
12+
13+
cap.open( string(argv[1]) );
14+
cout <<"Opened file: " <<argv[1] <<endl;
15+
16+
cv::Mat frame;
17+
18+
for(;;) {
19+
20+
cap >> frame;
21+
22+
if( frame.empty() ) break; // Ran out of film
23+
24+
cv::imshow( "Example 2-3", frame );
25+
26+
if( (char)cv::waitKey(33) >= 0 ) break;
27+
28+
// int c = cv::waitKey(33);
29+
// for(int i=0;i<32;i++) {
30+
// cout <<((c&(0x1<<(31-i)))?1:0);
31+
// }
32+
// cout <<endl;
33+
// cout <<"Breakey: '" <<(int)c <<"'"<<endl;
34+
// if( (signed char)c >= 0 ) {
35+
// break;
36+
// }
37+
38+
}
39+
40+
return 0;
41+
42+
}

example_02-04.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "opencv2/highgui/highgui.hpp"
2+
#include "opencv2/imgproc/imgproc.hpp"
3+
#include <iostream>
4+
#include <fstream>
5+
6+
using namespace std;
7+
8+
int g_slider_position = 0;
9+
int g_run = 1, g_dontset = 0; //start out in single step mode
10+
cv::VideoCapture g_cap;
11+
12+
void onTrackbarSlide( int pos, void *) {
13+
14+
g_cap.set( CV_CAP_PROP_POS_FRAMES, pos );
15+
16+
if( !g_dontset ) g_run = 1;
17+
18+
g_dontset = 0;
19+
20+
}
21+
int main( int argc, char** argv ) {
22+
23+
cv::namedWindow( "Example 2-4", cv::WINDOW_AUTOSIZE );
24+
25+
g_cap.open( string(argv[1]) );
26+
27+
int frames = (int) g_cap.get( CV_CAP_PROP_FRAME_COUNT );
28+
int tmpw = (int) g_cap.get( CV_CAP_PROP_FRAME_WIDTH );
29+
int tmph = (int) g_cap.get( CV_CAP_PROP_FRAME_HEIGHT );
30+
31+
cout << "Video has " << frames << " frames of dimensions("
32+
<< tmpw << ", " << tmph << ")." << endl;
33+
34+
cv::createTrackbar(
35+
"Position",
36+
"Example 2-4",
37+
&g_slider_position,
38+
frames,
39+
onTrackbarSlide
40+
);
41+
cv::Mat frame;
42+
43+
for(;;) {
44+
45+
if( g_run != 0 ) {
46+
g_cap >> frame;
47+
if(frame.empty()) break;
48+
int current_pos = (int)g_cap.get( CV_CAP_PROP_POS_FRAMES );
49+
g_dontset = 1;
50+
51+
cv::setTrackbarPos("Position", "Example 2-4", current_pos);
52+
cv::imshow( "Example 2-4", frame );
53+
g_run-=1;
54+
}
55+
56+
char c = (char) cv::waitKey(10);
57+
58+
if( c == 's' ) { // single step
59+
g_run = 1;
60+
cout << "Single step, run = " << g_run << endl;
61+
}
62+
63+
if( c == 'r' ) { // run mode
64+
g_run = -1;
65+
cout << "Run mode, run = " << g_run <<endl;
66+
}
67+
68+
if( c == 27 ) break;
69+
}
70+
71+
return(0);
72+
73+
}
74+
75+

example_02-05.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
int main( int argc, char** argv ) {
4+
5+
// Load an image specified on the command line.
6+
//
7+
cv::Mat image = cv::imread(argv[1],-1);
8+
9+
// Create some windows to show the input
10+
// and output images in.
11+
//
12+
cv::namedWindow( "Example 2-5-in", cv::WINDOW_AUTOSIZE );
13+
cv::namedWindow( "Example 2-5-out", cv::WINDOW_AUTOSIZE );
14+
15+
// Create a window to show our input image
16+
//
17+
cv::imshow( "Example 2-5-in", image );
18+
19+
// Create an image to hold the smoothed output
20+
//
21+
cv::Mat out;
22+
23+
// Do the smoothing
24+
// ( Note: Could use GaussianBlur(), blur(), medianBlur() or
25+
// bilateralFilter(). )
26+
//
27+
cv::GaussianBlur( image, out, cv::Size(5,5), 3, 3);
28+
cv::GaussianBlur( out, out, cv::Size(5,5), 3, 3);
29+
30+
// Show the smoothed image in the output window
31+
//
32+
cv::imshow( "Example 2-5-out", out );
33+
34+
// Wait for the user to hit a key, windows will self destruct
35+
//
36+
cv::waitKey( 0 );
37+
38+
}

example_02-06.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
int main( int argc, char** argv ) {
4+
5+
cv::Mat img1,img2;
6+
7+
cv::namedWindow( "Example 2-6-in", cv::WINDOW_AUTOSIZE );
8+
cv::namedWindow( "Example 2-6-out", cv::WINDOW_AUTOSIZE );
9+
10+
img1 = cv::imread( argv[1] );
11+
12+
cv::imshow( "Example 2-6-in", img1 );
13+
cv::pyrDown( img1, img2);
14+
15+
cv::imshow( "Example 2-6-out", img2 );
16+
cv::waitKey(0);
17+
18+
return 0;
19+
20+
};

example_02-07.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
int main( int argc, char** argv ) {
4+
5+
cv::Mat img_rgb, img_gry, img_cny;
6+
7+
cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
8+
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );
9+
10+
img_rgb = cv::imread( argv[1] );
11+
12+
cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);
13+
cv::imshow( "Example Gray", img_gry );
14+
15+
cv::Canny( img_gry, img_cny, 10, 100, 3, true );
16+
cv::imshow( "Example Canny", img_cny );
17+
18+
cv::waitKey(0);
19+
20+
}

example_02-08.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
int main( int argc, char** argv ) {
4+
5+
cv::Mat img_rgb, img_gry, img_cny, img_pyr, img_pyr2;
6+
7+
cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
8+
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );
9+
10+
img_rgb = cv::imread( argv[1] );
11+
12+
cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);
13+
14+
cv::pyrDown( img_gry, img_pyr );
15+
cv::pyrDown( img_pyr, img_pyr2 );
16+
17+
cv::Canny( img_pyr2, img_cny, 10, 100, 3, true );
18+
19+
cv::imshow( "Example Gray", img_gry );
20+
21+
cv::imshow( "Example Canny", img_cny );
22+
23+
cv::waitKey(0);
24+
25+
}

example_02-09.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
int main( int argc, char** argv ) {
4+
5+
cv::Mat img_rgb, img_gry, img_cny, img_pyr, img_pyr2;
6+
7+
cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
8+
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );
9+
10+
img_rgb = cv::imread( argv[1] );
11+
12+
cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);
13+
14+
cv::pyrDown( img_gry, img_pyr );
15+
cv::pyrDown( img_pyr, img_pyr2 );
16+
17+
cv::Canny( img_pyr2, img_cny, 10, 100, 3, true );
18+
19+
// ----------------------------------------------------
20+
// Start new code for example 2-9
21+
//
22+
23+
int x = 16, y = 32;
24+
cv::Vec3b intensity = img_rgb.at< cv::Vec3b >(y, x);
25+
26+
// ( Note: We could write img_rgb.at< cv::Vec3b >(x,y)[0] )
27+
//
28+
uchar blue = intensity[0];
29+
uchar green = intensity[1];
30+
uchar red = intensity[2];
31+
32+
std::cout << "At (x,y) = (" << x << ", " << y <<
33+
"): (blue, green, red) = (" <<
34+
(unsigned int) blue <<
35+
", " << (unsigned int)green << ", " <<
36+
(unsigned int) red << ")" << std::endl;
37+
38+
std::cout << "Gray pixel there is: " <<
39+
(unsigned int) img_gry.at<uchar>(y, x) << std::endl;
40+
41+
x /= 4; y /= 4;
42+
43+
std::cout << "Pyramid2 pixel there is: " <<
44+
(unsigned int)img_pyr2.at<uchar>(y, x) << std::endl;
45+
46+
img_cny.at<uchar>(x, y) = 128; // Set the Canny pixel there to 128
47+
48+
//
49+
// End new code for example 2-9
50+
// ----------------------------------------------------
51+
52+
cv::imshow( "Example Gray", img_gry );
53+
cv::imshow( "Example Canny", img_cny );
54+
55+
cv::waitKey(0);
56+
57+
}

0 commit comments

Comments
 (0)