1. 程式人生 > >十四天canny邊緣檢測

十四天canny邊緣檢測

import cv2 as cv


def edge_demo(image):
    blurred = cv.GaussianBlur(image, (3, 3), 0)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
    edge_output = cv.Canny(xgrad,ygrad,50,150)        ##    得出彩色邊緣
    cv.imshow("canny edge",edge_output)
    dst = cv.bitwise_and(image, image, mask=edge_output)    ##灰度值與sobel邊緣與
    cv.imshow("Color Edge", dst)
src = cv.imread("C:/Users/weiqiangwen/Desktop/sest/data/lena.jpg")
# cv.namedWindow("input contours",cv.WINDOW_AUTOSIZE)
cv.imshow("contours", src)
edge_demo(src)
cv.waitKey(0)

cv.destroyAllWindows()
print("--------- Python OpenCV Tutorial ---------")