samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp#

Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector Sample screenshot
Check the corresponding tutorial for more details

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