Skip to content

Commit

Permalink
Good merge with prasanna + gary Merge remote-tracking branch 'origin/…
Browse files Browse the repository at this point in the history
…master'
  • Loading branch information
garybradski committed May 22, 2017
2 parents bda3163 + bcad9b4 commit 3f7a9ab
Show file tree
Hide file tree
Showing 34 changed files with 525 additions and 4 deletions.
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ add_executable( example_11-02 example_11-02.cpp )
add_executable( example_11-03 example_11-03.cpp )
add_executable( example_12-01 example_12-01.cpp )
add_executable( example_12-02 example_12-02.cpp )
add_executable( example_13-01 example_13-01.cpp )
add_executable( example_13-02 example_13-02.cpp )
add_executable( example_13-03 example_13-03.cpp )
add_executable( example_14-01 example_14-01.cpp )
add_executable( example_14-02 example_14-02.cpp )
add_executable( example_14-03 example_14-03.cpp )
add_executable( example_14-04 example_14-04.cpp )
add_executable( example_16-01 example_16-01.cpp )

target_link_libraries( example_02-01 ${OpenCV_LIBS} )
Expand Down Expand Up @@ -75,8 +82,11 @@ target_link_libraries( example_11-02 ${OpenCV_LIBS} )
target_link_libraries( example_11-03 ${OpenCV_LIBS} )
target_link_libraries( example_12-01 ${OpenCV_LIBS} )
target_link_libraries( example_12-02 ${OpenCV_LIBS} )
target_link_libraries( example_13-01 ${OpenCV_LIBS} )
target_link_libraries( example_13-02 ${OpenCV_LIBS} )
target_link_libraries( example_13-03 ${OpenCV_LIBS} )
target_link_libraries( example_14-01 ${OpenCV_LIBS} )
target_link_libraries( example_14-02 ${OpenCV_LIBS} )
target_link_libraries( example_14-03 ${OpenCV_LIBS} )
target_link_libraries( example_14-04 ${OpenCV_LIBS} )
target_link_libraries( example_16-01 ${OpenCV_LIBS} )




Binary file added HandIndoorColor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HandOutdoorColor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HandOutdoorSunColor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions example_13-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Example 13-1. Histogram computation and display

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;

int main( int argc, char** argv ){
if(argc != 2) {
cout << "Computer Color Histogram\nUsage: " <<argv[0] <<" <imagename>" << endl;
return -1;
}

cv::Mat src = cv::imread( argv[1],1 );
if( src.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; }

// Compute the HSV image, and decompose it into separate planes.
//
cv::Mat hsv;
cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);

float h_ranges[] = {0, 180}; // hue is [0, 180]
float s_ranges[] = {0, 256};
const float* ranges[] = {h_ranges, s_ranges};
int histSize[] = {30, 32}, ch[] = {0, 1};

cv::Mat hist;

// Compute the histogram
//
cv::calcHist(&hsv, 1, ch, cv::noArray(), hist, 2, histSize, ranges, true);
cv::normalize(hist, hist, 0, 255, cv::NORM_MINMAX);

int scale = 10;
cv::Mat hist_img(histSize[0]*scale, histSize[1]*scale, CV_8UC3);

// Draw our histogram.
//
for( int h = 0; h < histSize[0]; h++ ) {
for( int s = 0; s < histSize[1]; s++ ){
float hval = hist.at<float>(h, s);
cv::rectangle(
hist_img,
cv::Rect(h*scale,s*scale,scale,scale),
cv::Scalar::all(hval),
-1
);
}
}

cv::imshow("image", src);
cv::imshow("H-S histogram", hist_img);
cv::waitKey();
return 0;
}
162 changes: 162 additions & 0 deletions example_13-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Example 13-2. Creating signatures from histograms for EMD; note that this code is the
// source of the data in Table 13-1, in which the hand histogram is compared in different
// lighting conditions

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;

void help( char** argv ){
cout << "\nCall is:\n"
<< argv[0] <<" modelImage0 testImage1 testImage2 badImage3\n\n"
<< "for example: " << argv[0]
<< " HandIndoorColor.jpg HandOutdoorColor.jpg "
<< "HandOutdoorSunColor.jpg fruits.jpg\n"
<< "\n";
}

