Skip to content

Commit

Permalink
Working code from Chapter 2.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adrian-kaehler committed Feb 18, 2017
1 parent 4da9e25 commit 7222a2b
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 0 deletions.
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

cmake_minimum_required(VERSION 2.8)

project( examples )

find_package( OpenCV REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( example_02-01 example_02-01.cpp )
add_executable( example_02-02 example_02-02.cpp )
add_executable( example_02-03 example_02-03.cpp )
add_executable( example_02-04 example_02-04.cpp )
add_executable( example_02-05 example_02-05.cpp )
add_executable( example_02-06 example_02-06.cpp )
add_executable( example_02-07 example_02-07.cpp )
add_executable( example_02-08 example_02-08.cpp )
add_executable( example_02-09 example_02-09.cpp )
add_executable( example_02-10 example_02-10.cpp )
add_executable( example_02-11 example_02-11.cpp )

target_link_libraries( example_02-01 ${OpenCV_LIBS} )
#target_link_libraries( example_02-01 opencv_core opencv_highgui opencv_imgcodecs )
target_link_libraries( example_02-02 ${OpenCV_LIBS} )
target_link_libraries( example_02-03 ${OpenCV_LIBS} )
target_link_libraries( example_02-04 ${OpenCV_LIBS} )
target_link_libraries( example_02-05 ${OpenCV_LIBS} )
target_link_libraries( example_02-06 ${OpenCV_LIBS} )
target_link_libraries( example_02-07 ${OpenCV_LIBS} )
target_link_libraries( example_02-08 ${OpenCV_LIBS} )
target_link_libraries( example_02-09 ${OpenCV_LIBS} )
target_link_libraries( example_02-10 ${OpenCV_LIBS} )
target_link_libraries( example_02-11 ${OpenCV_LIBS} )




5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Learning OpenCV 3

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

New code is added regularly. We apologize that all of the code from the
book is not up yet, but it will be uploaded as soon as possible. Please
bear with us, the authors and the staff are very busy people and we are
doing our best.

Click the Download Zip button to the right to download example code.

Visit the catalog page [here](http://shop.oreilly.com/product/0636920044765.do).
Expand Down
16 changes: 16 additions & 0 deletions example_02-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//Include file for every supported OpenCV function
#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

cv::Mat img = cv::imread( argv[1], -1 );

if( img.empty() ) return -1;

cv::namedWindow( "Example 2-1", cv::WINDOW_AUTOSIZE );
cv::imshow( "Example 2-1", img );
cv::waitKey( 0 );
cv::destroyWindow( "Example 2-1" );

return 0;
}
18 changes: 18 additions & 0 deletions example_02-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main( int argc, char** argv ) {

Mat img = imread( argv[1], -1 );

if( img.empty() ) return -1;

namedWindow( "Example 2-2", cv::WINDOW_AUTOSIZE );

imshow( "Example 2-2", img );

waitKey( 0 );

destroyWindow( "Example 2-2" );
}
42 changes: 42 additions & 0 deletions example_02-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace std;

int main( int argc, char** argv ) {

cv::namedWindow( "Example 2-3", cv::WINDOW_AUTOSIZE );

cv::VideoCapture cap;

cap.open( string(argv[1]) );
cout <<"Opened file: " <<argv[1] <<endl;

cv::Mat frame;

for(;;) {

cap >> frame;

if( frame.empty() ) break; // Ran out of film

cv::imshow( "Example 2-3", frame );

if( (char)cv::waitKey(33) >= 0 ) break;

// int c = cv::waitKey(33);
// for(int i=0;i<32;i++) {
// cout <<((c&(0x1<<(31-i)))?1:0);
// }
// cout <<endl;
// cout <<"Breakey: '" <<(int)c <<"'"<<endl;
// if( (signed char)c >= 0 ) {
// break;
// }

}

return 0;

}
75 changes: 75 additions & 0 deletions example_02-04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <fstream>

using namespace std;

int g_slider_position = 0;
int g_run = 1, g_dontset = 0; //start out in single step mode
cv::VideoCapture g_cap;

void onTrackbarSlide( int pos, void *) {

g_cap.set( CV_CAP_PROP_POS_FRAMES, pos );

if( !g_dontset ) g_run = 1;

g_dontset = 0;

}
int main( int argc, char** argv ) {

cv::namedWindow( "Example 2-4", cv::WINDOW_AUTOSIZE );

g_cap.open( string(argv[1]) );

int frames = (int) g_cap.get( CV_CAP_PROP_FRAME_COUNT );
int tmpw = (int) g_cap.get( CV_CAP_PROP_FRAME_WIDTH );
int tmph = (int) g_cap.get( CV_CAP_PROP_FRAME_HEIGHT );

cout << "Video has " << frames << " frames of dimensions("
<< tmpw << ", " << tmph << ")." << endl;

cv::createTrackbar(
"Position",
"Example 2-4",
&g_slider_position,
frames,
onTrackbarSlide
);
cv::Mat frame;

for(;;) {

if( g_run != 0 ) {
g_cap >> frame;
if(frame.empty()) break;
int current_pos = (int)g_cap.get( CV_CAP_PROP_POS_FRAMES );
g_dontset = 1;

cv::setTrackbarPos("Position", "Example 2-4", current_pos);
cv::imshow( "Example 2-4", frame );
g_run-=1;
}

char c = (char) cv::waitKey(10);

if( c == 's' ) { // single step
g_run = 1;
cout << "Single step, run = " << g_run << endl;
}

if( c == 'r' ) { // run mode
g_run = -1;
cout << "Run mode, run = " << g_run <<endl;
}

if( c == 27 ) break;
}

return(0);

}


38 changes: 38 additions & 0 deletions example_02-05.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

// Load an image specified on the command line.
//
cv::Mat image = cv::imread(argv[1],-1);

// Create some windows to show the input
// and output images in.
//
cv::namedWindow( "Example 2-5-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example 2-5-out", cv::WINDOW_AUTOSIZE );

// Create a window to show our input image
//
cv::imshow( "Example 2-5-in", image );

// Create an image to hold the smoothed output
//
cv::Mat out;

// Do the smoothing
// ( Note: Could use GaussianBlur(), blur(), medianBlur() or
// bilateralFilter(). )
//
cv::GaussianBlur( image, out, cv::Size(5,5), 3, 3);
cv::GaussianBlur( out, out, cv::Size(5,5), 3, 3);

// Show the smoothed image in the output window
//
cv::imshow( "Example 2-5-out", out );

// Wait for the user to hit a key, windows will self destruct
//
cv::waitKey( 0 );

}
20 changes: 20 additions & 0 deletions example_02-06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

