Best value for threshold in Canny

后端 未结 3 1099
一生所求
一生所求 2021-01-02 06:43

I have an image which I want to detect edges on that. I found Canny has been used a lot ( I don\'t know whether I have a better option than that). I have set the values as f

3条回答
  •  一生所求
    2021-01-02 07:26

    As Samer said it could be case by case. Here is some code that uses trackbars in opencv, and displays the canny image next to the original, in order to quickly experiment with different threshold values.

    import cv2
    import numpy as np 
    import matplotlib.pyplot as plt 
    
    def callback(x):
        print(x)
    
    img = cv2.imread('your_image.png', 0) #read image as grayscale
    
    
    canny = cv2.Canny(img, 85, 255) 
    
    cv2.namedWindow('image') # make a window with name 'image'
    cv2.createTrackbar('L', 'image', 0, 255, callback) #lower threshold trackbar for window 'image
    cv2.createTrackbar('U', 'image', 0, 255, callback) #upper threshold trackbar for window 'image
    
    while(1):
        numpy_horizontal_concat = np.concatenate((img, canny), axis=1) # to display image side by side
        cv2.imshow('image', numpy_horizontal_concat)
        k = cv2.waitKey(1) & 0xFF
        if k == 27: #escape key
            break
        l = cv2.getTrackbarPos('L', 'image')
        u = cv2.getTrackbarPos('U', 'image')
    
        canny = cv2.Canny(img, l, u)
    
    cv2.destroyAllWindows()
    

提交回复
热议问题