繁体   English   中英

如何使用 numpy Python 检测图像上的边缘

[英]How to detect edges on an image using numpy Python

我有以下图像:黑色背景中的白色圆环,我想检测它的边缘。 我的代码看起来像这样:

possible_moves = np.array([[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]])

image = np.array(cv2.imread('Example.png'))
image_copy = image.copy()
spots_0 = np.argwhere((image[:, :, 0] == 255) & (image[:, :, 1] == 255) & (image[:, :, 2] == 255))

index = 0
start_time = time.time()

while(True): 
    
    future_Neighbors = np.add(possible_moves, [spots_0[index, 0], spots_0[index, 1]])
    Neighbors_inFrame = (np.argwhere((image[future_Neighbors[:, 0].astype(int), future_Neighbors[:, 1].astype(int), 0] ==  255) & ((image[future_Neighbors[:, 0].astype(int), future_Neighbors[:, 1].astype(int), 1] ==  255)) & ((image[future_Neighbors[:, 0].astype(int), future_Neighbors[:, 1].astype(int), 2] ==  255))))
        
    if len(Neighbors_inFrame) < 8:
        image_copy[future_Neighbors[Neighbors_inFrame[:], 0], future_Neighbors[Neighbors_inFrame[:], 1]] = [0, 0, 255]

    index += 1

    if len(spots_0) == index:  
        break
print(time.time() - start_time)

我的代码的 output 给出以下图像结果

我有以下问题:

1)有没有办法让我的代码或逻辑更快? 处理空洞图像实际上需要 156 毫秒,我需要让它至少快 10 倍,以便我可以在另一个步骤中处理实时图像。

  1. 什么时候可以让它更快。 这可以用 numpy 函数来完成吗?

谢谢

看起来您想要勾勒边缘。

这可以使用形态梯度来完成

# im = cv.imread("image.png", cv.IMREAD_GRAYSCALE)

edgemap = cv.dilate(im, None) - cv.erode(im, None)

# color them red
im[edgemap != 0] = (0,0,255)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM