Image Processing12(Laplace Operator )

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function Laplacian() to implement a discrete analog of the Laplacian operator.
  • 学习使用OpenCV的库函数:Laplacian()进行离散模拟拉普拉斯算子。

Theory

  1. In the previous tutorial we learned how to use the Sobel Operator. It was based on the fact that in the edge area, the pixel intensity shows a "jump" or a high variation of intensity. Getting the first derivative of the intensity, we observed that an edge is characterized by a maximum, as it can be seen in the figure:

  2. 在前面的教程中,学习了使用Sobel算子。它是根据这个事实:在图像的边缘部分,图像的像素强度会发生较大的变化。计算像素的一阶导数,可以观察到边缘处的特征值是最大的,就像下图所示:

                                          Laplace_Operator_Tutorial_Theory_Previous.jpg
  3. And...what happens if we take the second derivative?

  4. 但是,当我们想要计算二阶导数怎么办?

                                                                    Laplace_Operator_Tutorial_Theory_ddIntensity.jpg

    You can observe that the second derivative is zero! So, we can also use this criterion to attempt to detect edges in an image. However, note that zeros will not only appear in edges (they can actually appear in other meaningless locations); this can be solved by applying filtering where needed.

  5. 可以观察到,二阶导数为零!(这个是很显然的高中数学!)因此,我们这里可以使用这个准则尝试着检测图像的边缘。当然,零不仅仅会出现在图像的边缘部分(它们也可能会出现在其他没有任何的地方--数学上教过这些),这个可以通过在需要的地方进行滤波解决(?)

Laplacian Operator

  1. From the explanation above, we deduce that the second derivative can be used to detect edges. Since images are "*2D*", we would need to take the derivative in both dimensions. Here, the Laplacian operator comes handy.
  2. 从上面的解释可以看出,我们可以推断出二阶的导数可以被用来检测图像的边缘部分。由于图像是2维的,因此,我们需要计算两个维度的导数。
  3. The Laplacian operator is defined by:

  4. 拉普拉斯算子定义如下:

    Laplace(f)=2fx2+2fy2
  5. The Laplacian operator is implemented in OpenCV by the function Laplacian() . In fact, since the Laplacian uses the gradient of images, it calls internally the Sobel operator to perform its computation.
  6. 拉普拉斯算子在OpenCV中的函数是:Laplacian()。由于拉普拉斯算子也用到图像的梯度,因而,事实上,它在内部调用了Sobel算子。

Code

  1. What does this program do?
    • Loads an image(加载图像)
    • Remove noise by applying a Gaussian blur and then convert the original image to grayscale
    • 高斯平滑去噪声,然后转化为灰度图
    • Applies a Laplacian operator to the grayscale image and stores the output image
    • 使用Laplacian()算子
    • Display the result in a window(展示结果,在窗口中)

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
int main( int argc, char** argv )
{
    // Declare the variables we are going to use
    Mat src, src_gray, dst;
    int kernel_size = 3;
    int scale = 1;
    int delta = 0;
    int ddepth = CV_16S;
    const char* window_name = "Laplace Demo";
    const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
    src = imread( imageName, IMREAD_COLOR ); // Load an image
    // Check if image is loaded fine
    if(src.empty()){
        printf(" Error opening image\n");
        printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
        return -1;
    }
    // Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
    GaussianBlur( src, src, Size(3, 3), 0, 0, BORDER_DEFAULT );//输入,输出,大小,X标准差,Y标准差,边界类型
    cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
    Mat abs_dst;
    Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );//输入,输出,深度,内核大小,比例因子,加到算子结果的偏差,边界类型
    // converting back to CV_8U
    convertScaleAbs( dst, abs_dst );//输入,输出,appha,betal
    //在每一个像素上执行下面的操作,dst(I)=saturate\_cast<uchar>(|src(I)∗alpha+beta|)
    imshow( window_name, abs_dst );
    waitKey(0);
    return 0;

}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS "-std=c++11")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )


install(TARGETS DisplayImage RUNTIME DESTINATION bin)

Results


猜你喜欢

转载自blog.csdn.net/qq_27806947/article/details/80297127