줘이리의 인생적기

CV - opencv(Python) Canny Edge 본문

공부/Computer Vision(py)

CV - opencv(Python) Canny Edge

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

Canny Edge에 대해 알아보겠습니다.

-1986년 제안된 알고리즘

-모든 경계선 검출 가능

-경계선 위치 정확히 측정 가능

-구현순서 : 가우시안필터로 노이즈 제거 -> sobel mask를 통한 경계선 검출 -> Non maximum suppression -> Double thresholding

 

Canny Edge

 

threshold1은 약한 경계선에 대한 값

threshold2는 강한 경게선에 대한 값

 

 

코드 원형 설명

def cannyEdge(image, threshold1=100, threshold2=200):
    return cv2.Canny(image, threshold1, threshold2) 

 

코드 설명

더보기
from OpenCV_Functions import *

def nothing(x):
    pass

#이미지 설정
imagePath = "<이미지 경로>"
image = imageRead(imagePath) 
backup = imageCopy(image)

#창 띄우기
cv2.namedWindow('Canny Edge', cv2.WINDOW_GUI_EXPANDED)

#트랙바 설정
cv2.createTrackbar('threshold1', 'Canny Edge', 0, 200, nothing)
cv2.createTrackbar('threshold2', 'Canny Edge', 100, 400, nothing)

switch = '0:OFF\n1:On'
cv2.createTrackbar(switch, 'Canny Edge', 1, 1, nothing)

while True:
    cv2.imshow('Canny Edge', image)

    if cv2.waitKey(1) & 0xFF == 27:
        break
    threshold1 = cv2.getTrackbarPos('threshold1', 'Canny Edge')
    threshold2 = cv2.getTrackbarPos('threshold2', 'Canny Edge')
    s = cv2.getTrackbarPos(switch, 'Canny Edge')
    if s == 1:
        image = cannyEdge(backup, threshold1, threshold2)
    else:
        image = backup

cv2.destroyAllWindows()