首页 > 代码库 > OpenCV Tutorials —— Laplace Operator

OpenCV Tutorials —— Laplace Operator

发掘图像边界 —— 一阶导数顶点不好求,可用二阶导数过零点来代替

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:

Previous theory

 

Second derivative

You can observe that the second derivative is zero! So, we can also use this criterion to attempt to detect edges in an image.

 

  1. The Laplacian operator is defined by:

Laplace(f) = \dfrac{\partial^{2} f}{\partial x^{2}} + \dfrac{\partial^{2} f}{\partial y^{2}}

 

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

  • src_gray: The input image.
  • dst: Destination (output) image
  • ddepth: Depth of the destination image. Since our input is CV_8U we define ddepth = CV_16S to avoid overflow
  • kernel_size: The kernel size of the Sobel operator to be applied internally. We use 3 in this example.
  • scale, delta and BORDER_DEFAULT: We leave them as default values.
convertScaleAbs( dst, abs_dst );

Convert the output from the Laplacian operator to a CV_8U image

 

Code

#include "stdafx.h"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <stdlib.h>#include <stdio.h>using namespace cv;/** @function main */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";	int c;	/// Load an image	src = http://www.mamicode.com/imread("img2.jpg");	if( !src.data )	{ return -1; }	/// Remove noise by blurring with a Gaussian filter	GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );	/// Convert the image to grayscale	cvtColor( src, src_gray, CV_RGB2GRAY );	/// Create window	namedWindow( window_name, CV_WINDOW_AUTOSIZE );	/// Apply Laplace function	Mat abs_dst;	Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );	convertScaleAbs( dst, abs_dst );	/// Show what you got	imshow( window_name, abs_dst );	waitKey(0);	return 0;}

OpenCV Tutorials —— Laplace Operator