多幅图像的边缘检测

Edge detection for multiple images

您好,我有一组图像,我想一次对所有图像进行边缘检测。我可以为每个图像手动执行此操作,但我认为这不是正确的方法。我怎样才能一次处理所有图像?我认为它应该在一个循环中,但我不知道如何实现它

我已经读取了多张灰度图像,现在我想对所有图像进行边缘检测。我们如何 select Canny 函数的最大值和最小值参数。图片可以访问here

import glob
import cv2

images = [cv2.imread(file,0) for file in glob.glob("images/*.jpg")]
edges = cv2.Canny(images,100,200)

要自动 select cv2.Canny() 的最大值和最小值,您可以使用 Adrian Rosebrock 在他的博客 Zero-parameter, automatic Canny edge detection with Python and OpenCV 中创建的 auto_canny() 函数。这个想法是计算图像中像素强度的中值,然后取这个中值来确定 lowerupper 阈值。有关更详细的解释,请查看他的博客。这是函数

def auto_canny(image, sigma=0.33):
    # Compute the median of the single channel pixel intensities
    v = np.median(image)

    # Apply automatic Canny edge detection using the computed median
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    return cv2.Canny(image, lower, upper)

要对多张图像执行边缘检测,您可以使用 glob 库遍历每张图像,应用 canny 边缘检测,然后保存图像。这是结果

import cv2
import numpy as np
import glob

def auto_canny(image, sigma=0.33):
    # Compute the median of the single channel pixel intensities
    v = np.median(image)

    # Apply automatic Canny edge detection using the computed median
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    return cv2.Canny(image, lower, upper)

# Read in each image and convert to grayscale
images = [cv2.imread(file,0) for file in glob.glob("images/*.jpg")]

# Iterate through each image, perform edge detection, and save image
number = 0
for image in images:
    canny = auto_canny(image)
    cv2.imwrite('canny_{}.png'.format(number), canny)
    number += 1