如何使用python和open获得距离变换图像中的“线”

2024-04-19 18:49:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个半问题的项目,我目前正在工作。我已经成功地在图像上使用了距离变换(cv2.distanceTransform)。现在,我需要得到明亮的'线',是在轮廓内(的地方,是最远的轮廓)。我已经有了一个算法,但结果有一些错误,我想纠正。但我不知道怎么做。我正在使用python、opencv和PyCharm。你知道吗

距离图像:

enter image description here

我尝试了几种形式的阈值,canny和hough(我想这对曲线不起作用)。没有一个能满足我的需要。 我当前的代码是这样工作的。我构造了一个区间,在这里我寻找具有最大值的像素。这些像素的值设置为255。我看了两遍图像。一次从行开始,一次从列开始。不幸的是,这样做会带来不属于该行的像素。这样做的原因当然是所使用的间隔是按行或列计算的。你知道吗

结果是:

enter image description here

你可以看到它不是一条穿过轮廓的最终线条。这张图我稍后会用它作为遮罩来绘制直方图。你知道吗

def maxValueinIntervall(pathname, bounds, rowMax, colMax):
    img = cv2.imread(pathname, 0)
    res = np.zeros(img.shape, img.dtype)
    maxValue = 0
    kernel = np.ones((5, 5), np.uint8)
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            if(img[i, j] == bounds and img[i, j + 1] > bounds and img[i, j - 1] <= bounds):
                n = 1
                k = j
                myList = []
                while(img[i, k + n ] > bounds):
                    myList.append(img[i, k + n])
                    k = n + k
                maxValue = max(myList)

            if (maxValue > rowMax and img[i, j] == maxValue):
                res[i, j] = 255

    for j in range(img.shape[1]):
        for i in range(img.shape[0]):
            if(img[i, j] == bounds and img[i + 1, j] > bounds and img[i - 1, j] <= bounds):
                n = 1
                k = i
                myList = []
                while(img[k + n, j] > bounds):
                    myList.append(img[k + n, j])
                    k = n + k
                maxValue = max(myList)

            if (maxValue > colMax and img[i, j] == maxValue):
                res[i, j] = 255

    return res

可能已经有一个opencv函数,我没有找到它。我希望改进我的解决方案,这样我就能为下一步得到更好的面膜。 谢谢你抽出时间。你知道吗


Tags: andin图像imgforifnprange