opencv/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp

73 lines
1.6 KiB
C++
Raw Normal View History

/**
* @file Sobel_Demo.cpp
2016-07-18 21:32:05 +08:00
* @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
* @author OpenCV team
*/
2016-02-15 21:37:29 +08:00
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
using namespace cv;
/**
* @function main
*/
int main( int, char** argv )
{
2016-07-18 21:32:05 +08:00
//![variables]
Mat src, src_gray;
2012-10-17 07:18:30 +08:00
Mat grad;
const char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
2016-07-18 21:32:05 +08:00
//![variables]
2016-07-18 21:32:05 +08:00
//![load]
src = imread( argv[1], IMREAD_COLOR ); // Load an image
if( src.empty() )
{ return -1; }
2016-07-18 21:32:05 +08:00
//![load]
2016-07-18 21:32:05 +08:00
//![reduce_noise]
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
2016-07-18 21:32:05 +08:00
//![reduce_noise]
2016-07-18 21:32:05 +08:00
//![convert_to_gray]
cvtColor( src, src_gray, COLOR_BGR2GRAY );
//![convert_to_gray]
2016-07-18 21:32:05 +08:00
//![sobel]
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
2012-10-17 07:18:30 +08:00
/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
2012-10-17 07:18:30 +08:00
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
2012-10-17 07:18:30 +08:00
/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
2012-10-17 07:18:30 +08:00
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
2016-07-18 21:32:05 +08:00
//![sobel]
//![convert]
convertScaleAbs( grad_x, abs_grad_x );
convertScaleAbs( grad_y, abs_grad_y );
2016-07-18 21:32:05 +08:00
//![convert]
2016-07-18 21:32:05 +08:00
//![blend]
/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
2016-07-18 21:32:05 +08:00
//![blend]
2016-07-18 21:32:05 +08:00
//![display]
imshow( window_name, grad );
waitKey(0);
2016-07-18 21:32:05 +08:00
//![display]
return 0;
}