掌握

1、Laplace算子理论(通过Laplace卷积核求取图像边缘);
2、API使用;
3、代码演示;

Laplace理论

1、在图像像素二阶导数中,图像像素变化最大处的值为0,即边缘是零值,因此,理论上我们可以通过计算图像二阶导数,提取图像边缘;

2、数学原理:二阶导求导公式:

相关API

1、OpenCV中提供的函数:cv::Laplacian()(不再分x方向,y方向)
注:depth—CV_16S; ksize–3(3阶卷积算子)

Laplace运算流程

  1. 高斯模糊降噪处理;
  2. 转换为灰度图像;
  3. 拉普拉斯计算;
  4. 取绝对值(负的值也是边缘有效信息);
  5. 显示结果;

Code

代码注意:Laplace受干扰影响大,Laplace计算完后可以使用threshold()阈值操作API再滤除下干扰;

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
   
	Mat src,dst;
	src = imread("C:\\Users\\hello\\Desktop\\lena512color.tiff");
	if (!src.data)
	{
   
		cout << "could not load the image..." << endl;
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	//Laplace运算
	//Laplace的噪声比较明显 , 处理完成后可以再用图像阈值处理API处理一下
	Mat g1,grayimage ;
	GaussianBlur(src, g1, Size(3, 3), 0, 0);
	cvtColor(g1, grayimage, CV_BGR2GRAY);

	Laplacian(grayimage, dst, CV_16S, 3);  //Size大小为3

	convertScaleAbs(dst, dst);  //转换为正值
	threshold(dst, dst, 0, 255, THRESH_OTSU | THRESH_BINARY);  //图像二值化


	imshow("Laplace Image", dst);

	waitKey(0);
	return 0;
}

效果

注:threshold阈值处理,将图像转换为二值图像,轮廓效果显示更好。


Lena: