Doxygen tutorials: cpp done
This commit is contained in:
@@ -17,96 +17,14 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/generalContours_demo1.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@includelineno samples/cpp/tutorial_code/ShapeDescriptors/generalContours_demo1.cpp
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
void thresh_callback(int, void* );
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* @function thresh_callback */
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat threshold_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
/// 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) );
|
||||
|
||||
/// 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( int i = 0; i < contours.size(); i++ )
|
||||
{ approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
|
||||
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
|
||||
minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
|
||||
}
|
||||
|
||||
|
||||
/// Draw polygonal contour + bonding rects + circles
|
||||
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
|
||||
for( int 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, 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 );
|
||||
}
|
||||
|
||||
/// Show in a window
|
||||
namedWindow( "Contours", WINDOW_AUTOSIZE );
|
||||
imshow( "Contours", drawing );
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
1. Here it is:
|
||||
|
||||
---------- ----------
|
||||
|BRC_0| |BRC_1|
|
||||
---------- ----------
|
||||
|
||||
|
||||
Here it is:
|
||||

|
||||

|
||||
|
@@ -17,98 +17,14 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/generalContours_demo2.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@includelineno samples/cpp/tutorial_code/ShapeDescriptors/generalContours_demo2.cpp
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
void thresh_callback(int, void* );
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* @function thresh_callback */
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat threshold_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
/// 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( int i = 0; i < contours.size(); i++ )
|
||||
{ minRect[i] = minAreaRect( Mat(contours[i]) );
|
||||
if( contours[i].size() > 5 )
|
||||
{ minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
|
||||
}
|
||||
|
||||
/// Draw contours + rotated rects + ellipses
|
||||
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
|
||||
for( int 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, 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 );
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
1. Here it is:
|
||||
|
||||
---------- ----------
|
||||
|BRE_0| |BRE_1|
|
||||
---------- ----------
|
||||
|
||||
|
||||
Here it is:
|
||||

|
||||

|
||||
|
@@ -17,81 +17,14 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/findContours_demo.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@includelineno samples/cpp/tutorial_code/ShapeDescriptors/findContours_demo.cpp
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
void thresh_callback(int, void* );
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* @function thresh_callback */
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat canny_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
/// 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) );
|
||||
|
||||
/// Draw contours
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( int i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
|
||||
}
|
||||
|
||||
/// Show in a window
|
||||
namedWindow( "Contours", WINDOW_AUTOSIZE );
|
||||
imshow( "Contours", drawing );
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
1. Here it is:
|
||||
|
||||
-------------- --------------
|
||||
|contour_0| |contour_1|
|
||||
-------------- --------------
|
||||
|
||||
|
||||
Here it is:
|
||||

|
||||

|
||||
|
@@ -18,102 +18,15 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/moments_demo.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@includelineno samples/cpp/tutorial_code/ShapeDescriptors/moments_demo.cpp
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
void thresh_callback(int, void* );
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load source image and convert it to gray
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert image to gray and blur it
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
blur( src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window
|
||||
char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* @function thresh_callback */
|
||||
void thresh_callback(int, void* )
|
||||
{
|
||||
Mat canny_output;
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
|
||||
/// 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) );
|
||||
|
||||
/// Get the moments
|
||||
vector<Moments> mu(contours.size() );
|
||||
for( int i = 0; i < contours.size(); i++ )
|
||||
{ mu[i] = moments( contours[i], false ); }
|
||||
|
||||
/// Get the mass centers:
|
||||
vector<Point2f> mc( contours.size() );
|
||||
for( int i = 0; i < contours.size(); i++ )
|
||||
{ mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
|
||||
|
||||
/// Draw contours
|
||||
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
|
||||
for( int i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
|
||||
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
|
||||
circle( drawing, mc[i], 4, color, -1, 8, 0 );
|
||||
}
|
||||
|
||||
/// 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( int i = 0; i< contours.size(); i++ )
|
||||
{
|
||||
printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", 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, i, color, 2, 8, hierarchy, 0, Point() );
|
||||
circle( drawing, mc[i], 4, color, -1, 8, 0 );
|
||||
}
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
1. Here it is:
|
||||
|
||||
--------- --------- ---------
|
||||
|MU_0| |MU_1| |MU_2|
|
||||
--------- --------- ---------
|
||||
|
||||
|
||||
Here it is:
|
||||

|
||||

|
||||

|
||||
|
@@ -16,91 +16,14 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/pointPolygonTest_demo.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@includelineno samples/cpp/tutorial_code/ShapeDescriptors/pointPolygonTest_demo.cpp
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Create an image
|
||||
const int r = 100;
|
||||
Mat src = Mat::zeros( Size( 4*r, 4*r ), CV_8UC1 );
|
||||
|
||||
/// Create a sequence of points to make a contour:
|
||||
vector<Point2f> vert(6);
|
||||
|
||||
vert[0] = Point( 1.5*r, 1.34*r );
|
||||
vert[1] = Point( 1*r, 2*r );
|
||||
vert[2] = Point( 1.5*r, 2.866*r );
|
||||
vert[3] = Point( 2.5*r, 2.866*r );
|
||||
vert[4] = Point( 3*r, 2*r );
|
||||
vert[5] = Point( 2.5*r, 1.34*r );
|
||||
|
||||
/// 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; vector<Vec4i> hierarchy;
|
||||
Mat src_copy = src.clone();
|
||||
|
||||
findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
||||
|
||||
/// 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) = pointPolygonTest( contours[0], Point2f(i,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] = 255 - (int) abs(raw_dist.at<float>(j,i))*255/minVal; }
|
||||
else if( raw_dist.at<float>(j,i) > 0 )
|
||||
{ drawing.at<Vec3b>(j,i)[2] = 255 - (int) raw_dist.at<float>(j,i)*255/maxVal; }
|
||||
else
|
||||
{ drawing.at<Vec3b>(j,i)[0] = 255; drawing.at<Vec3b>(j,i)[1] = 255; drawing.at<Vec3b>(j,i)[2] = 255; }
|
||||
}
|
||||
}
|
||||
|
||||
/// Create Window and show your results
|
||||
char* source_window = "Source";
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
imshow( source_window, src );
|
||||
namedWindow( "Distance", WINDOW_AUTOSIZE );
|
||||
imshow( "Distance", drawing );
|
||||
|
||||
waitKey(0);
|
||||
return(0);
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
1. Here it is:
|
||||
|
||||
---------- ----------
|
||||
|PPT_0| |PPT_1|
|
||||
---------- ----------
|
||||
|
||||
|
||||
Here it is:
|
||||

|
||||

|
||||
|
Reference in New Issue
Block a user