两通连接组件,组件数量问题 [英] Two pass connected component, Number of components issue

查看:185
本文介绍了两通连接组件,组件数量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两通连通分量算法是检测一个图像中的独立分量,每次检测后,我将每一个分量保存为一个不同的图像。要在单独的图像上显示每个组件,我使用了多个if条件,但是如果条件在每个组件的图像形状,到目前为止我已经使用7条件,但它是增加。任何想法如何使用循环或如何处理它。

 标签中的(x,y):
component = uf.find(labels [(x,y)])
labels [(x,y)] = component
############### #############################################
if标签[(x,y)] == 0:
Zero [y] [x] = int(255)
count = count + 1
如果count <= 43:
继续
elif计数> 43:
Zeroth = Image.fromarray(Zero)
Zeroth.save(os.path.join(dirs,'Zero.png'),'png')
############################################## ############### $ b $如果标签[(x,y)] == 1:
一个[y] [x] = int(255)
count1 = count1 + 1
if count1 <= 43:
continue
elif count1> 43:
First = Image.froma rray(One)
First.save(os.path.join(dirs,'First.png'),'png')


解决方案

我不确定我是否完全理解你的代码,但是我认为可能会有一个简单的修复, c $ c> if 语句。而不是使用单独的变量和代码来保存每个图像,你应该把它们放在列表中,并索引这些列表来获取值来更新和保存。



如何可能看起来你的代码:

 #在上面的某个点上,创建一个图像列表,而不是单独的零,一,等变量
#也创建一个计数列表,而不是count,count1等

为标签中的(x,y):
component = uf.find(labels [(x,y)])
标签[(x,y)] =组件

#可选地,在这里添加代码以创建一个新的图像

images [component] [y] [x] = 255#update image
counts [component] + = 1#update count

if counts [component]> 43:#if count足够高,保存图像
img = images [component] = Image.fromarray(Zero)
img.save(os.path.join(dirs,'image {:02d } .png'.format(component)'png')

请注意,您需要生成图像文件名编程,所以而不是 Zero.png One.png 等,我去了 image00.png image01.png 如果你想保持同名的系统,你可以创建一个从数字到英文名字的映射,但我怀疑使用数字会更方便您以后使用。

如果您不知道多少图像和计数您需要提前,您可以在循环中添加一些额外的逻辑,根据需要创建一个新的逻辑,将它们附加到 images counts 列表。我在上面的代码中给出了一个注释,显示了你想要的逻辑。


Two pass connected component algorithm is detecting separate components in one image, and after each detection i am saving every component as a different image. To display every component on separate image i am using multiple if conditions but these if conditions are increasing whenever there are alot of shapes in an image for every component, so far i have used 7 if conditions but it is increaing. Any ideas how to use loop for it or how to deal with it.

 for (x, y) in labels:
            component = uf.find(labels[(x, y)])
            labels[(x, y)] = component
            ############################################################
            if labels[(x, y)]==0:
                Zero[y][x]=int(255)
                count=count+1
                if count<=43:
                    continue
                elif count>43:
                    Zeroth = Image.fromarray(Zero)
                    Zeroth.save(os.path.join(dirs, 'Zero.png'), 'png')
            #############################################################
            if labels[(x, y)]==1:
                One[y][x]=int(255)
                count1=count1+1
                if count1<=43:
                    continue
                elif count1>43:
                    First = Image.fromarray(One)
                    First.save(os.path.join(dirs, 'First.png'),'png')

解决方案

I'm not sure I fully understand your code, but I think there might be a simple fix to your problem of too many variables and if statements. Instead of using separate variables and code to save each of your images, you should put them in lists and index those lists to get at the values to update and save.

Here's how that might look for your code:

# at some point above, create an "images" list instead of separate Zero, One, etc variables
# also create a "counts" list instead of count, count1, etc

    for (x, y) in labels:
        component = uf.find(labels[(x, y)])
        labels[(x, y)] = component

        # optionally, add code here to create a new image if needed

        images[component][y][x] = 255   # update image
        counts[component] += 1          # update count

        if counts[component] > 43:      # if count is high enough, save out image
            img = images[component] = Image.fromarray(Zero)
            img.save(os.path.join(dirs, 'image{:02d}.png'.format(component), 'png')

Note that you need to generate the image filenames programmatically, so instead of Zero.png and One.png, etc, I went for image00.png and image01.png. You could probably create a mapping from numbers to English names if you wanted to keep the same name system, but I suspect using digits will be more convenient for your later use anyway.

If you don't know how many images and counts you need ahead of time, you could add some extra logic to the loop that would create a new ones as needed, appending them to the images and counts lists. I put a comment in the code above showing where you'd want that logic.

这篇关于两通连接组件,组件数量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