多张图像的边缘检测

时间:2019-10-10 01:55:22

标签: python opencv image-processing edge-detection

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

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

import glob
import cv2

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

1 个答案:

答案 0 :(得分:0)

要自动选择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边缘检测,然后保存图像。这是结果

enter image description here

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