mirror of
https://github.com/zebrajr/opencv.git
synced 2026-01-15 12:15:17 +00:00
Add Java and Python code for the following imgproc tutorials: Finding contours in your image, Convex Hull, Creating Bounding boxes and circles for contours, Creating Bounding rotated boxes and ellipses for contours, Image Moments, Point Polygon Test.
This commit is contained in:
@@ -12,9 +12,8 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
@@ -25,34 +24,31 @@ void thresh_callback(int, void* );
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image
|
||||
String imageName("../data/happyfish.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread(imageName, IMREAD_COLOR);
|
||||
/// Load source image
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/HappyFish.jpg | input image}" );
|
||||
Mat src = imread( parser.get<String>( "@input" ) );
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (src.empty())
|
||||
{
|
||||
cerr << "No image supplied ..." << endl;
|
||||
return -1;
|
||||
}
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window );
|
||||
imshow( source_window, src );
|
||||
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
const int max_thresh = 255;
|
||||
createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,24 +56,23 @@ int main( int argc, char** argv )
|
||||
*/
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat canny_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
/// Detect edges using Canny
|
||||
Mat canny_output;
|
||||
Canny( src_gray, canny_output, thresh, thresh*2 );
|
||||
|
||||
/// Detect edges using canny
|
||||
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
|
||||
/// Find contours
|
||||
findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
||||
/// Find contours
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );
|
||||
|
||||
/// Draw contours
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
|
||||
}
|
||||
/// Draw contours
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
|
||||
drawContours( drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0 );
|
||||
}
|
||||
|
||||
/// Show in a window
|
||||
namedWindow( "Contours", WINDOW_AUTOSIZE );
|
||||
imshow( "Contours", drawing );
|
||||
/// Show in a window
|
||||
imshow( "Contours", drawing );
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
@@ -25,42 +24,37 @@ void thresh_callback(int, void* );
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//![setup]
|
||||
/// Load source image
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
|
||||
if( src.empty() )
|
||||
//! [setup]
|
||||
/// Load source image
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
Mat src = imread( parser.get<String>( "@input" ) );
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
//![setup]
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
//! [setup]
|
||||
|
||||
//![createWindow]
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
//![createWindow]
|
||||
//! [createWindow]
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window );
|
||||
imshow( source_window, src );
|
||||
//! [createWindow]
|
||||
|
||||
//![taskbar]
|
||||
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
//![taskbar]
|
||||
//! [trackbar]
|
||||
const int max_thresh = 255;
|
||||
createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
//! [trackbar]
|
||||
|
||||
//![callback00]
|
||||
thresh_callback( 0, 0 );
|
||||
//![callback00]
|
||||
|
||||
//![waitForIt]
|
||||
waitKey(0);
|
||||
//![waitForIt]
|
||||
|
||||
return(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,53 +62,50 @@ int main( int argc, char** argv )
|
||||
*/
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat threshold_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
//! [Canny]
|
||||
/// Detect edges using Canny
|
||||
Mat canny_output;
|
||||
Canny( src_gray, canny_output, thresh, thresh*2 );
|
||||
//! [Canny]
|
||||
|
||||
//![threshold]
|
||||
/// Detect edges using Threshold
|
||||
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
|
||||
//![threshold]
|
||||
//! [findContours]
|
||||
/// Find contours
|
||||
vector<vector<Point> > contours;
|
||||
findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
|
||||
//! [findContours]
|
||||
|
||||
//![findContours]
|
||||
/// Find contours
|
||||
findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
||||
//![findContours]
|
||||
//! [allthework]
|
||||
/// Approximate contours to polygons + get bounding rects and circles
|
||||
vector<vector<Point> > contours_poly( contours.size() );
|
||||
vector<Rect> boundRect( contours.size() );
|
||||
vector<Point2f>centers( contours.size() );
|
||||
vector<float>radius( contours.size() );
|
||||
|
||||
/// Approximate contours to polygons + get bounding rects and circles
|
||||
vector<vector<Point> > contours_poly( contours.size() );
|
||||
vector<Rect> boundRect( contours.size() );
|
||||
vector<Point2f>center( contours.size() );
|
||||
vector<float>radius( contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
approxPolyDP( contours[i], contours_poly[i], 3, true );
|
||||
boundRect[i] = boundingRect( contours_poly[i] );
|
||||
minEnclosingCircle( contours_poly[i], centers[i], radius[i] );
|
||||
}
|
||||
//! [allthework]
|
||||
|
||||
//![allthework]
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
approxPolyDP( contours[i], contours_poly[i], 3, true );
|
||||
boundRect[i] = boundingRect( contours_poly[i] );
|
||||
minEnclosingCircle( contours_poly[i], center[i], radius[i] );
|
||||
}
|
||||
//![allthework]
|
||||
//! [zeroMat]
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
//! [zeroMat]
|
||||
|
||||
//![zeroMat]
|
||||
/// Draw polygonal contour + bonding rects + circles
|
||||
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
|
||||
//![zeroMat]
|
||||
//! [forContour]
|
||||
/// Draw polygonal contour + bonding rects + circles
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
|
||||
drawContours( drawing, contours_poly, (int)i, color );
|
||||
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2 );
|
||||
circle( drawing, centers[i], (int)radius[i], color, 2 );
|
||||
}
|
||||
//! [forContour]
|
||||
|
||||
//![forContour]
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
|
||||
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
|
||||
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
|
||||
}
|
||||
//![forContour]
|
||||
|
||||
//![showDrawings]
|
||||
/// Show in a window
|
||||
namedWindow( "Contours", WINDOW_AUTOSIZE );
|
||||
imshow( "Contours", drawing );
|
||||
//![showDrawings]
|
||||
//! [showDrawings]
|
||||
/// Show in a window
|
||||
imshow( "Contours", drawing );
|
||||
//! [showDrawings]
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
@@ -25,30 +24,31 @@ void thresh_callback(int, void* );
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
|
||||
if( src.empty() )
|
||||
/// Load source image and convert it to gray
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
Mat src = imread( parser.get<String>( "@input" ) );
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
const int max_thresh = 255;
|
||||
createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,41 +56,43 @@ int main( int argc, char** argv )
|
||||
*/
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat threshold_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
/// Detect edges using Canny
|
||||
Mat canny_output;
|
||||
Canny( src_gray, canny_output, thresh, thresh*2 );
|
||||
/// Find contours
|
||||
vector<vector<Point> > contours;
|
||||
findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
||||
|
||||
/// Detect edges using Threshold
|
||||
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
|
||||
/// Find contours
|
||||
findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
||||
/// Find the rotated rectangles and ellipses for each contour
|
||||
vector<RotatedRect> minRect( contours.size() );
|
||||
vector<RotatedRect> minEllipse( contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
minRect[i] = minAreaRect( contours[i] );
|
||||
if( contours[i].size() > 5 )
|
||||
{
|
||||
minEllipse[i] = fitEllipse( contours[i] );
|
||||
}
|
||||
}
|
||||
|
||||
/// Find the rotated rectangles and ellipses for each contour
|
||||
vector<RotatedRect> minRect( contours.size() );
|
||||
vector<RotatedRect> minEllipse( contours.size() );
|
||||
/// Draw contours + rotated rects + ellipses
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
|
||||
// contour
|
||||
drawContours( drawing, contours, (int)i, color );
|
||||
// ellipse
|
||||
ellipse( drawing, minEllipse[i], color, 2 );
|
||||
// rotated rectangle
|
||||
Point2f rect_points[4];
|
||||
minRect[i].points( rect_points );
|
||||
for ( int j = 0; j < 4; j++ )
|
||||
{
|
||||
line( drawing, rect_points[j], rect_points[(j+1)%4], color );
|
||||
}
|
||||
}
|
||||
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{ minRect[i] = minAreaRect( contours[i] );
|
||||
if( contours[i].size() > 5 )
|
||||
{ minEllipse[i] = fitEllipse( contours[i] ); }
|
||||
}
|
||||
|
||||
/// Draw contours + rotated rects + ellipses
|
||||
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
// contour
|
||||
drawContours( drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
|
||||
// ellipse
|
||||
ellipse( drawing, minEllipse[i], color, 2, 8 );
|
||||
// rotated rectangle
|
||||
Point2f rect_points[4]; minRect[i].points( rect_points );
|
||||
for( int j = 0; j < 4; j++ )
|
||||
line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
|
||||
}
|
||||
|
||||
/// Show in a window
|
||||
namedWindow( "Contours", WINDOW_AUTOSIZE );
|
||||
imshow( "Contours", drawing );
|
||||
/// Show in a window
|
||||
imshow( "Contours", drawing );
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
@@ -25,30 +24,31 @@ void thresh_callback(int, void* );
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
/// Load source image and convert it to gray
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
Mat src = imread( parser.get<String>( "@input" ) );
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
const int max_thresh = 255;
|
||||
createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,31 +56,30 @@ int main( int argc, char** argv )
|
||||
*/
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat threshold_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
/// Detect edges using Canny
|
||||
Mat canny_output;
|
||||
Canny( src_gray, canny_output, thresh, thresh*2 );
|
||||
|
||||
/// Detect edges using Threshold
|
||||
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
|
||||
/// Find contours
|
||||
vector<vector<Point> > contours;
|
||||
findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
|
||||
|
||||
/// Find contours
|
||||
findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
||||
/// Find the convex hull object for each contour
|
||||
vector<vector<Point> >hull( contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
convexHull( contours[i], hull[i] );
|
||||
}
|
||||
|
||||
/// Find the convex hull object for each contour
|
||||
vector<vector<Point> >hull( contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{ convexHull( contours[i], hull[i], false ); }
|
||||
/// Draw contours + hull results
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
|
||||
drawContours( drawing, contours, (int)i, color );
|
||||
drawContours( drawing, hull, (int)i, color );
|
||||
}
|
||||
|
||||
/// Draw contours + hull results
|
||||
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
|
||||
drawContours( drawing, hull, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
|
||||
}
|
||||
|
||||
/// Show in a window
|
||||
namedWindow( "Hull demo", WINDOW_AUTOSIZE );
|
||||
imshow( "Hull demo", drawing );
|
||||
/// Show in a window
|
||||
imshow( "Hull demo", drawing );
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
@@ -25,31 +25,32 @@ void thresh_callback(int, void* );
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
|
||||
/// Load source image
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/stuff.jpg | input image}" );
|
||||
Mat src = imread( parser.get<String>( "@input" ) );
|
||||
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "usage: " << argv[0] << " <Input image>" << endl;
|
||||
exit(0);
|
||||
}
|
||||
if( src.empty() )
|
||||
{
|
||||
cout << "Could not open or find the image!\n" << endl;
|
||||
cout << "usage: " << argv[0] << " <Input image>" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
/// Create Window
|
||||
const char* source_window = "Source";
|
||||
namedWindow( source_window );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
const int max_thresh = 255;
|
||||
createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,44 +58,47 @@ int main( int argc, char** argv )
|
||||
*/
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat canny_output;
|
||||
vector<vector<Point> > contours;
|
||||
/// Detect edges using canny
|
||||
Mat canny_output;
|
||||
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
|
||||
/// Find contours
|
||||
vector<vector<Point> > contours;
|
||||
findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
|
||||
|
||||
/// Detect edges using canny
|
||||
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
|
||||
/// Find contours
|
||||
findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
|
||||
/// Get the moments
|
||||
vector<Moments> mu(contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
mu[i] = moments( contours[i] );
|
||||
}
|
||||
|
||||
/// Get the moments
|
||||
vector<Moments> mu(contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{ mu[i] = moments( contours[i], false ); }
|
||||
/// Get the mass centers
|
||||
vector<Point2f> mc( contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
//add 1e-5 to avoid division by zero
|
||||
mc[i] = Point2f( static_cast<float>(mu[i].m10 / (mu[i].m00 + 1e-5)),
|
||||
static_cast<float>(mu[i].m01 / (mu[i].m00 + 1e-5)) );
|
||||
cout << "mc[" << i << "]=" << mc[i] << endl;
|
||||
}
|
||||
|
||||
/// Get the mass centers:
|
||||
vector<Point2f> mc( contours.size() );
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{ mc[i] = Point2f( static_cast<float>(mu[i].m10/mu[i].m00) , static_cast<float>(mu[i].m01/mu[i].m00) ); }
|
||||
/// Draw contours
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
|
||||
drawContours( drawing, contours, (int)i, color, 2 );
|
||||
circle( drawing, mc[i], 4, color, -1 );
|
||||
}
|
||||
|
||||
/// Draw contours
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours, (int)i, color, 2, LINE_8 );
|
||||
circle( drawing, mc[i], 4, color, -1, 8, 0 );
|
||||
}
|
||||
/// Show in a window
|
||||
imshow( "Contours", drawing );
|
||||
|
||||
/// Show in a window
|
||||
namedWindow( "Contours", WINDOW_AUTOSIZE );
|
||||
imshow( "Contours", drawing );
|
||||
|
||||
/// Calculate the area with the moments 00 and compare with the result of the OpenCV function
|
||||
printf("\t Info: Area and Contour Length \n");
|
||||
for( size_t i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", (int)i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours, (int)i, color, 2, LINE_8 );
|
||||
circle( drawing, mc[i], 4, color, -1, 8, 0 );
|
||||
}
|
||||
/// Calculate the area with the moments 00 and compare with the result of the OpenCV function
|
||||
cout << "\t Info: Area and Contour Length \n";
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
cout << " * Contour[" << i << "] - Area (M_00) = " << std::fixed << std::setprecision(2) << mu[i].m00
|
||||
<< " - Area OpenCV: " << contourArea(contours[i]) << " - Length: " << arcLength( contours[i], true ) << endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,60 +16,71 @@ using namespace std;
|
||||
*/
|
||||
int main( void )
|
||||
{
|
||||
/// Create an image
|
||||
const int r = 100;
|
||||
Mat src = Mat::zeros( Size( 4*r, 4*r ), CV_8UC1 );
|
||||
/// Create an image
|
||||
const int r = 100;
|
||||
Mat src = Mat::zeros( Size( 4*r, 4*r ), CV_8U );
|
||||
|
||||
/// Create a sequence of points to make a contour:
|
||||
vector<Point2f> vert(6);
|
||||
/// Create a sequence of points to make a contour
|
||||
vector<Point2f> vert(6);
|
||||
vert[0] = Point( 3*r/2, static_cast<int>(1.34*r) );
|
||||
vert[1] = Point( 1*r, 2*r );
|
||||
vert[2] = Point( 3*r/2, static_cast<int>(2.866*r) );
|
||||
vert[3] = Point( 5*r/2, static_cast<int>(2.866*r) );
|
||||
vert[4] = Point( 3*r, 2*r );
|
||||
vert[5] = Point( 5*r/2, static_cast<int>(1.34*r) );
|
||||
|
||||
vert[0] = Point( 3*r/2, static_cast<int>(1.34*r) );
|
||||
vert[1] = Point( 1*r, 2*r );
|
||||
vert[2] = Point( 3*r/2, static_cast<int>(2.866*r) );
|
||||
vert[3] = Point( 5*r/2, static_cast<int>(2.866*r) );
|
||||
vert[4] = Point( 3*r, 2*r );
|
||||
vert[5] = Point( 5*r/2, static_cast<int>(1.34*r) );
|
||||
/// Draw it in src
|
||||
for( int i = 0; i < 6; i++ )
|
||||
{
|
||||
line( src, vert[i], vert[(i+1)%6], Scalar( 255 ), 3 );
|
||||
}
|
||||
|
||||
/// Draw it in src
|
||||
for( int j = 0; j < 6; j++ )
|
||||
{ line( src, vert[j], vert[(j+1)%6], Scalar( 255 ), 3, 8 ); }
|
||||
/// Get the contours
|
||||
vector<vector<Point> > contours;
|
||||
findContours( src, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
||||
|
||||
/// Get the contours
|
||||
vector<vector<Point> > contours;
|
||||
/// Calculate the distances to the contour
|
||||
Mat raw_dist( src.size(), CV_32F );
|
||||
for( int i = 0; i < src.rows; i++ )
|
||||
{
|
||||
for( int j = 0; j < src.cols; j++ )
|
||||
{
|
||||
raw_dist.at<float>(i,j) = (float)pointPolygonTest( contours[0], Point2f((float)j, (float)i), true );
|
||||
}
|
||||
}
|
||||
|
||||
findContours( src, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
||||
double minVal, maxVal;
|
||||
minMaxLoc( raw_dist, &minVal, &maxVal );
|
||||
minVal = abs(minVal);
|
||||
maxVal = abs(maxVal);
|
||||
|
||||
/// Calculate the distances to the contour
|
||||
Mat raw_dist( src.size(), CV_32FC1 );
|
||||
|
||||
for( int j = 0; j < src.rows; j++ )
|
||||
{ for( int i = 0; i < src.cols; i++ )
|
||||
{ raw_dist.at<float>(j,i) = (float)pointPolygonTest( contours[0], Point2f((float)i,(float)j), true ); }
|
||||
}
|
||||
|
||||
double minVal; double maxVal;
|
||||
minMaxLoc( raw_dist, &minVal, &maxVal, 0, 0, Mat() );
|
||||
minVal = abs(minVal); maxVal = abs(maxVal);
|
||||
|
||||
/// Depicting the distances graphically
|
||||
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
|
||||
|
||||
for( int j = 0; j < src.rows; j++ )
|
||||
{ for( int i = 0; i < src.cols; i++ )
|
||||
{
|
||||
if( raw_dist.at<float>(j,i) < 0 )
|
||||
{ drawing.at<Vec3b>(j,i)[0] = (uchar)(255 - abs(raw_dist.at<float>(j,i))*255/minVal); }
|
||||
else if( raw_dist.at<float>(j,i) > 0 )
|
||||
{ drawing.at<Vec3b>(j,i)[2] = (uchar)(255 - raw_dist.at<float>(j,i)*255/maxVal); }
|
||||
/// Depicting the distances graphically
|
||||
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
|
||||
for( int i = 0; i < src.rows; i++ )
|
||||
{
|
||||
for( int j = 0; j < src.cols; j++ )
|
||||
{
|
||||
if( raw_dist.at<float>(i,j) < 0 )
|
||||
{
|
||||
drawing.at<Vec3b>(i,j)[0] = (uchar)(255 - abs(raw_dist.at<float>(i,j)) * 255 / minVal);
|
||||
}
|
||||
else if( raw_dist.at<float>(i,j) > 0 )
|
||||
{
|
||||
drawing.at<Vec3b>(i,j)[2] = (uchar)(255 - raw_dist.at<float>(i,j) * 255 / maxVal);
|
||||
}
|
||||
else
|
||||
{ drawing.at<Vec3b>(j,i)[0] = 255; drawing.at<Vec3b>(j,i)[1] = 255; drawing.at<Vec3b>(j,i)[2] = 255; }
|
||||
}
|
||||
}
|
||||
{
|
||||
drawing.at<Vec3b>(i,j)[0] = 255;
|
||||
drawing.at<Vec3b>(i,j)[1] = 255;
|
||||
drawing.at<Vec3b>(i,j)[2] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show your results
|
||||
imshow( "Source", src );
|
||||
imshow( "Distance", drawing );
|
||||
/// Show your results
|
||||
imshow( "Source", src );
|
||||
imshow( "Distance", drawing );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user