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.
- Loading branch information
Prasanna Krishnasamy
committed
May 21, 2017
1 parent
ca1ef89
commit bcad9b4
Showing
25 changed files
with
232 additions
and
4 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
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,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; | ||
} |
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,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; | ||
} |
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,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; | ||
} | ||
|
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,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; | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.