05-12 11:13
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] Laplace Operator 본문

Programming/OpenCV

[OpenCV] Laplace Operator

cinema4dr12 2015. 7. 25. 20:04

Edge를 검출하기 위한 Operator로써 Sobel, Prewitt, Robert, Scharr 등이 있는데 이들은 모두 1차 미분을 기반으로 하고 있습니다.

2차 미분값을 이용하여서도 Edge를 검출할 수가 있는데 이는 일반적으로 Edge에서 2차 미분값이 0이 되기 때문입니다. 그러나, 2차 미분값이 0이 된다고해서 반드시 Edge라는 보장은 없습니다. Edge가 아닌 곳에서도 2차 미분값이 0이 될 수 있는 가능성이 있기 때문입니다.

Theory

Laplacian Operator는 2차 미분 Operator로써 다음과 같이 정의됩니다:


\( \nabla^2 = \nabla \cdot \nabla = \begin{bmatrix} \displaystyle{ \frac{\partial}{\partial x} } & \displaystyle{ \frac{\partial}{\partial y} } \end{bmatrix} \begin{bmatrix} \displaystyle{\frac{\partial}{\partial x}} \\ \displaystyle{\frac{\partial}{\partial y}} \end{bmatrix} = \displaystyle{\frac{\partial^2}{\partial x^2}} + \displaystyle{\frac{\partial^2}{\partial y^2}} \)


함수 \(f\)에 대한  Laplacian Operator는


\( \nabla^2 f = \displaystyle{ \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2} } \)


입니다.

Implementation

(1)  이미지를 불러옵니다.

(2)  Gaussian Blur를 수행합니다 (노이즈를 제거하기 위함입니다).

(3)  RGB 컬러를 Grayscale로 변환합니다.

(4)  Laplacian 연산을 수행합니다.

(5)  연산된 이미지를 띄웁니다.

cv::Laplacian() 함수에 대한 자세한 설명은 다음 링크를 참합니다:

http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/imgtrans/laplace_operator/laplace_operator.html?highlight=laplacian

Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <opencv2/opencv.hpp>
 
#include <stdlib.h>
#include <stdio.h>
 
using namespace std;
using namespace cv;
 
/* @ function main */
int main( int argc, char** argv )
{
  cv::Mat src, src_gray, dst;
 
  int kernel_size = 3;
  int scale = 1;
  int delta = 0;
  int ddepth = CV_16S;
 
  /// Load an image
  src = cv::imread( {YOUR_IMAGE_PATH}, 1 );
 
  if!src.data ) { return -1; }
 
  /// Remove noise by blurring with a Gaussian filter
  cv::GaussianBlur( src, src, Size(3,3), 00, BORDER_DEFAULT );
 
  /// Convert the image to grayscale
  cv::cvtColor( src, src_gray, COLOR_RGB2GRAY );
 
  /// Apply Laplace function
  cv::Mat abs_dst;
 
  cv::Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
  cv::convertScaleAbs( dst, abs_dst );
 
  /// Create window
  cv::namedWindow( "Origianl Image", WINDOW_AUTOSIZE );
  cv::namedWindow( "Laplacian Image", WINDOW_AUTOSIZE );
 
  /// Show what you got
  cv::imshow( "Origianl Image", src );
  cv::imshow( "Laplacian Image", abs_dst );
 
  cv::waitKey(0);
 
  return 0;
}
cs

Results

Original Image(1)


Laplacian Operator 적용된 이미지(1)

Original Image(2)


Laplacian Operator가 적용된 이미지(2)


Comments