Skip to content

Commit

Permalink
Adding examples for chapter 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Prasanna Krishnasamy committed May 21, 2017
1 parent ca1ef89 commit bcad9b4
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 4 deletions.
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ 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 @@ -73,8 +77,8 @@ 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} )




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;
}
70 changes: 70 additions & 0 deletions example_14-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Example 14-2. Finding and drawing contours on an input image

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

using namespace std;

struct AreaCmp {
AreaCmp(const vector<float>& _areas) : areas(&_areas) {}
bool operator()(int a, int b) const { return (*areas)[a] > (*areas)[b]; }
const vector<float>* areas;
};

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

cv::Mat img, img_edge, img_color;

// load image or show help if no image was provided
//
if( argc != 2 || (img = cv::imread(argv[1],cv::IMREAD_GRAYSCALE)).empty() ) {
cout << "\nExample 14_2 Drawing Contours\nCall is:\n" << argv[0] << " image\n\n";
return -1;
}

cv::threshold(img, img_edge, 128, 255, cv::THRESH_BINARY);
cv::imshow("Image after threshold", img_edge);
vector< vector< cv::Point > > contours;
vector< cv::Vec4i > hierarchy;

cv::findContours(
img_edge,
contours,
hierarchy,
cv::RETR_LIST,
cv::CHAIN_APPROX_SIMPLE
);
cout << "\n\nHit any key to draw the next contour, ESC to quit\n\n";
cout << "Total Contours Detected: " << contours.size() << endl;

vector<int> sortIdx(contours.size());
vector<float> areas(contours.size());
for( int n = 0; n < (int)contours.size(); n++ ) {
sortIdx[n] = n;
areas[n] = contourArea(contours[n], false);
}

// sort contours so that the largest contours go first
//
std::sort( sortIdx.begin(), sortIdx.end(), AreaCmp(areas ));

for( int n = 0; n < (int)sortIdx.size(); n++ ) {
int idx = sortIdx[n];
cv::cvtColor( img, img_color, cv::COLOR_GRAY2BGR );
cv::drawContours(
img_color, contours, idx,
cv::Scalar(0,0,255), 2, 8, hierarchy,
0 // Try different values of max_level, and see what happens
);
cout << "Contour #" << idx << ": area=" << areas[idx] <<
", nvertices=" << contours[idx].size() << endl;
cv::imshow(argv[0], img_color);
int k;
if( (k = cv::waitKey()&255) == 27 )
break;
}
cout << "Finished all contours\n";

return 0;
}
51 changes: 51 additions & 0 deletions example_14-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Example 14-3. Drawing labeled connected components

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

using namespace std;

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

cv::Mat img, img_edge, labels, img_color, stats;

// load image or show help if no image was provided
if( argc != 2
|| (img = cv::imread( argv[1], cv::IMREAD_GRAYSCALE )).empty()
) {
cout << "\nExample 8_3 Drawing Connected componnents\n" \
<< "Call is:\n" <<argv[0] <<" image\n\n";
return -1;
}

cv::threshold(img, img_edge, 128, 255, cv::THRESH_BINARY);
cv::imshow("Image after threshold", img_edge);

int i, nccomps = cv::connectedComponentsWithStats (
img_edge, labels,
stats, cv::noArray()
);
cout << "Total Connected Components Detected: " << nccomps << endl;

vector<cv::Vec3b> colors(nccomps+1);
colors[0] = cv::Vec3b(0,0,0); // background pixels remain black.
for( i = 1; i <= nccomps; i++ ) {
colors[i] = cv::Vec3b(rand()%256, rand()%256, rand()%256);
if( stats.at<int>(i-1, cv::CC_STAT_AREA) < 100 )
colors[i] = cv::Vec3b(0,0,0); // small regions are painted with black too.
}
img_color = cv::Mat::zeros(img.size(), CV_8UC3);
for( int y = 0; y < img_color.rows; y++ )
for( int x = 0; x < img_color.cols; x++ )
{
int label = labels.at<int>(y, x);
CV_Assert(0 <= label && label <= nccomps);
img_color.at<cv::Vec3b>(y, x) = colors[label];
}

cv::imshow("Labeled map", img_color);
cv::waitKey();
return 0;
}

54 changes: 54 additions & 0 deletions example_14-04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Example 14-4. Using the shape context distance extractor

#include <algorithm>
#include <iostream>
#include <string>

#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

static vector<Point> sampleContour( const Mat& image, int n=300 ) {

vector<vector<Point> > _contours;
vector<Point> all_points;
findContours(image, _contours, RETR_LIST, CHAIN_APPROX_NONE);
for (size_t i=0; i <_contours.size(); i++) {
for (size_t j=0; j <_contours[i].size(); j++)
all_points.push_back( _contours[i][j] );

// If too little points, replicate them
//
int dummy=0;
for (int add=(int)all_points.size(); add<n; add++)
all_points.push_back(all_points[dummy++]);

// Sample uniformly
random_shuffle(all_points.begin(), all_points.end());
vector<Point> sampled;
for (int i=0; i<n; i++)
sampled.push_back(all_points[i]);
return sampled;
}
}

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

string path = "../data/shape_sample/";
int indexQuery = 1;

Ptr<ShapeContextDistanceExtractor> mysc = createShapeContextDistanceExtractor();

Size sz2Sh(300,300);
Mat img1=imread(argv[1], IMREAD_GRAYSCALE);
Mat img2=imread(argv[2], IMREAD_GRAYSCALE);
vector<Point> c1 = sampleContour(img1);
vector<Point> c2 = sampleContour(img2);
float dis = mysc->computeDistance( c1, c2 );
cout << "shape context distance between " <<
argv[1] << " and " << argv[2] << " is: " << dis << endl;

return 0;

}
Binary file added shape_sample/1.png
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 shape_sample/10.png
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 shape_sample/11.png
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 shape_sample/12.png
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 shape_sample/13.png
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 shape_sample/14.png
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 shape_sample/15.png
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 shape_sample/16.png
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 shape_sample/17.png
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 shape_sample/18.png
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 shape_sample/19.png
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 shape_sample/2.png
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 shape_sample/20.png
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 shape_sample/3.png
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 shape_sample/4.png
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 shape_sample/5.png
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 shape_sample/6.png
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 shape_sample/7.png
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 shape_sample/8.png
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 shape_sample/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bcad9b4

Please sign in to comment.