Advertisement
jyun14

Sobel

Nov 24th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. /**
  2.  * @file Sobel_Demo.cpp
  3.  * @brief Sample code using Sobel and/orScharr OpenCV functions to make a simple Edge Detector
  4.  * @author OpenCV team
  5.  */
  6.  
  7. #include "opencv2/imgproc/imgproc.hpp"
  8. #include "opencv2/highgui/highgui.hpp"
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. using namespace cv;
  13.  
  14. /**
  15.  * @function main
  16.  */
  17. int main( int, char** argv )
  18. {
  19.  
  20.   Mat src, src_gray;
  21.   Mat grad;
  22.   const char* window_name = "Sobel Demo - Simple Edge Detector";
  23.   int scale = 1;
  24.   int delta = 0;
  25.   int ddepth = CV_16S;
  26.  
  27.   /// Load an image
  28.   src = imread( argv[1] );
  29.  
  30.   if( !src.data )
  31.     { return -1; }
  32.  
  33.   GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
  34.  
  35.   /// Convert it to gray
  36.   cvtColor( src, src_gray, COLOR_RGB2GRAY );
  37.  
  38.   /// Create window
  39.   //namedWindow( window_name, WINDOW_AUTOSIZE );
  40.  
  41.   /// Generate grad_x and grad_y
  42.   Mat grad_x, grad_y;
  43.   Mat abs_grad_x, abs_grad_y;
  44.  
  45.   /// Gradient X
  46.   //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
  47.   Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
  48.   convertScaleAbs( grad_x, abs_grad_x );
  49.  
  50.   /// Gradient Y
  51.   //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
  52.   Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
  53.   convertScaleAbs( grad_y, abs_grad_y );
  54.  
  55.   /// Total Gradient (approximate)
  56.   addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
  57.   imwrite( argv[2],grad );
  58.     /*imshow( window_name, grad );*/
  59.  
  60.   waitKey(0);
  61.  
  62.   return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement