多张图像的边缘检测

问题描述 投票:1回答:1

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

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

import glob
import cv2

images = [cv2.imread(file,0) for file in glob.glob("images/*.jpg")]
edges = cv2.Canny(images,100,200)
python opencv image-processing edge-detection
1个回答
0
投票

要选择cv2.Canny()的最大值和最小值,您可以使用Adrian Rosebrock在他的博客auto_canny()中创建的Zero-parameter, automatic Canny edge detection with Python and OpenCV函数。想法是计算图像中像素强度的中值,然后采用该中值确定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
© www.soinside.com 2019 - 2024. All rights reserved.