줘이리의 인생적기

CV - opencv(Python) 경계선 검출(edge detection) 본문

공부/Computer Vision(py)

CV - opencv(Python) 경계선 검출(edge detection)

줘이리 2020. 7. 20. 08:00
728x90

경계선 검출은 영상의 밝기가 낮은 값에서 높은 값으로, 또는 반대로 변하는 지점을 검출하는 과정이다.

객체의 경계를 검출함으로써 모양(shape), 방향(direction)을 탐지할 수 있다.

 

첫 번째로 1차 미분(prewitt, sobel, scharr) 방법을 통해 명암, 밝기 변화율을 검출한 후,

2차 미분(Laplacian) 방법을 이용하여 더욱 민감하게 검출한다.

 

prewitt 연산자 - 수평이나 수직 경계선에 민감

 

sobel 연산자 - 대각선 경계선에 민감

 

scharr 연산자 - sobel 보다 민감

 

Laplacian 연산자 - 모든 방향의 경계선을 강조

 

참고 사이트 : https://iskim3068.tistory.com/49

 

윤곽선 검출(Edge Detection) 에지 디텍션

영상에서 윤곽선 검출(Edge Detection)이 의미하는 것이 무엇인지 알아보자 1. 윤곽선&윤곽선 검출(Edge & Edge Detection ) Edge는 경계선, 윤곽선을 의미한다. 영상에서의 edge란 영상의 밝기가 낮은 값에서

iskim3068.tistory.com

 

 

1번 기본적인 prewitt, sobel, scharr, laplacian

 

2번 여러 laplacian (아래 코드를 통해 Laplacian 행렬 확인)

 

 

 

코드 원형 설명

#prewitt 연산자
def imageEdgePrewitt(image):
    kernel_x = np.array([[-1, 0, 1],
                         [-1, 0, 1],
                         [-1, 0, 1]], np.float32)
    kernel_y = np.array([[-1, -1, -1],
                         [0, 0, 0],
                         [1, 1, 1]], np.float32)
    img_dx = imageFiltering(image, kernel_x)
    img_dy = imageFiltering(image, kernel_y)
    return img_dx + img_dy

#sobel 연산자
def imageEdgeSobel(image):
    img_dx = cv2.Sobel(image, -1, 1, 0, ksize=3)
    img_dy = cv2.Sobel(image, -1, 0, 1, ksize=3)
    return img_dx + img_dy

#scharr 연산자
def imageEdgeScharr(image):
    img_dx = cv2.Scharr(image, -1, 1, 0)
    img_dy = cv2.Scharr(image, -1, 0, 1)
    return img_dx + img_dy

#laplacian 연산자
def imageEdgeLaplacianCV(image):
    return cv2.Laplacian(image, -1)

 

1번, 2번 코드 설명

더보기
from OpenCV_Functions import *

#1번 그림 코드
#이미지 설정
imagePath = "<이미지 경로>"
image = imageRead(imagePath) 
imageShow("image", image)

#prewitt 연산자
image_Prewitt = imageEdgePrewitt(image)
imageShow("Prewitt", image_Prewitt)

#sobel 연산자
image_Sobel = imageEdgeSobel(image)
imageShow("Sobel", image_Sobel)

#scharr 연산자
image_Scharr = imageEdgeScharr(image)
imageShow("Scharr", image_Scharr)

#laplacian 연산자
image_LaplacianCV = imageEdgeLaplacianCV(image)
imageShow("LaplacianCV", image_LaplacianCV)

#2번 그림 코드
#여러 laplacian 연산자
laplacian_m4_kernel = np.array([
                                [0, 1, 0],
                                [1, -4, 1],
                                [0, 1, 0]], np.float32)

image_LaplacianFilter_m4 = imageFiltering(image, laplacian_m4_kernel)
imageShow("LaplacianFilter_m4", image_LaplacianFilter_m4)

laplacian_4_kernel = np.array([
                                [0, -1, 0],
                                [-1, 4, -1],
                                [0, -1, 0]], np.float32)

image_LaplacianFilter_4 = imageFiltering(image, laplacian_4_kernel)
imageShow("LaplacianFilter_4", image_LaplacianFilter_4)

laplacian_m8_kernel = np.array([
                                [1, 1, 1],
                                [1, -8, 1],
                                [1, 1, 1]], np.float32)

image_LaplacianFilter_m8 = imageFiltering(image, laplacian_m8_kernel)
imageShow("LaplacianFilter_m8", image_LaplacianFilter_m8)

laplacian_8_kernel = np.array([
                                [-1, -1, -1],
                                [-1, 8, -1],
                                [-1, -1, -1]], np.float32)

image_LaplacianFilter_8 = imageFiltering(image, laplacian_4_kernel)
imageShow("LaplacianFilter_8", image_LaplacianFilter_8)

cv2.destroyAllWindows()