根据图像处理的原理,二阶导数可以用来进行边缘检测,因为图像是二维的,需要在两个方向上求导,使用Laplacian算子将会使求导过程变得简单。
Laplacian算子的定义:
需要说明的是,由于Laplacian算子使用了图像梯度,它内部的代码其实是调用了Sobel算子的。
让一幅图像减去它的Laplacian算子可以增强它的对比度。
Laplacian()函数函数原型:
void Laplacian(InputArray src, outputArray dst, int ddepth, int ksize=1, double scale=1, doubel delta=0, int borderType=BORDER_DEFAULT) 第一个参数:输入图像,Mat类的对象即可,需为单通道的8位图像。第二个参数:输出的边缘图,需要和输入图像有一样的尺寸和通道数。第三个参数:int类型的ddepth,目标图像的深度。第四个参数:int类型的ksize,用于计算二阶导数的滤波器的核的大小,必须是正奇数,默认值是1.第五个参数:double类型的scale,计算拉普拉斯值的时候可选的比例因子,默认值是1.第六个参数:double类型的delta,表示结果存入目标图之前可选的delta值,默认值是0.第七个参数:int类型borderType。Laplacian()函数其实主要利用sobel算子进行运算,通过sobel算子得到图像x方向和y方向的导数,然后得到拉普拉斯变换的结果。
计算过程为:
当ksize=1时,Laplacian()函数采用3*3的核。
代码示例:
#include <iostream>#include <opencv2/opencv.hpp>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main(){ Mat srcImage; srcImage = imread("/Users/dwz/Desktop/cpp/1.jpg"); GaussianBlur(srcImage, srcImage, Size(3, 3), 0, 0); Mat grayImage; cvtColor(srcImage, grayImage, COLOR_BGR2GRAY); Mat dst, abs_dst; Laplacian(grayImage, dst, CV_16S, 3, 1, 0, BORDER_DEFAULT); convertScaleAbs(dst, abs_dst); imwrite("laplacian.jpg", abs_dst);}输入:
输出: