Update documentation ( tutorials )

This commit is contained in:
Suleyman TURKMEN
2016-07-18 16:32:05 +03:00
parent 55d0945149
commit bb6f65c199
34 changed files with 369 additions and 1333 deletions
@@ -21,6 +21,7 @@ Mat src1;
Mat src2;
Mat dst;
//![on_trackbar]
/**
* @function on_trackbar
* @brief Callback for trackbar
@@ -35,7 +36,7 @@ static void on_trackbar( int, void* )
imshow( "Linear Blend", dst );
}
//![on_trackbar]
/**
* @function main
@@ -43,9 +44,11 @@ static void on_trackbar( int, void* )
*/
int main( void )
{
/// Read image ( same size, same type )
//![load]
/// Read images ( both have to be of the same size and type )
src1 = imread("../data/LinuxLogo.jpg");
src2 = imread("../data/WindowsLogo.jpg");
//![load]
if( src1.empty() ) { printf("Error loading src1 \n"); return -1; }
if( src2.empty() ) { printf("Error loading src2 \n"); return -1; }
@@ -53,13 +56,15 @@ int main( void )
/// Initialize values
alpha_slider = 0;
/// Create Windows
namedWindow("Linear Blend", 1);
//![window]
namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
//![window]
/// Create Trackbars
//![create_trackbar]
char TrackbarName[50];
sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
//![create_trackbar]
/// Show some stuff
on_trackbar( alpha_slider, 0 );
@@ -66,6 +66,7 @@ int main( int, char** argv )
return 0;
}
//![erosion]
/**
* @function Erosion
*/
@@ -76,14 +77,19 @@ void Erosion( int, void* )
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
//![kernel]
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
//![kernel]
/// Apply the erosion operation
erode( src, erosion_dst, element );
imshow( "Erosion Demo", erosion_dst );
}
//![erosion]
//![dilation]
/**
* @function Dilation
*/
@@ -97,7 +103,9 @@ void Dilation( int, void* )
Mat element = getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
/// Apply the dilation operation
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
}
//![dilation]
@@ -31,27 +31,35 @@ void Morphology_Operations( int, void* );
*/
int main( int, char** argv )
{
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
//![load]
/// Create window
namedWindow( window_name, WINDOW_AUTOSIZE );
//![window]
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create window
//![window]
//![create_trackbar1]
/// Create Trackbar to select Morphology operation
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", window_name, &morph_operator, max_operator, Morphology_Operations );
//![create_trackbar1]
//![create_trackbar2]
/// Create Trackbar to select kernel type
createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
&morph_elem, max_elem,
Morphology_Operations );
//![create_trackbar2]
//![create_trackbar3]
/// Create Trackbar to choose kernel size
createTrackbar( "Kernel size:\n 2n +1", window_name,
&morph_size, max_kernel_size,
Morphology_Operations );
//![create_trackbar3]
/// Default start
Morphology_Operations( 0, 0 );
@@ -60,13 +68,16 @@ int main( int, char** argv )
return 0;
}
//![morphology_operations]
/**
* @function Morphology_Operations
*/
void Morphology_Operations( int, void* )
{
// Since MORPH_X : 2,3,4,5 and 6
//![operation]
int operation = morph_operator + 2;
//![operation]
Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
@@ -74,3 +85,4 @@ void Morphology_Operations( int, void* )
morphologyEx( src, dst, operation, element );
imshow( window_name, dst );
}
//![morphology_operations]
+19 -8
View File
@@ -28,39 +28,50 @@ int main( void )
printf( " * [d] -> Zoom out \n" );
printf( " * [ESC] -> Close program \n \n" );
/// Test image - Make sure it s divisible by 2^{n}
src = imread( "../data/chicky_512.png" );
//![load]
src = imread( "../data/chicky_512.png" ); // Loads the test image
if( src.empty() )
{ printf(" No data! -- Exiting the program \n");
return -1; }
//![load]
tmp = src;
dst = tmp;
/// Create window
namedWindow( window_name, WINDOW_AUTOSIZE );
//![create_window]
imshow( window_name, dst );
//![create_window]
/// Loop
//![infinite_loop]
for(;;)
{
int c;
c = waitKey(10);
c = waitKey(0);
if( (char)c == 27 )
{ break; }
if( (char)c == 'u' )
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
{
//![pyrup]
pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
//![pyrup]
printf( "** Zoom In: Image x 2 \n" );
}
else if( (char)c == 'd' )
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
{
//![pyrdown]
pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
//![pyrdown]
printf( "** Zoom Out: Image / 2 \n" );
}
imshow( window_name, dst );
//![update_tmp]
tmp = dst;
//![update_tmp]
}
//![infinite_loop]
return 0;
}
@@ -43,33 +43,38 @@ int main( void )
/// Applying Homogeneous blur
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
//![blur]
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
//![blur]
/// Applying Gaussian blur
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
//![gaussianblur]
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
//![gaussianblur]
/// Applying Median blur
if( display_caption( "Median Blur" ) != 0 ) { return 0; }
//![medianblur]
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ medianBlur ( src, dst, i );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
//![medianblur]
/// Applying Bilateral Filter
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
//![bilateralfilter]
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ bilateralFilter ( src, dst, i, i*2, i/2 );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
//![bilateralfilter]
/// Wait until user press a key
display_caption( "End: Press a key!" );
+14 -12
View File
@@ -32,29 +32,30 @@ void Threshold_Demo( int, void* );
*/
int main( int, char** argv )
{
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//! [load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
/// Convert the image to Gray
cvtColor( src, src_gray, COLOR_BGR2GRAY );
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
//! [load]
/// Create a window to display results
namedWindow( window_name, WINDOW_AUTOSIZE );
//! [window]
namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
//! [window]
/// Create Trackbar to choose type of Threshold
//! [trackbar]
createTrackbar( trackbar_type,
window_name, &threshold_type,
max_type, Threshold_Demo );
max_type, Threshold_Demo ); // Create Trackbar to choose type of Threshold
createTrackbar( trackbar_value,
window_name, &threshold_value,
max_value, Threshold_Demo );
max_value, Threshold_Demo ); // Create Trackbar to choose Threshold value
//! [trackbar]
/// Call the function to initialize
Threshold_Demo( 0, 0 );
Threshold_Demo( 0, 0 ); // Call the function to initialize
/// Wait until user finishes program
for(;;)
@@ -67,7 +68,7 @@ int main( int, char** argv )
}
//![Threshold_Demo]
/**
* @function Threshold_Demo
*/
@@ -84,3 +85,4 @@ void Threshold_Demo( int, void* )
imshow( window_name, dst );
}
//![Threshold_Demo]
@@ -10,8 +10,7 @@
using namespace cv;
/// Global variables
//![variables]
Mat src, src_gray;
Mat dst, detected_edges;
@@ -21,6 +20,7 @@ int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
const char* window_name = "Edge Map";
//![variables]
/**
* @function CannyThreshold
@@ -28,17 +28,28 @@ const char* window_name = "Edge Map";
*/
static void CannyThreshold(int, void*)
{
//![reduce_noise]
/// Reduce noise with a kernel 3x3
blur( src_gray, detected_edges, Size(3,3) );
//![reduce_noise]
//![canny]
/// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
//![canny]
/// Using Canny's output as a mask, we display our result
//![fill]
dst = Scalar::all(0);
//![fill]
//![copyto]
src.copyTo( dst, detected_edges);
//![copyto]
//![display]
imshow( window_name, dst );
//![display]
}
@@ -47,23 +58,30 @@ static void CannyThreshold(int, void*)
*/
int main( int, char** argv )
{
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
//![load]
//![create_mat]
/// Create a matrix of the same type and size as src (for dst)
dst.create( src.size(), src.type() );
//![create_mat]
/// Convert the image to grayscale
//![convert_to_gray]
cvtColor( src, src_gray, COLOR_BGR2GRAY );
//![convert_to_gray]
/// Create a window
//![create_window]
namedWindow( window_name, WINDOW_AUTOSIZE );
//![create_window]
//![create_trackbar]
/// Create a Trackbar for user to enter threshold
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
//![create_trackbar]
/// Show the image
CannyThreshold(0, 0);
@@ -15,39 +15,45 @@ using namespace cv;
*/
int main( int, char** argv )
{
//![variables]
Mat src, src_gray, dst;
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
const char* window_name = "Laplace Demo";
//![variables]
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
//![load]
/// Remove noise by blurring with a Gaussian filter
//![reduce_noise]
/// Reduce noise by blurring with a Gaussian filter
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
//![reduce_noise]
/// Convert the image to grayscale
cvtColor( src, src_gray, COLOR_RGB2GRAY );
/// Create window
namedWindow( window_name, WINDOW_AUTOSIZE );
//![convert_to_gray]
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
//![convert_to_gray]
/// Apply Laplace function
Mat abs_dst;
//![laplacian]
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
//![laplacian]
//![convert]
convertScaleAbs( dst, abs_dst );
//![convert]
/// Show what you got
//![display]
imshow( window_name, abs_dst );
waitKey(0);
//![display]
return 0;
}
@@ -1,6 +1,6 @@
/**
* @file Sobel_Demo.cpp
* @brief Sample code using Sobel and/orScharr OpenCV functions to make a simple Edge Detector
* @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
* @author OpenCV team
*/
@@ -15,28 +15,31 @@ using namespace cv;
*/
int main( int, char** argv )
{
//![variables]
Mat src, src_gray;
Mat grad;
const char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
//![variables]
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
//![load]
//![reduce_noise]
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
//![reduce_noise]
/// Convert it to gray
cvtColor( src, src_gray, COLOR_RGB2GRAY );
/// Create window
namedWindow( window_name, WINDOW_AUTOSIZE );
//![convert_to_gray]
cvtColor( src, src_gray, COLOR_BGR2GRAY );
//![convert_to_gray]
//![sobel]
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
@@ -44,19 +47,26 @@ int main( int, char** argv )
/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
//![sobel]
//![convert]
convertScaleAbs( grad_x, abs_grad_x );
convertScaleAbs( grad_y, abs_grad_y );
//![convert]
//![blend]
/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
//![blend]
//![display]
imshow( window_name, grad );
waitKey(0);
//![display]
return 0;
}
@@ -10,12 +10,13 @@
using namespace cv;
/// Global Variables
//![variables]
Mat src, dst;
int top, bottom, left, right;
int borderType;
const char* window_name = "copyMakeBorder Demo";
RNG rng(12345);
//![variables]
/**
* @function main
@@ -25,14 +26,15 @@ int main( int, char** argv )
int c;
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{
printf(" No data entered, please enter the path to an image file \n");
return -1;
}
//![load]
/// Brief how-to for this program
printf( "\n \t copyMakeBorder Demo: \n" );
@@ -41,18 +43,22 @@ int main( int, char** argv )
printf( " ** Press 'r' to set the border to be replicated \n");
printf( " ** Press 'ESC' to exit the program \n");
/// Create window
//![create_window]
namedWindow( window_name, WINDOW_AUTOSIZE );
//![create_window]
//![init_arguments]
/// Initialize arguments for the filter
top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows);
left = (int) (0.05*src.cols); right = (int) (0.05*src.cols);
dst = src;
//![init_arguments]
dst = src;
imshow( window_name, dst );
for(;;)
{
//![check_keypress]
c = waitKey(500);
if( (char)c == 27 )
@@ -61,11 +67,19 @@ int main( int, char** argv )
{ borderType = BORDER_CONSTANT; }
else if( (char)c == 'r' )
{ borderType = BORDER_REPLICATE; }
//![check_keypress]
//![update_value]
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
//![update_value]
//![copymakeborder]
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
//![copymakeborder]
//![display]
imshow( window_name, dst );
//![display]
}
return 0;
@@ -27,19 +27,19 @@ int main ( int, char** argv )
int c;
/// Load an image
src = imread( argv[1], IMREAD_COLOR );
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
//![load]
/// Create window
namedWindow( window_name, WINDOW_AUTOSIZE );
//![init_arguments]
/// Initialize arguments for the filter
anchor = Point( -1, -1 );
delta = 0;
ddepth = -1;
//![init_arguments]
/// Loop - Will filter the image with different kernel sizes each 0.5 seconds
int ind = 0;
@@ -50,12 +50,15 @@ int main ( int, char** argv )
if( (char)c == 27 )
{ break; }
//![update_kernel]
/// Update kernel size for a normalized box filter
kernel_size = 3 + 2*( ind%5 );
kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);
//![update_kernel]
/// Apply filter
//![apply_filter]
filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
//![apply_filter]
imshow( window_name, dst );
ind++;
}
@@ -16,7 +16,6 @@ using namespace cv;
*/
int main( void )
{
double alpha = 0.5; double beta; double input;
Mat src1, src2, dst;
@@ -27,26 +26,28 @@ int main( void )
std::cout<<"* Enter alpha [0-1]: ";
std::cin>>input;
// We use the alpha provided by the user iff it is between 0 and 1
// We use the alpha provided by the user if it is between 0 and 1
if( alpha >= 0 && alpha <= 1 )
{ alpha = input; }
/// Read image ( same size, same type )
//![load]
/// Read images ( both have to be of the same size and type )
src1 = imread("../data/LinuxLogo.jpg");
src2 = imread("../data/WindowsLogo.jpg");
//![load]
if( src1.empty() ) { std::cout<< "Error loading src1"<<std::endl; return -1; }
if( src2.empty() ) { std::cout<< "Error loading src2"<<std::endl; return -1; }
/// Create Windows
namedWindow("Linear Blend", 1);
//![blend_images]
beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta, 0.0, dst);
//![blend_images]
//![display]
imshow( "Linear Blend", dst );
waitKey(0);
//![display]
return 0;
}
@@ -23,6 +23,7 @@ void MyLine( Mat img, Point start, Point end );
*/
int main( void ){
//![create_images]
/// Windows names
char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook";
@@ -30,10 +31,12 @@ int main( void ){
/// Create black empty images
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
//![create_images]
/// 1. Draw a simple atom:
/// -----------------------
//![draw_atom]
/// 1.a. Creating ellipses
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
@@ -42,26 +45,31 @@ int main( void ){
/// 1.b. Creating circles
MyFilledCircle( atom_image, Point( w/2, w/2) );
//![draw_atom]
/// 2. Draw a rook
/// ------------------
//![draw_rook]
/// 2.a. Create a convex polygon
MyPolygon( rook_image );
//![rectangle]
/// 2.b. Creating rectangles
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
-1,
8 );
FILLED,
LINE_8 );
//![rectangle]
/// 2.c. Create a few lines
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
//![draw_rook]
/// 3. Display your stuff!
imshow( atom_window, atom_image );
@@ -75,6 +83,7 @@ int main( void ){
/// Function Declaration
//![myellipse]
/**
* @function MyEllipse
* @brief Draw a fixed-size ellipse with different angles
@@ -94,31 +103,32 @@ void MyEllipse( Mat img, double angle )
thickness,
lineType );
}
//![myellipse]
//![myfilledcircle]
/**
* @function MyFilledCircle
* @brief Draw a fixed-size filled circle
*/
void MyFilledCircle( Mat img, Point center )
{
int thickness = -1;
int lineType = 8;
circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
thickness,
lineType );
FILLED,
LINE_8 );
}
//![myfilledcircle]
//![mypolygon]
/**
* @function MyPolygon
* @function Draw a simple concave polygon (rook)
* @brief Draw a simple concave polygon (rook)
*/
void MyPolygon( Mat img )
{
int lineType = 8;
int lineType = LINE_8;
/** Create some points */
Point rook_points[1][20];
@@ -149,11 +159,13 @@ void MyPolygon( Mat img )
fillPoly( img,
ppt,
npt,
1,
1,
Scalar( 255, 255, 255 ),
lineType );
}
//![mypolygon]
//![myline]
/**
* @function MyLine
* @brief Draw a simple line
@@ -161,7 +173,7 @@ void MyPolygon( Mat img )
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = 8;
int lineType = LINE_8;
line( img,
start,
end,
@@ -169,3 +181,4 @@ void MyLine( Mat img, Point start, Point end )
thickness,
lineType );
}
//![myline]
@@ -166,10 +166,14 @@ int main( int argc, char* argv[] ){
// load the image (note that we don't have the projection information. You will
// need to load that yourself or use the full GDAL driver. The values are pre-defined
// at the top of this file
//![load1]
cv::Mat image = cv::imread(argv[1], cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR );
//![load1]
//![load2]
// load the dem model
cv::Mat dem = cv::imread(argv[2], cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH );
//![load2]
// create our output products
cv::Mat output_dem( image.size(), CV_8UC3 );