import cv2 as cv def on_change(val): pass if __name__ == '__main__': windowName = 'Canny edge detector' cv.namedWindow(windowName, cv.WINDOW_NORMAL) image = cv.imread("../img/lena.png", cv.IMREAD_COLOR) kernelSizes = [(1, 1), (3, 3), (9, 9), (15, 15)] blur_t = 'Blur' low_threshold_t = 'Low Threshold' high_threshold_t = 'High Threshold' aperture_size_t = 'Aperture Size' cv.createTrackbar(blur_t, windowName, 0, 3, on_change) cv.createTrackbar(low_threshold_t, windowName, 0, 300, on_change) cv.createTrackbar(high_threshold_t, windowName, 0, 300, on_change) cv.createTrackbar(aperture_size_t, windowName, 3, 30, on_change) cv.imshow(windowName, image) while 1: img = image.copy() k = cv.waitKey(1) & 0xFF if k == 27: break blur = cv.getTrackbarPos(blur_t, windowName) low_threshold = cv.getTrackbarPos(low_threshold_t, windowName) high_threshold = cv.getTrackbarPos(high_threshold_t, windowName) aperture_size = cv.getTrackbarPos(aperture_size_t, windowName) lena_blurred = cv.GaussianBlur(img, kernelSizes[blur], 0, 0) edges = cv.Canny(lena_blurred, low_threshold, high_threshold, aperture_size) cv.imshow(windowName, edges) cv.destroyAllWindows()