Two pass connected component, Number of components issue

落爺英雄遲暮 提交于 2019-12-20 07:32:53

问题


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')

回答1:


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.



来源:https://stackoverflow.com/questions/47964336/two-pass-connected-component-number-of-components-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!