https://www.cnblogs.com/german-iris/p/4840647.html

Laplace算子和Sobel算子一样,属于空间锐化滤波操作。起本质与前面的Spatial Filter操作大同小异,下面就通过Laplace算子来介绍一下空间锐化滤波,并对OpenCV中提供的Laplacian函数进行一些说明。

  • 数学原理

离散函数导数

离散函数的导数退化成了差分,一维一阶差分公式和二阶差分公式分别为,

CodeCogsEqn

CodeCogsEqn(2)

Laplace算子的差分形式

分别对Laplace算子x,y两个方向的二阶导数进行差分就得到了离散函数的Laplace算子。

在一个二维函数f(x,y)中,x,y两个方向的二阶差分分别为,

CodeCogsEqn(3)

CodeCogsEqn(4)

所以Laplace算子的差分形式为,

CodeCogsEqn(5)

写成filter mask的形式如下,

0 1 0
1 -4 1
0 1 0
注意该mask的特点,mask在上下左右四个90度的方向上结果相同,也就是说在90度方向上无方向性。为了让该mask在45度的方向上也具有该性质,对该filter mask进行扩展定义为,
1 1 1
1 -8 1
1 1 1
 

注:

有时我们也会见到不同于上述结果的Laplace算子的filter mask,

0 -1 0
-1 4 -1
0 -1 0
 
-1 -1 -1
-1 8 -1
-1 -1 -1

其原因是在定义二阶导数的时候采用了相反的定义,这个无关紧要,但是要注意,当用Laplace算子滤波后的图像与原图叠加时,混合操作是加还是减因上述的定义而异。

图像的Laplace操作

如同本文开始时说的那样,将Laplace算子写成filter mask后,其操作大同小异于其他的空间滤波操作。将filter mask在原图上逐行移动,然后mask中数值与其重合的像素相乘后求和,赋给与mask中心重合的像素,对图像的第一,和最后的行和列无法做上述操作的像素赋值零,就得到了拉普拉斯操作结果。

拉普拉斯操作结果与原图的混合

因为Laplace算子是二阶导数操作,其在强调图像素中灰度不连续的部分的同时也不在强调灰度值连续的部分。这样会产生一个具有很明显的灰度边界,但是没有足够特征的黑色背景。背景特征可以通过原图像与Laplace算子操作后的图像混合恢复。用公式,

CodeCogsEqn(6)

其中的参数c的取值和上面的两种mask定义有关,当mask中心的数值取正时c=-1,相反c=1;

  • 基于OpenCV的Laplace算子的计算

OpenCV中Laplacian函数可以实现对图像的Laplace操作,具体用法如下,

  1. Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );

参数意义为,

  1. src_gray,输入图像
  2. dst,Laplace操作结果
  3. ddepth,输出图像深度,因为输入图像一般为CV_8U,为了避免数据溢出,输出图像深度应该设置为CV_16S
  4. kernel_size,filter mask的规模,我们的mask时3×3的,所以这里应该设置为3
  5. scale,delta,BORDER_DEFAULT,默认设置就好

基于OpenCV的Laplace算子仿真代码段如下,

复制代码
  1. //load the Original Image and get some informations
  2. Mat src = imread("012.jpg",0);
  3. namedWindow("OriginalImage");
  4. imshow("OriginalImage",src);
  5. CV_Assert(src.depth() == CV_8U);
  6.  
  7. //OpenCV solution - Laplacian
  8. Mat dst,abs_dst_laplace;
  9. Laplacian(src,dst,CV_16S,3);
  10. convertScaleAbs(dst,abs_dst_laplace);
  11.  
  12. //show the result
  13. namedWindow("result_laplacian");
  14. imshow("result_laplacian",abs_dst_laplace);
复制代码

其中convertScaleAbs函数功能是将CV_16S型的输出图像转变成CV_8U型的图像。

仿真结果:

原图:

original

Laplace操作结果:

abs_dst_laplae

  • 基于mask operation原理仿真

Laplace算子滤波仿真

根据数学原理中介绍的算法,编写相应代码,进行相关仿真。其中对Laplace操作结果进行了图像拉伸显示,因为Laplace操作结果的像素值范围可能落在了[0,255]之外,而计算机在显示的时候将赋值全部置为0,大于255的像素全部显示成255。

代码段如下,

