▪︎ 경계선 검출

▫︎ 경계선 검출 과정

  1. 이미지를 그레이 스케일로 변환
  2. 블러 처리 등을 통해 노이즈를 제거
  3. 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)

▫︎ 중첩으로 블러 사용

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)

▪︎ 윤곽선 검출