原理:

上一篇文章介绍了 Sobel 算子 ,其基础来自于一个事实,即在边缘部分,像素值出现”跳跃“或者较大的变化。如果在此边缘部分求取一阶导数,会看到极值的出现。如下图所示:

 示例程序031--Laplace算子

如果在边缘部分求二阶导数的话:

示例程序031--Laplace算子

会发现在一阶导数的极值位置,二阶导数为0。所以我们也可以用这个特点来作为检测图像边缘的方法。 但是, 二阶导数的0值不仅仅出现在边缘(它们也可能出现在无意义的位置),但是我们可以过滤掉这些点。

 

Laplacian算子:

从以上分析中,我们推论二阶导数可以用来 检测边缘 。 因为图像是 “2维”, 我们需要在两个方向求导。使用Laplacian算子将会使求导过程变得简单。

Laplacian 算子 的定义:

示例程序031--Laplace算子 

OpenCV函数 Laplacian 实现了Laplacian算子。 实际上,由于 Laplacian使用了图像梯度,它内部调用了Sobel 算子。

 

用到的函数:

    Laplacian();

 

代码及注释:

// 038 Lapalce算子.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;


int main( int argc, char** argv )
{
  Mat src, src_gray, dst;
  int kernel_size = 3;
  int scale = 1;
  int delta = 0;
  int ddepth = CV_16S;
  char* window_name = "Laplace Demo";

 

   src = imread( "Lena.jpg" );

  if( !src.data )
    { return -1; }

 

  /// 使用高斯滤波消除噪声
  GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );

 

  /// 转换为灰度图
  cvtColor( src, src_gray, CV_RGB2GRAY );

  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

 

  Mat abs_dst;
  /// 使用Laplace函数
  Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );

 

  //将输出图像的深度转化为 CV_8U :
  convertScaleAbs( dst, abs_dst );

 

  imshow( window_name, abs_dst );

  waitKey(0);

  return 0;
  }

运行结果:

示例程序031--Laplace算子

相关文章:

  • 2021-05-28
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-05-09
  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
猜你喜欢
  • 2021-08-30
  • 2022-12-23
  • 2021-04-19
  • 2021-07-22
  • 2022-12-23
  • 2021-11-24
  • 2022-02-23
相关资源
相似解决方案