cv::Mat img1,img2;

cv::namedWindow( "Example 2-6-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example 2-6-out", cv::WINDOW_AUTOSIZE );

img1 = cv::imread( argv[1] );

cv::imshow( "Example 2-6-in", img1 );
cv::pyrDown( img1, img2);

cv::imshow( "Example 2-6-out", img2 );
cv::waitKey(0);

return 0;

};
20 changes: 20 additions & 0 deletions example_02-07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

cv::Mat img_rgb, img_gry, img_cny;

cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );

img_rgb = cv::imread( argv[1] );

cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);
cv::imshow( "Example Gray", img_gry );

cv::Canny( img_gry, img_cny, 10, 100, 3, true );
cv::imshow( "Example Canny", img_cny );

cv::waitKey(0);

}
25 changes: 25 additions & 0 deletions example_02-08.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

cv::Mat img_rgb, img_gry, img_cny, img_pyr, img_pyr2;

cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );

img_rgb = cv::imread( argv[1] );

cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);

cv::pyrDown( img_gry, img_pyr );
cv::pyrDown( img_pyr, img_pyr2 );

cv::Canny( img_pyr2, img_cny, 10, 100, 3, true );

cv::imshow( "Example Gray", img_gry );

cv::imshow( "Example Canny", img_cny );

cv::waitKey(0);

}
57 changes: 57 additions & 0 deletions example_02-09.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

cv::Mat img_rgb, img_gry, img_cny, img_pyr, img_pyr2;

cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );

img_rgb = cv::imread( argv[1] );

cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);

cv::pyrDown( img_gry, img_pyr );
cv::pyrDown( img_pyr, img_pyr2 );

cv::Canny( img_pyr2, img_cny, 10, 100, 3, true );

// ----------------------------------------------------
// Start new code for example 2-9
//

int x = 16, y = 32;
cv::Vec3b intensity = img_rgb.at< cv::Vec3b >(y, x);

// ( Note: We could write img_rgb.at< cv::Vec3b >(x,y)[0] )
//
uchar blue = intensity[0];
uchar green = intensity[1];
uchar red = intensity[2];

std::cout << "At (x,y) = (" << x << ", " << y <<
"): (blue, green, red) = (" <<
(unsigned int) blue <<
", " << (unsigned int)green << ", " <<
(unsigned int) red << ")" << std::endl;

std::cout << "Gray pixel there is: " <<
(unsigned int) img_gry.at<uchar>(y, x) << std::endl;

x /= 4; y /= 4;

std::cout << "Pyramid2 pixel there is: " <<
(unsigned int)img_pyr2.at<uchar>(y, x) << std::endl;

img_cny.at<uchar>(x, y) = 128; // Set the Canny pixel there to 128

//
// End new code for example 2-9
// ----------------------------------------------------

cv::imshow( "Example Gray", img_gry );
cv::imshow( "Example Canny", img_cny );

cv::waitKey(0);

}
Loading

0 comments on commit 7222a2b

Please sign in to comment.