▪︎ 경계선 검출
- 경계선(edge) : 이미지에서 색이 갑자기 바뀌는 지점을 연결한 것(경계)
cv2.Canny(img, threshold1, threshold2)
threshold1 : 약한 경계 임계값
threshold2 : 강한 경계 임계값
▫︎ 경계선 검출 과정
- 이미지를 그레이 스케일로 변환
- 블러 처리 등을 통해 노이즈를 제거
- Canny 알고리즘을 적용
▫︎ 기본 구현
img = cv.imread(MOUNTAIN_PATH, cv.IMREAD_GRAYSCALE)
canny = cv.Canny(img, 50, 150)
cv.imshow("img", img)
cv.imshow("canny", canny)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
▫︎ 트랙바를 이용한 구현
img = cv.imread(MOUNTAIN_PATH, cv.IMREAD_GRAYSCALE)
name = "Canny"
cv.namedWindow(name)
cv.createTrackbar("threshold1", name, 50, 255, lambda x:x)
cv.createTrackbar("threshold2", name, 150, 255, lambda x:x)
while True:
threshold1 = cv.getTrackbarPos("threshold1", name)
threshold2 = cv.getTrackbarPos("threshold2", name)
canny = cv.Canny(img, threshold1, threshold2)
cv.imshow(name, canny)
if cv.waitKey(1) == ord("q"):
break
cv.destroyAllWindows()
cv.waitKey(1)
▫︎ 캠 화면에서 구현
cap = cv.VideoCapture(0)
name = "Canny"
cv.namedWindow(name)
cv.createTrackbar("threshold1", name, 50, 255, lambda x:x)
cv.createTrackbar("threshold2", name, 150, 255, lambda x:x)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
threshold1 = cv.getTrackbarPos("threshold1", name)
threshold2 = cv.getTrackbarPos("threshold2", name)
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
canny = cv.Canny(gray, threshold1, threshold2)
cv.imshow(name, canny)
if cv.waitKey(1) == ord("q"):
break
cap.release()
cv.destroyAllWindows()
cv.waitKey(1)
▫︎ 중첩으로 블러 사용
- Canny 알고리즘에 이미 가우시안 블러가 적용되어있음
- 추가적으로 노이즈 제거 필요 시 중첩으로 블러 적용
img = cv.imread("../images/dog.jpg", cv.IMREAD_GRAYSCALE)
blur = cv.GaussianBlur(img, (5,5), 0)
canny = cv.Canny(img, 50, 150)
canny_blur = cv.Canny(blur, 50, 150)
cv.imshow("img", canny)
cv.imshow("blur", canny_blur)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
▪︎ 윤곽선 검출