2011-06-27 07:09:04 +08:00
|
|
|
/**
|
|
|
|
* @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
|
2011-06-27 07:09:04 +08:00
|
|
|
* @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"
|
2011-06-27 07:09:04 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2012-11-07 22:21:20 +08:00
|
|
|
int main( int, char** argv )
|
2011-06-27 07:09:04 +08:00
|
|
|
{
|
2016-07-18 21:32:05 +08:00
|
|
|
//![variables]
|
2011-06-27 07:09:04 +08:00
|
|
|
Mat src, src_gray;
|
2012-10-17 07:18:30 +08:00
|
|
|
Mat grad;
|
2012-11-07 22:21:20 +08:00
|
|
|
const char* window_name = "Sobel Demo - Simple Edge Detector";
|
2011-06-27 07:09:04 +08:00
|
|
|
int scale = 1;
|
|
|
|
int delta = 0;
|
|
|
|
int ddepth = CV_16S;
|
2016-07-18 21:32:05 +08:00
|
|
|
//![variables]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2016-07-18 21:32:05 +08:00
|
|
|
//![load]
|
|
|
|
src = imread( argv[1], IMREAD_COLOR ); // Load an image
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2014-08-13 19:08:27 +08:00
|
|
|
if( src.empty() )
|
2011-06-27 07:09:04 +08:00
|
|
|
{ return -1; }
|
2016-07-18 21:32:05 +08:00
|
|
|
//![load]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2016-07-18 21:32:05 +08:00
|
|
|
//![reduce_noise]
|
2011-06-27 07:09:04 +08:00
|
|
|
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
|
2016-07-18 21:32:05 +08:00
|
|
|
//![reduce_noise]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2016-07-18 21:32:05 +08:00
|
|
|
//![convert_to_gray]
|
|
|
|
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
|
|
|
//![convert_to_gray]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2016-07-18 21:32:05 +08:00
|
|
|
//![sobel]
|
2011-06-27 07:09:04 +08:00
|
|
|
/// 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
|
|
|
|
2011-06-27 07:09:04 +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 );
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
/// Gradient Y
|
2011-06-27 07:09:04 +08:00
|
|
|
//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 );
|
2011-06-27 07:09:04 +08:00
|
|
|
convertScaleAbs( grad_y, abs_grad_y );
|
2016-07-18 21:32:05 +08:00
|
|
|
//![convert]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2016-07-18 21:32:05 +08:00
|
|
|
//![blend]
|
2011-06-27 07:09:04 +08:00
|
|
|
/// 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]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
2016-07-18 21:32:05 +08:00
|
|
|
//![display]
|
2011-06-27 07:09:04 +08:00
|
|
|
imshow( window_name, grad );
|
|
|
|
waitKey(0);
|
2016-07-18 21:32:05 +08:00
|
|
|
//![display]
|
2011-06-27 07:09:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|