Laplacian 算子
简单了解基于图像导数的高通线性滤波器:
L
a
p
l
a
c
e
(
f
)
=
∂
2
f
∂
x
2
+
∂
2
f
∂
y
2
Laplace(f)=\frac{\partial^2f}{\partial x^2}+\frac{\partial^2f}{\partial y^2}
Laplace(f)=∂x2∂2f+∂y2∂2f
其出发点在于图像信息一阶导极值位置为二阶导为0位置,利用这个点作为检测图像边缘方法。
这个方程离散化放到图像中记为:
∇
2
f
=
[
f
(
x
+
1
,
y
)
+
f
(
x
−
1
,
y
)
+
f
(
x
,
y
+
1
)
,
f
(
x
,
y
−
1
)
]
−
4
f
(
x
,
y
)
\nabla^2 f= [f(x+1,y)+f(x-1,y)+f(x,y+1),f(x,y-1)]-4f(x,y)
∇2f=[f(x+1,y)+f(x−1,y)+f(x,y+1),f(x,y−1)]−4f(x,y)
即滑动核应该是:
0 | 1 | 0 |
---|---|---|
1 | -4 | 1 |
0 | 1 | 0 |
当然也有其他版本:[OpenCV边缘检测算子原理总结及实现 | 易学教程 (e-learn.cn)](https://www.e-learn.cn/topic/1764407#:~:text=OpenCV边缘检测算子原理总结及实现. 1. 拉普拉斯算子.,原理:是一种基于图像导数运算的高通线性滤波器。. 它通过二阶导数来度量图像函数的曲率。. 拉普拉斯算子是最简单的各向同性微分算子,它具有旋转不变性。. 一个二维图像函数的拉普拉斯变换是各向同性的二阶导数,定义为:.)
API
CV_EXPORTS_W void Laplacian( InputArray src, OutputArray dst, int ddepth,
int ksize = 1, double scale = 1, double delta = 0,
int borderType = BORDER_DEFAULT );
- src 输入图像
- dst 输出图像
- ddepth 图像深度
- ksize 核大小
- scale 缩放因子
GaussianBlur的必要性
Laplacian算子由于其对二阶导的依赖导致图像中出现极端噪点可能对滤波结果造成较大影响(因为椒盐噪声这种像素值突变的情况也可能导致二阶导为0),所以为了避免被噪声干扰,一般Laplacian使用前需要用高斯模糊降噪。
代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat src, dst;
src = imread("klay2_pyrdown.jpg");
if (!src.data)
{
printf("Wrong with Loading img!\n");
return -1;
}
Mat src_gray,src_gaussian;
GaussianBlur(src, src_gaussian, Size(3, 3), 0, 0);
cvtColor(src_gaussian, src_gray, COLOR_BGR2GRAY);
Laplacian(src_gray, dst, CV_16S, 3);
/*Laplacian(src, dst1, CV_16S, 3);
Laplacian(src_gray, dst2, CV_16S, 3);*/
convertScaleAbs(dst,dst);
imshow("src", src);
imshow("dst", dst);
threshold(dst, dst, 0, 255, THRESH_OTSU | THRESH_BINARY);
imshow("threshold", dst);
waitKey(0);
return -1;
}