diff --git a/example_10-01.cpp b/example_10-01.cpp new file mode 100644 index 0000000..c9948b1 --- /dev/null +++ b/example_10-01.cpp @@ -0,0 +1,50 @@ +//Example 10-1. Using cv::threshold() to sum three channels of an image + +#include +#include + +using namespace std; + +void sum_rgb( const cv::Mat& src, cv::Mat& dst ) { + + // Split image onto the color planes. + // + vector< cv::Mat> planes; + cv::split(src, planes); + cv::Mat b = planes[0], g = planes[1], r = planes[2], s; + + // Add equally weighted rgb values. + // + cv::addWeighted( r, 1./3., g, 1./3., 0.0, s ); + cv::addWeighted( s, 1., b, 1./3., 0.0, s ); + + // Truncate values above 100. + // + cv::threshold( s, dst, 100, 100, cv::THRESH_TRUNC ); +} + +void help() { + cout << "Call: ./example_10-03 ../faces.jpg" << endl; + cout << "Shows use of alpha blending (addWeighted) and threshold" << endl; +} + +int main(int argc, char** argv) { + help(); + if(argc < 2) { cout << "specify input image" << endl; return -1; } + + // Load the image from the given file name. + // + cv::Mat src = cv::imread( argv[1] ), dst; + if( src.empty() ) { cout << "can not load " << argv[1] << endl; return -1; } + sum_rgb( src, dst); + + // Create a named window with the name of the file and + // show the image in the window + // + cv::imshow( argv[1], dst ); + + // Idle until the user hits any key. + // + cv::waitKey(0); + return 0; +} diff --git a/example_10-02.cpp b/example_10-02.cpp new file mode 100644 index 0000000..77352de --- /dev/null +++ b/example_10-02.cpp @@ -0,0 +1,53 @@ +// Example 10-2. Alternative method to combine and threshold image planes + +#include +#include + +using namespace std; + +void sum_rgb( const cv::Mat& src, cv::Mat& dst ) { + + // Split image onto the color planes. + // + vector planes; + cv::split(src, planes); + cv::Mat b = planes[0], g = planes[1], r = planes[2]; + + // Accumulate separate planes, combine and threshold. + // + cv::Mat s = cv::Mat::zeros(b.size(), CV_32F); + cv::accumulate(b, s); + cv::accumulate(g, s); + cv::accumulate(r, s); + + // Truncate values above 100 and rescale into dst. + // + cv::threshold( s, s, 100, 100, cv::THRESH_TRUNC ); + s.convertTo(dst, b.type()); +} + +void help() { + cout << "Call: ./example_10-02 ../faces.jpg" << endl; + cout << "Shows an alternative use of alpha blending and threshold" << endl; +} + +int main(int argc, char** argv) { + help(); + if(argc < 2) { cout << "specify input image" << endl; return -1; } + + // Load the image from the given file name. + // + cv::Mat src = cv::imread( argv[1] ), dst; + if( src.empty() ) { cout << "can not load " << argv[1] << endl; return -1; } + sum_rgb( src, dst); + + // Create a named window with the name of the file and + // show the image in the window + // + cv::imshow( argv[1], dst ); + + // Idle until the user hits any key. + // + cv::waitKey(0); + return 0; +} diff --git a/example_10-03.cpp b/example_10-03.cpp new file mode 100644 index 0000000..0d1315b --- /dev/null +++ b/example_10-03.cpp @@ -0,0 +1,57 @@ +// Example 10-3. Threshold versus adaptive threshold +#include +#include +#include +using namespace std; + +int main( int argc, char** argv ) +{ + if(argc != 7) { cout << + "Usage: " <