opencv/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp

125 lines
3.0 KiB
C++
Raw Normal View History

/**
* @file Sobel_Demo.cpp
2017-08-31 21:39:28 +08:00
* @brief Sample code uses Sobel or Scharr OpenCV functions for edge detection
* @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"
2017-08-31 21:39:28 +08:00
#include <iostream>
using namespace cv;
2017-08-31 21:39:28 +08:00
using namespace std;
/**
* @function main
*/
2017-07-26 13:39:53 +08:00
int main( int argc, char** argv )
{
2017-08-31 21:39:28 +08:00
cv::CommandLineParser parser(argc, argv,
"{@input |../data/lena.jpg|input image}"
"{ksize k|1|ksize (hit 'K' to increase its value)}"
"{scale s|1|scale (hit 'S' to increase its value)}"
"{delta d|0|delta (hit 'D' to increase its value)}"
"{help h|false|show help message}");
cout << "The sample uses Sobel or Scharr OpenCV functions for edge detection\n\n";
parser.printMessage();
cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
2016-07-18 21:32:05 +08:00
//![variables]
2017-08-22 19:28:23 +08:00
// First we declare the variables we are going to use
2017-08-31 21:39:28 +08:00
Mat image,src, src_gray;
2012-10-17 07:18:30 +08:00
Mat grad;
2017-08-31 21:39:28 +08:00
const String window_name = "Sobel Demo - Simple Edge Detector";
int ksize = parser.get<int>("ksize");
int scale = parser.get<int>("scale");
int delta = parser.get<int>("delta");
int ddepth = CV_16S;
2016-07-18 21:32:05 +08:00
//![variables]
2016-07-18 21:32:05 +08:00
//![load]
2017-08-22 19:28:23 +08:00
String imageName = parser.get<String>("@input");
// As usual we load our source image (src)
2017-08-31 21:39:28 +08:00
image = imread( imageName, IMREAD_COLOR ); // Load an image
2017-08-22 19:28:23 +08:00
// Check if image is loaded fine
2017-08-31 21:39:28 +08:00
if( image.empty() )
2017-07-26 13:39:53 +08:00
{
2017-08-22 19:28:23 +08:00
printf("Error opening image: %s\n", imageName.c_str());
2017-08-31 21:39:28 +08:00
return 1;
2017-07-26 13:39:53 +08:00
}
2016-07-18 21:32:05 +08:00
//![load]
2017-08-31 21:39:28 +08:00
for (;;)
{
//![reduce_noise]
2017-08-22 19:28:23 +08:00
// Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
2017-08-31 21:39:28 +08:00
GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
//![reduce_noise]
2017-08-31 21:39:28 +08:00
//![convert_to_gray]
2017-08-22 19:28:23 +08:00
// Convert the image to grayscale
2017-08-31 21:39:28 +08:00
cvtColor(src, src_gray, COLOR_BGR2GRAY);
//![convert_to_gray]
2017-08-31 21:39:28 +08:00
//![sobel]
/// 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
2017-08-31 21:39:28 +08:00
/// Gradient X
Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
2017-08-31 21:39:28 +08:00
/// Gradient Y
Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
//![sobel]
2016-07-18 21:32:05 +08:00
2017-08-31 21:39:28 +08:00
//![convert]
2017-08-22 19:28:23 +08:00
// converting back to CV_8U
2017-08-31 21:39:28 +08:00
convertScaleAbs(grad_x, abs_grad_x);
convertScaleAbs(grad_y, abs_grad_y);
//![convert]
2017-08-31 21:39:28 +08:00
//![blend]
/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
//![blend]
2017-08-31 21:39:28 +08:00
//![display]
imshow(window_name, grad);
char key = (char)waitKey(0);
//![display]
2017-08-31 21:39:28 +08:00
if(key == 27)
{
return 0;
}
if (key == 'k' || key == 'K')
{
ksize = ksize < 30 ? ksize+2 : -1;
}
if (key == 's' || key == 'S')
{
scale++;
}
if (key == 'd' || key == 'D')
{
delta++;
}
if (key == 'r' || key == 'R')
{
scale = 1;
ksize = -1;
delta = 0;
}
}
return 0;
}