将Canny边缘检测转换为np.array



我写了一个函数,我想使用Canny算法检测图像的边缘。然后,我想提取此图像的 2d 数组,然后将其展平为一个 1d 数组。

def canny_detection(image):
    # Convert to grayscale and convert the image to float
    RGB = img_as_float(color.rgb2gray(image))
    # Apply Canny edge detection algorithm
    edge_canny = feature.canny(RGB, 3).astype(int)
    #Get output array
    canny_arr = np.array(edge_canny)
    # Flatten output array
    canny_flat = canny_arr.flatten()
    return canny_flat

但是,当我使用示例图像调用该函数时,输出只是一个巨大的 0 数组。我敢肯定这是不对的。我已经在图像上测试了精明的算法,结果图像是正确的。但问题是当我想获取图像的矢量时。

谁能帮忙?

我怀疑问题可能出在那一行:

edge_canny = feature.canny(RGB, 3).astype(int)

请将其替换为

edge_canny = feature.canny(RGB, 3)
print(edge_canny)
edge_canny = edge_canny.astype(int)
print(edge_canny)

并检查它打印的内容,如果第一个是一些非零浮点值<1.0第二个是 0 s,这可能意味着feature.canny生成从 0.01.0 的值,然后您丢失了它转换为 int .编辑:修复了我的代码。

最新更新