Laplace_Demo.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @file Laplace_Demo.cpp
  3. * @brief Sample code showing how to detect edges using the Laplace operator
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/highgui.hpp"
  9. using namespace cv;
  10. /**
  11. * @function main
  12. */
  13. int main( int argc, char** argv )
  14. {
  15. //![variables]
  16. // Declare the variables we are going to use
  17. Mat src, src_gray, dst;
  18. int kernel_size = 3;
  19. int scale = 1;
  20. int delta = 0;
  21. int ddepth = CV_16S;
  22. const char* window_name = "Laplace Demo";
  23. //![variables]
  24. //![load]
  25. const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
  26. src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
  27. // Check if image is loaded fine
  28. if(src.empty()){
  29. printf(" Error opening image\n");
  30. printf(" Program Arguments: [image_name -- default lena.jpg] \n");
  31. return -1;
  32. }
  33. //![load]
  34. //![reduce_noise]
  35. // Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
  36. GaussianBlur( src, src, Size(3, 3), 0, 0, BORDER_DEFAULT );
  37. //![reduce_noise]
  38. //![convert_to_gray]
  39. cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
  40. //![convert_to_gray]
  41. /// Apply Laplace function
  42. Mat abs_dst;
  43. //![laplacian]
  44. Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
  45. //![laplacian]
  46. //![convert]
  47. // converting back to CV_8U
  48. convertScaleAbs( dst, abs_dst );
  49. //![convert]
  50. //![display]
  51. imshow( window_name, abs_dst );
  52. waitKey(0);
  53. //![display]
  54. return 0;
  55. }