opencv Canny中阈值的最佳值

huwehgph  于 2022-11-30  发布在  其他
关注(0)|答案(3)|浏览(165)

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

Imgproc.Canny(img, img, 10, 100, 3,true)

我已经改变了阈值,但在我的图像中没有看到太大的变化。谁能向我解释一下是否有一个逻辑的方法来计算阈值的数字(我的图像是灰度的)
谢谢你......

b1uwtaje

b1uwtaje1#

我认为这应该采取的情况下,如果你张贴一些样本图像将是有用的,但我会尝试回答无论如何.这里是从OpenCV文档

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)

我通常使用highThreshold = 255 and lowThreshold = 255/3

ev7lccsx

ev7lccsx2#

正如Samer所说,这可以根据具体情况而定。这里有一些代码,它在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()
pod7payv

pod7payv3#

你可以使用这个方程,它是有用的,你可以应用蓝色来增强它。

blurred_img = cv2.blur(img,ksize=(5,5))
med_val = np.median(img) 
lower = int(max(0 ,0.7*median_pix))
upper = int(min(255,1.3*median_pix))
edges = cv2.Canny(image=img, threshold1=lower,threshold2=upper)

相关问题