两次通过连接的组件,组件数量问题
Two pass connected component, Number of components issue
两次通过连通分量算法检测一幅图像中的单独分量,每次检测后我将每个 component
保存为不同的图像。为了在单独的图像上显示每个 component
,我使用了多个 if 条件,但是只要每个组件的图像中有很多形状,这些 if conditions
就会增加,到目前为止我已经使用了 7 个 if 条件,但它是增加。关于如何使用循环或如何处理它的任何想法。
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')
我不确定我是否完全理解您的代码,但我认为可能有一个简单的方法可以解决您的变量和 if
语句过多的问题。与其使用单独的变量和代码来保存每张图像,不如将它们放入列表中并为这些列表编制索引以获取要更新和保存的值。
以下是您的代码的查找方式:
# 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')
请注意,您需要以编程方式生成图像文件名,因此我选择了 image00.png
和 image01.png
,而不是 Zero.png
和 One.png
等。如果你想保持相同的名称系统,你可能会创建一个从数字到英文名称的映射,但我怀疑无论如何使用数字对于你以后的使用来说会更方便。
如果您不知道提前需要多少图像和计数,您可以在循环中添加一些额外的逻辑,根据需要创建新的逻辑,将它们附加到 images
和counts
列表。我在上面的代码中添加了注释,显示了您需要该逻辑的位置。
两次通过连通分量算法检测一幅图像中的单独分量,每次检测后我将每个 component
保存为不同的图像。为了在单独的图像上显示每个 component
,我使用了多个 if 条件,但是只要每个组件的图像中有很多形状,这些 if conditions
就会增加,到目前为止我已经使用了 7 个 if 条件,但它是增加。关于如何使用循环或如何处理它的任何想法。
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')
我不确定我是否完全理解您的代码,但我认为可能有一个简单的方法可以解决您的变量和 if
语句过多的问题。与其使用单独的变量和代码来保存每张图像,不如将它们放入列表中并为这些列表编制索引以获取要更新和保存的值。
以下是您的代码的查找方式:
# 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')
请注意,您需要以编程方式生成图像文件名,因此我选择了 image00.png
和 image01.png
,而不是 Zero.png
和 One.png
等。如果你想保持相同的名称系统,你可能会创建一个从数字到英文名称的映射,但我怀疑无论如何使用数字对于你以后的使用来说会更方便。
如果您不知道提前需要多少图像和计数,您可以在循环中添加一些额外的逻辑,根据需要创建新的逻辑,将它们附加到 images
和counts
列表。我在上面的代码中添加了注释,显示了您需要该逻辑的位置。