gpt4 book ai didi

python - 使用细化和命中或遗失实现的骨架化永不停止

转载 作者:行者123 更新时间:2023-12-02 17:26:50 30 4
gpt4 key购买 nike

我正在尝试使用https://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm中所述的稀疏/命中或遗漏方法来实现骨架化算法。

我想提取以下图像的骨架,我认为这是测试的好图像:

https://github.com/TiagoB936/Image-Processing-and-Computer-Vision/blob/master/Skeletonization/x.png

问题是我的代码无法停止运行,而且我没有发现问题。

我的代码执行以下步骤:

  • 读取图像并将其二值化
  • 创建2个主要内核:

    [[0,0,0]

    [-1,1,-1]

    [1,1,1]



    [[-1,0,0]

    [1,1,0]

    [-1,1,1]]
  • 创建另外6个内核,每个内核旋转90度
  • 应用命中或错过创建8个输出图像
  • 添加这些图像并减去输入图像的结果,生成输出图像
  • 验证输出图像是否等于输入图像
  • 输入图像变成输出图像
  • 输入和输出图像之间没有区别时重复

  • 这是一些测试代码:

    import cv2 as cv
    import numpy as np

    input_image = cv.imread('x.png',0)
    _,input_image = cv.threshold(input_image,60,255,cv.THRESH_BINARY)

    kernel1 = np.array((
    [0, 0, 0],
    [-1, 1, -1],
    [1, 1, 1]), dtype="int")
    kernel2 = np.array((
    [-1, 0, 0],
    [1, 1, 0],
    [-1, 1, -1]), dtype="int")
    kernel3 = np.rot90(kernel1)
    kernel4 = np.rot90(np.rot90(kernel1))
    kernel5 = np.rot90(np.rot90(np.rot90(kernel1)))
    kernel6 = np.rot90(kernel2)
    kernel7 = np.rot90(np.rot90(kernel2))
    kernel8 = np.rot90(np.rot90(np.rot90(kernel2)))


    output_image_1 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel1)
    output_image_2 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel2)
    output_image_3 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel3)
    output_image_4 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel4)
    output_image_5 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel5)
    output_image_6 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel6)
    output_image_7 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel7)
    output_image_8 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel8)
    hit_miss = output_image_1 + output_image_2 + output_image_3 + output_image_4 + output_image_5 + output_image_6 + output_image_7 + output_image_8
    output_image = input_image - hit_miss

    while not np.array_equal(output_image, input_image):
    input_image = output_image
    output_image_1 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel1)
    output_image_2 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel2)
    output_image_3 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel3)
    output_image_4 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel4)
    output_image_5 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel5)
    output_image_6 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel6)
    output_image_7 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel7)
    output_image_8 = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel8)
    hit_miss = output_image_1 + output_image_2 + output_image_3 + output_image_4 + output_image_5 + output_image_6 + output_image_7 + output_image_8
    output_image = input_image - hit_miss

    cv.imshow("hit_miss", hit_miss)
    cv.imshow("out", output_image)
    cv.waitKey(0)
    cv.destroyAllWindows()

    我原本希望从输入中获得薄X图像,但是代码无法停止运行。我想念什么吗?

    最佳答案

    您想使用逻辑或在应用所有内核之后而不只是添加它们。

    import cv2 as cv
    import numpy as np

    img = cv.imread('x.png',0)
    _,img = cv.threshold(img,60,255,cv.THRESH_BINARY)

    cv.imshow("input", img)
    cv.waitKey(0)
    cv.destroyAllWindows()

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

    kernel3 = np.rot90(kernel1)
    kernel4 = np.rot90(kernel2)
    kernel5 = np.rot90(np.rot90(kernel1))
    kernel6 = np.rot90(np.rot90(kernel2))
    kernel7 = np.rot90(np.rot90(np.rot90(kernel1)))
    kernel8 = np.rot90(np.rot90(np.rot90(kernel2)))
    skel = np.zeros(img.shape,np.uint8)
    mask = np.zeros(img.shape,np.uint8)
    kernels = [kernel1, kernel2, kernel3, kernel4, kernel5, kernel6, kernel7, kernel8]

    while (1):
    mask = np.zeros(img.shape,np.uint8)

    for kernel in kernels:
    out_image = cv.morphologyEx(img, cv.MORPH_HITMISS, kernel)
    mask = cv.bitwise_or(out_image, mask)

    img = img - mask

    if (np.array_equal(img, skel)):
    break

    skel = img

    cv.imshow("out", img)
    cv.waitKey(0)
    cv.destroyAllWindows()

    关于python - 使用细化和命中或遗失实现的骨架化永不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58790429/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com