opencv/samples/cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp

68 lines
1.6 KiB
C++
Raw Normal View History

/**
* @file Laplace_Demo.cpp
* @brief Sample code showing how to detect edges using the Laplace operator
* @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"
using namespace cv;
/**
* @function main
*/
2017-07-26 13:39:53 +08:00
int main( int argc, char** argv )
{
2017-08-22 07:17:09 +08:00
//![variables]
// Declare the variables we are going to use
Mat src, src_gray, dst;
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
const char* window_name = "Laplace Demo";
//![variables]
2017-08-22 07:17:09 +08:00
//![load]
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
2017-08-22 07:17:09 +08:00
src = imread( imageName, IMREAD_COLOR ); // Load an image
2017-08-22 07:17:09 +08:00
// Check if image is loaded fine
if(src.empty()){
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
return -1;
}
//![load]
2017-08-22 07:17:09 +08:00
//![reduce_noise]
// Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
GaussianBlur( src, src, Size(3, 3), 0, 0, BORDER_DEFAULT );
//![reduce_noise]
2017-08-22 07:17:09 +08:00
//![convert_to_gray]
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
//![convert_to_gray]
2016-07-18 21:32:05 +08:00
2017-08-22 07:17:09 +08:00
/// Apply Laplace function
Mat abs_dst;
//![laplacian]
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
//![laplacian]
2017-08-22 07:17:09 +08:00
//![convert]
// converting back to CV_8U
convertScaleAbs( dst, abs_dst );
//![convert]
2017-08-22 07:17:09 +08:00
//![display]
imshow( window_name, abs_dst );
waitKey(0);
//![display]
return 0;
}