复制代码
  1. //get some informations of original image
  2. int nr = src.rows;
  3. int nc = src.cols;
  4. int n = nr*nc;
  5. int arr[9] = {0};
  6.  
  7. //scan the whole pixels of original image
  8. //and do Laplacian Operation
  9. int* table_lap = new int[n];
  10. int* table_orig = new int[n];
  11. int l;
  12. for (int i=0;i<n;i++)
  13. {
  14. table_lap[i] = 0;
  15. table_orig[i] = 0;
  16. }
  17. for (int i=1;i<nr-1;i++)
  18. {
  19. const uchar* previous = src.ptr<uchar>(i-1);
  20. const uchar* current = src.ptr<uchar>(i);
  21. const uchar* next = src.ptr<uchar>(i+1);
  22. for (int j=1;j<nc-1;j++)
  23. {
  24. for (int k=0;k<3;k++)
  25. {
  26. arr[k] = previous[j+k-1];
  27. arr[k+3] = current[j+k-1];
  28. arr[k+6] = next[j+k-1];
  29. }
  30. l = nc*i+j; //calculate the location in the table of current pixel
  31. Lmaskoperation(table_lap,arr,l);
  32. table_orig[l] = arr[4];
  33. }
  34. }
  35.  
  36. //pixels scale
  37. uchar* La_scaled = new uchar[n];
  38. table_scale(table_lap,La_scaled,n);
  39.  
  40. //padding values
  41. Mat LaResult_own;
  42. LaResult_own.create(src.size(),src.type());
  43. uchar* p = NULL;
  44. for (int i=0;i<nr;i++)
  45. {
  46. p = LaResult_own.ptr<uchar>(i);
  47. for (int j=0;j<nc;j++)
  48. {
  49. l = nc*i+j;
  50. p[j] = La_scaled[l];
  51. }
  52. }
  53.  
  54. //show results
  55. namedWindow("LaResult_own");
  56. imshow("LaResult_own",LaResult_own);
复制代码

其中Lmaskoperation是我写的mask为Laplace mask的mask operation操作函数,函数段如下,

复制代码
  1. //**********************//
  2. //Laplacian mask operation
  3. //**********************//
  4. void Lmaskoperation(int* table,int* arr,int l)
  5. {
  6. int tmp[9] = {-1,-1,-1,-1,8,-1,-1,-1,-1};
  7. for (int i=0;i<9;i++)
  8. {
  9. table[l] = table[l] + tmp[i]*arr[i];
  10. }
  11. }
复制代码

tabel_scale函数就是我写的图像拉伸函数,将Laplace操作结果拉伸到[0,255],具体函数段如下,

复制代码
  1. //*****************************//
  2. //scale the pixels to [0 255]
  3. //*****************************//
  4. void table_scale(int* table,uchar* result,int n)
  5. {
  6. int min = table[0];
  7. int max = table[0];
  8. for (int i=0;i<n;i++)
  9. {
  10. if(min>table[i])
  11. {
  12. min = table[i];
  13. }
  14. if(max<table[i])
  15. {
  16. max = table[i];
  17. }
  18. }
  19. for (int i=0;i<n;i++)
  20. {
  21. result[i] = (uchar)(255*(table[i]-min)/(max-min));
  22. }
  23. }
复制代码

仿真结果,拉伸后Laplace算子的操作结果

LaResult_own

以灰色为主色调的显示结果就是Laplace算子操作拉伸后显示的一大特点。

Laplace滤波图像与原图像的混合

我使用的mask中心值为正,所以混合操作需要原图减去Laplace滤波图像,代码段如下,

复制代码
  1. //blending with the original image using Eq g(x,y)=f(x,y)+c*Lap(x,y)
  2. int* table_blend = new int[n];
  3. for(int i=0;i<n;i++)
  4. {
  5. table_blend[i] = table_orig[i] - table_lap[i];
  6. if(table_blend[i]<0)
  7. {
  8. table_blend[i] = 0;
  9. }
  10. else if (table_blend[i]>255)
  11. {
  12. table_blend[i] = 255;
  13. }
  14. }
  15.  
  16. //padding values to blending result
  17. Mat Blresult;
  18. Blresult.create(src.size(),src.type());
  19. for (int i=0;i<nr;i++)
  20. {
  21. p = Blresult.ptr<uchar>(i);
  22. for(int j=0;j<nc;j++)
  23. {
  24. l = nc*i+j;
  25. p[j] = table_blend[l];
  26. }
  27. }
  28.  
  29. //show blending result
  30. namedWindow("blending result_laplacian");
  31. imshow("blending result_laplacian",Blresult);
复制代码

仿真结果:

blending result_laplacian

 

版权声明:本文为jukan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jukan/p/7866753.html