Sobel_Demo.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file Sobel_Demo.cpp
  3. * @brief Sample code uses Sobel or Scharr OpenCV functions for edge detection
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/highgui.hpp"
  9. #include <iostream>
  10. using namespace cv;
  11. using namespace std;
  12. /**
  13. * @function main
  14. */
  15. int main( int argc, char** argv )
  16. {
  17. cv::CommandLineParser parser(argc, argv,
  18. "{@input |lena.jpg|input image}"
  19. "{ksize k|1|ksize (hit 'K' to increase its value at run time)}"
  20. "{scale s|1|scale (hit 'S' to increase its value at run time)}"
  21. "{delta d|0|delta (hit 'D' to increase its value at run time)}"
  22. "{help h|false|show help message}");
  23. cout << "The sample uses Sobel or Scharr OpenCV functions for edge detection\n\n";
  24. parser.printMessage();
  25. cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
  26. //![variables]
  27. // First we declare the variables we are going to use
  28. Mat image,src, src_gray;
  29. Mat grad;
  30. const String window_name = "Sobel Demo - Simple Edge Detector";
  31. int ksize = parser.get<int>("ksize");
  32. int scale = parser.get<int>("scale");
  33. int delta = parser.get<int>("delta");
  34. int ddepth = CV_16S;
  35. //![variables]
  36. //![load]
  37. String imageName = parser.get<String>("@input");
  38. // As usual we load our source image (src)
  39. image = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
  40. // Check if image is loaded fine
  41. if( image.empty() )
  42. {
  43. printf("Error opening image: %s\n", imageName.c_str());
  44. return EXIT_FAILURE;
  45. }
  46. //![load]
  47. for (;;)
  48. {
  49. //![reduce_noise]
  50. // Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
  51. GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
  52. //![reduce_noise]
  53. //![convert_to_gray]
  54. // Convert the image to grayscale
  55. cvtColor(src, src_gray, COLOR_BGR2GRAY);
  56. //![convert_to_gray]
  57. //![sobel]
  58. /// Generate grad_x and grad_y
  59. Mat grad_x, grad_y;
  60. Mat abs_grad_x, abs_grad_y;
  61. /// Gradient X
  62. Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
  63. /// Gradient Y
  64. Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
  65. //![sobel]
  66. //![convert]
  67. // converting back to CV_8U
  68. convertScaleAbs(grad_x, abs_grad_x);
  69. convertScaleAbs(grad_y, abs_grad_y);
  70. //![convert]
  71. //![blend]
  72. /// Total Gradient (approximate)
  73. addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
  74. //![blend]
  75. //![display]
  76. imshow(window_name, grad);
  77. char key = (char)waitKey(0);
  78. //![display]
  79. if(key == 27)
  80. {
  81. return EXIT_SUCCESS;
  82. }
  83. if (key == 'k' || key == 'K')
  84. {
  85. ksize = ksize < 30 ? ksize+2 : -1;
  86. }
  87. if (key == 's' || key == 'S')
  88. {
  89. scale++;
  90. }
  91. if (key == 'd' || key == 'D')
  92. {
  93. delta++;
  94. }
  95. if (key == 'r' || key == 'R')
  96. {
  97. scale = 1;
  98. ksize = -1;
  99. delta = 0;
  100. }
  101. }
  102. return EXIT_SUCCESS;
  103. }