forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example 10 1,2,4 and new image faces.png
- Loading branch information
1 parent
074a36b
commit fe7b617
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//Example 10-1. Using cv::threshold() to sum three channels of an image | ||
|
||
#include <opencv2/opencv.hpp> | ||
#include <iostream> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Example 10-2. Alternative method to combine and threshold image planes | ||
|
||
#include <opencv2/opencv.hpp> | ||
#include <iostream> | ||
|
||
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]; | ||
|
||
// 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Example 10-3. Threshold versus adaptive threshold | ||
#include <opencv2/opencv.hpp> | ||
#include <iostream> | ||
#include <cstdlib> | ||
using namespace std; | ||
|
||
int main( int argc, char** argv ) | ||
{ | ||
if(argc != 7) { cout << | ||
"Usage: " <<argv[0] <<" fixed_threshold invert(0=off|1=on) " | ||
"adaptive_type(0=mean|1=gaussian) block_size offset image\n" | ||
"Example: " <<argv[0] <<" 100 1 0 15 10 ../faces.png\n"; return -1; } | ||
|
||
// Process command line arguments | ||
// | ||
double fixed_threshold = (double)atof(argv[1]); | ||
int threshold_type = atoi(argv[2]) ? cv::THRESH_BINARY : cv::THRESH_BINARY_INV; | ||
int adaptive_method = atoi(argv[3]) ? cv::ADAPTIVE_THRESH_MEAN_C | ||
: cv::ADAPTIVE_THRESH_GAUSSIAN_C; | ||
int block_size = atoi(argv[4]); | ||
double offset = (double)atof(argv[5]); | ||
cv::Mat Igray = cv::imread(argv[6], cv::IMREAD_GRAYSCALE); | ||
|
||
// Read in gray image. | ||
// | ||
if( Igray.empty() ){ cout << "Can not load " << argv[6] << endl; return -1; } | ||
|
||
// Declare the output images. | ||
// | ||
cv::Mat It, Iat; | ||
|
||
// Thresholds. | ||
// | ||
cv::threshold( | ||
Igray, | ||
It, | ||
fixed_threshold, | ||
255, | ||
threshold_type); | ||
cv::adaptiveThreshold( | ||
Igray, | ||
Iat, | ||
255, | ||
adaptive_method, | ||
threshold_type, | ||
block_size, | ||
offset | ||
); | ||
|
||
// Show the results. | ||
// | ||
cv::imshow("Raw",Igray); | ||
cv::imshow("Threshold",It); | ||
cv::imshow("Adaptive Threshold",Iat); | ||
cv::waitKey(0); | ||
return 0; | ||
} |