【问题标题】:Best algorithm to find a region of the same values找到相同值区域的最佳算法
【发布时间】:2020-04-23 10:21:46
【问题描述】:

单击其中一个像素后,我想返回属于同一区域的像素列表。输入将是所选像素(种子),输出将是具有相同值且属于同一区域的所有像素的列表(不被任何不同值的像素分隔)。

我的想法是创建一个种子的辅助列表并检查每个种子的邻居。如果邻居的值与种子的值相同,则将其附加到区域列表中。我的python实现如下:

def region_growing(x, y):

    value = image[x,y]
    region = [(x,y),]
    seeds = [(x,y),]

    while seeds:
        seed = seeds.pop()
        x = seed[0]
        y = seed[1]
        for i in range(x-1, x+2):
            for j in range(y-1, y+2):
                if image[i,j] == value:
                    point = (i,j,z)
                    if point not in region:
                        seeds.append(point)
                        region.append(point)
    return region

它可以工作,但对于较大的区域来说非常慢。你会建议什么算法?

【问题讨论】:

  • point not in region 非常慢。如果您使用访问点的set 而不是列表,它会快得多。
  • 谢谢,我试试

标签: algorithm image-processing


【解决方案1】:

问题在于指令if point not in region,其执行时间会随着区域的大小而增加。因此复杂度是二次的。

另一个问题是您在该区域的边界多次访问相同的像素,因为您只跟踪该区域中的像素。

您可以通过使用以点为键的已访问像素字典来避免这种情况。


def region_growing(x, y):
    value = image[x,y]
    region = [(x,y),]
    seeds = [(x,y),]
    visited = {(x,y):true}

    while seeds:
        seed = seeds.pop()
        x = seed[0]
        y = seed[1]
        for i in range(x-1, x+2):
            for j in range(y-1, y+2):
                point = (i,j)
                if point in visited:
                    continue
                visited[point] = true
                if image[i,j] == value:
                    region.append(point)
                    seeds.append(point)
    return region

另一种方法是使用布尔矩阵代替字典。这更快,但需要更多的内存空间。

【讨论】:

  • 谢谢。我只建议使用集合而不是字典。我认为它有点快。
【解决方案2】:

我可以建议您使用任何区域填充/绘制算法并对其进行修补,而不是绘制,而是跟踪同一区域的像素。众所周知,史密斯算法快速高效,请参阅Tint Fill Algorithm。

请注意,存储所有像素是低效的,但算法建议水平段就足够了(因此只有两个像素par段)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2019-02-16
    • 1970-01-01
    • 2013-11-01
    • 2013-12-04
    • 2014-03-29
    相关资源
    最近更新 更多