// Compare 3 images' histograms
int main( int argc, char** argv ) {
if( argc != 5 ) { help( argv ); return -1; }
vector<cv::Mat> src(5);
cv::Mat tmp;
int i;

tmp = cv::imread( argv[1], 1);
if( tmp.empty() ) {
cerr << "Error on reading image 1," << argv[1] << "\n" << endl;
help( argv );
return(-1);
}

// Parse the first image into two image halves divided halfway on y
//
cv::Size size = tmp.size();
int width = size.width;
int height = size.height;
int halfheight = height >> 1;

cout <<"Getting size [[" <<tmp.cols <<"] [" <<tmp.rows <<"]]\n" <<endl;
cout <<"Got size (w,h): (" <<size.width <<"," <<size.height <<")" <<endl;

src[0] = cv::Mat(cv::Size(width,halfheight), CV_8UC3);
src[1] = cv::Mat(cv::Size(width,halfheight), CV_8UC3);

// Divide the first image into top and bottom halves into src[0] and src[1]
//
cv::Mat_<cv::Vec3b>::iterator tmpit = tmp.begin<cv::Vec3b>();

// top half
//
cv::Mat_<cv::Vec3b>::iterator s0it = src[0].begin<cv::Vec3b>();
for(i = 0; i < width*halfheight; ++i, ++tmpit, ++s0it) *s0it = *tmpit;

// Bottom half
//
cv::Mat_<cv::Vec3b>::iterator s1it = src[1].begin<cv::Vec3b>();
for(i = 0; i < width*halfheight; ++i, ++tmpit, ++s1it) *s1it = *tmpit;

// Load the other three images
//
for(i = 2; i<5; ++i){
src[i] = cv::imread(argv[i], 1);
if(src[i].empty()) {
cerr << "Error on reading image " << i << ": " << argv[i] << "\n" << endl;
help( argv );
return(-1);
}
}

// Compute the HSV image, and decompose it into separate planes.
//
vector<cv::Mat> hsv(5), hist(5), hist_img(5);
int h_bins = 8;
int s_bins = 8;
int hist_size[] = { h_bins, s_bins }, ch[] = {0, 1};
float h_ranges[] = { 0, 180 }; // hue range is [0,180]
float s_ranges[] = { 0, 255 };
const float* ranges[] = { h_ranges, s_ranges };
int scale = 10;

for(i = 0; i<5; ++i) {
cv::cvtColor( src[i], hsv[i], cv::COLOR_BGR2HSV );
cv::calcHist( &hsv[i], 1, ch, cv::noArray(), hist[i], 2, hist_size, ranges, true );
cv::normalize( hist[i], hist[i], 0, 255, cv::NORM_MINMAX );
hist_img[i] = cv::Mat::zeros( hist_size[0]*scale, hist_size[1]*scale, CV_8UC3 );

// Draw our histogram For the 5 images
//
for( int h = 0; h < hist_size[0]; h++ )
for( int s = 0; s < hist_size[1]; s++ ) {
float hval = hist[i].at<float>(h, s);
cv::rectangle(
hist_img[i],
cv::Rect(h*scale, s*scale, scale, scale),
cv::Scalar::all(hval),
-1
);
}
}

// Display
//
cv::namedWindow( "Source0", 1 );cv::imshow( "Source0", src[0] );
cv::namedWindow( "HS Histogram0", 1 );cv::imshow( "HS Histogram0", hist_img[0] );

cv::namedWindow( "Source1", 1 );cv::imshow( "Source1", src[1] );
cv::namedWindow( "HS Histogram1", 1 ); cv::imshow( "HS Histogram1", hist_img[1] );

cv::namedWindow( "Source2", 1 ); cv::imshow( "Source2", src[2] );
cv::namedWindow( "HS Histogram2", 1 ); cv::imshow( "HS Histogram2", hist_img[2] );

cv::namedWindow( "Source3", 1 ); cv::imshow( "Source3", src[3] );
cv::namedWindow( "HS Histogram3", 1 ); cv::imshow( "HS Histogram3", hist_img[3] );

cv::namedWindow( "Source4", 1 ); cv::imshow( "Source4", src[4] );
cv::namedWindow( "HS Histogram4", 1 ); cv::imshow( "HS Histogram4", hist_img[4] );

// Compare the histogram src0 vs 1, vs 2, vs 3, vs 4
cout << "Comparison:\n"
<< "Corr Chi Intersect Bhat\n"<< endl;

for(i=1; i<5; ++i) { // For each histogram
cout << "Hist[0] vs Hist[" << i << "]: " << endl;;
for(int j=0; j<4; ++j) { // For each comparison type
cout << "method[" << j << "]: " << cv::compareHist(hist[0],hist[i],j) << " ";
}
cout << endl;
}

//Do EMD and report
//
vector<cv::Mat> sig(5);
cout << "\nEMD: " << endl;

// Oi Vey, parse histograms to earth movers signatures
//
for( i=0; i<5; ++i) {

vector<cv::Vec3f> sigv;

// (re)normalize histogram to make the bin weights sum to 1.
//
cv::normalize(hist[i], hist[i], 1, 0, cv::NORM_L1);
for( int h = 0; h < h_bins; h++ )
for( int s = 0; s < s_bins; s++ ) {
float bin_val = hist[i].at<float>(h, s);
if( bin_val != 0 )
sigv.push_back( cv::Vec3f(bin_val, (float)h, (float)s));
}

// make Nx3 32fC1 matrix, where N is the number of nonzero histogram bins
//
sig[i] = cv::Mat(sigv).clone().reshape(1);
if( i > 0 )
cout << "Hist[0] vs Hist[" << i << "]: "
<< EMD(sig[0], sig[i], cv::DIST_L2) << endl;
}

cv::waitKey(0);

}
70 changes: 70 additions & 0 deletions example_13-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Example 13-3. Template matching

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;

