Canny门槛的最佳价值

Rub*_*bic 4 opencv edge-detection

我有一个图像,我想检测它的边缘.我发现Canny已经被使用了很多(我不知道我是否有更好的选择).我将值设置如下:

  Imgproc.Canny(img, img, 10, 100, 3,true)
Run Code Online (Sandbox Code Playgroud)

我已经更改了阈值,但没有看到我的图像有太大的变化.任何人都可以向我解释是否有合理的方法来计算出阈值的数字(我的图像是灰度)

谢谢...

Sam*_*mer 5

我认为这应该是个案,如果你发布一些样本图像会有用,但我会尽力回答.这是来自Opencv Documents

Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
where the arguments are:

detected_edges: Source image, grayscale
detected_edges: Output of the detector (can be the same as the input)
lowThreshold: The value entered by the user moving the Trackbar
highThreshold: Set in the program as three times the lower threshold (following Canny’s recommendation)
kernel_size: We defined it to be 3 (the size of the Sobel kernel to be used internally)
Run Code Online (Sandbox Code Playgroud)

通常对我有用的是 highThreshold = 255 and lowThreshold = 255/3

  • 255 不是像素颜色渐变的最大值吗? (2认同)

Mat*_*mas 5

正如萨默所说,这可能要视具体情况而定。下面是一些在 opencv 中使用轨迹栏的代码,并在原始图像旁边显示 canny 图像,以便快速试验不同的阈值。

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()
Run Code Online (Sandbox Code Playgroud)