Opencv (C++) learning series---Laplacian Laplacian edge detection algorithm

【1】Algorithm introduction

       The Laplacian operator is a second-order derivative operator that has rotation invariance and can meet the requirements of image edge sharpening (edge ​​detection) in different directions . Normally, the sum of the coefficients of its operators needs to be zero. The Laplacian operator has the characteristics of isotropy in all directions and can extract edges in any direction. It has the advantage of being non-directional. Therefore, using the Laplacian operator to extract edges does not require separately detecting edges in the X direction and edges in the Y direction. It only needs one time. Just edge detection. The Laplacian operator is a second-order derivative operator that is sensitive to noise, so it often needs to be used together with Gaussian filtering.
        d3378c7ba1064b49981e661b1cbf2fa5.png 

         It should be noted that in the above image, the value of the calculation result may be positive or negative. Therefore, it is necessary to take the absolute value of the calculation result to ensure that subsequent calculations and displays are correct.

41f754b2f4ca4a25a6dc3dfd3a7a16d3.png

【2】Algorithm parameters

void Laplacian( InputArray src, OutputArray dst, int ddepth,
                             int ksize = 1, double scale = 1, double delta = 0,
                             int borderType = BORDER_DEFAULT );

src: Input the original image, which can be a grayscale image or a color image.
dst: output image, which has the same size and number of channels as the input image src.
ddepth: the data type (depth) of the output image. It has different value ranges according to the data type of the input image. The specific value range is in Table 5- 1 is given, and when assigned a value of -1, the data type of the output image is automatically selected.
ksize: The size of the filter, must be a positive odd number.
scale: The scaling factor used to scale the derivative calculation results. The default coefficient is 1, which means no scaling.
delta: offset value, add the offset value to the calculation result.
borderType: pixel extrapolation method selection flag, the default parameter is BORDER_DEFAULT, which means that the border value is not included in reverse filling.

【3】Complete code

#include<opencv2\opencv.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	//载入原始图
	Mat src = imread("E:\\乔大花进度\\11-28\\边缘检测\\4.jpg");
	//【1】定义变量
	Mat src_gray, dst, abs_dst;
	//【2】显示原图
	imshow("原始图",src);
	//【3】使用高斯滤波消除噪声
	GaussianBlur(src,src,Size(3,3),0,0,BORDER_DEFAULT);
	//【4】转为灰度图
	cvtColor(src,src_gray,COLOR_BGR2GRAY);
	//【5】Laplacian查找边缘
	Laplacian(src_gray,dst,CV_16S,3,1,0);

	//【6】计算绝对值,并将结果转为8位
	convertScaleAbs(dst,abs_dst);

	//【7】显示效果图
	imshow("Laplacian变换",abs_dst);
	cout << "Laplacian算法输出图像的通道" << abs_dst.channels() << endl;

	waitKey(0);
	system("pause");
	destroyAllWindows();

	return 0;

}

operation result:
31488877edcb449396658860942de52b.png

Guess you like

Origin blog.csdn.net/qiaodahua/article/details/128083612