void help( char** argv ){
cout << "\n"
<<"Example of using matchTemplate(). The call is:\n"
<<"\n"
<<argv[0] <<" template image_to_be_searched\n"
<<"\n"
<<" This routine will search using all methods:\n"
<<" cv::TM_SQDIFF 0\n"
<<" cv::TM_SQDIFF_NORMED 1\n"
<<" cv::TM_CCORR 2\n"
<<" cv::TM_CCORR_NORMED 3\n"
<<" cv::TM_CCOEFF 4\n"
<<" cv::TM_CCOEFF_NORMED 5\n"
<<"\n";
}

// Display the results of the matches
//
int main( int argc, char** argv ) {

if( argc != 3) {
help( argv );
return -1;
}

cv::Mat src, templ, ftmp[6]; // ftmp is what to display on

// Read in the template to be used for matching:
//
if((templ=cv::imread(argv[1], 1)).empty()) {
cout << "Error on reading template " << argv[1] << endl;
help( argv );return -1;
}

// Read in the source image to be searched:
//
if((src=cv::imread(argv[2], 1)).empty()) {
cout << "Error on reading src image " << argv[2] << endl;
help( argv );return -1;
}

// Do the matching of the template with the image
for(int i=0; i<6; ++i){
cv::matchTemplate( src, templ, ftmp[i], i);
cv::normalize(ftmp[i],ftmp[i],1,0,cv::NORM_MINMAX);
}

// Display
//
cv::imshow( "Template", templ );
cv::imshow( "Image", src );
cv::imshow("SQDIFF", ftmp[0] );
cv::imshow("SQDIFF_NORMED", ftmp[1] );
cv::imshow("CCORR", ftmp[2] );
cv::imshow("CCORR_NORMED", ftmp[3] );
cv::imshow("CCOEFF", ftmp[4] );
cv::imshow("CCOEFF_NORMED", ftmp[5] );

// Let user view results:
//
cv::waitKey(0);
}

49 changes: 49 additions & 0 deletions example_14-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Example 14-1. Finding contours based on a trackbar’s location; the contours are
// updated whenever the trackbar is moved

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;

cv::Mat g_gray, g_binary;
int g_thresh = 100;

void on_trackbar( int, void* ) {
cv::threshold( g_gray, g_binary, g_thresh, 255, cv::THRESH_BINARY );
vector< vector< cv::Point> > contours;
cv::findContours(
g_binary,
contours,
cv::noArray(),
cv::RETR_LIST,
cv::CHAIN_APPROX_SIMPLE
);
g_binary = cv::Scalar::all(0);

cv::drawContours( g_binary, contours, -1, cv::Scalar::all(255));
cv::imshow( "Contours", g_binary );

}

int main( int argc, char** argv ) {
if( argc != 2 || ( g_gray = cv::imread(argv[1], 0)).empty() ) {
cout << "Find threshold dependent contours\nUsage: " <<argv[0]
<<"fruits.jpg" << endl;
return -1;
}
cv::namedWindow( "Contours", 1 );

cv::createTrackbar(
"Threshold",
"Contours",
&g_thresh,
255,
on_trackbar
);
on_trackbar(0, 0);

cv::waitKey();

return 0;
}
Loading

0 comments on commit 3f7a9ab

Please sign in to comment.