【发布时间】:2017-12-25 13:55:43
【问题描述】:
labels[(x, y)] 返回coordinate(x) 和coordinate(y) 处的值。labels[(x, y)] 实际上代表图像中的不同形状,检测后每个形状都保存为不同的图像。对于每个形状或组件,我使用多个if 条件,例如if labels[(x, y)] == 0: # save it as an image。但是,对于每个新形状,我都必须创建一个新的if,到目前为止我使用的是7 if conditions。我怎样才能只用一个 if 条件来解决这个问题。
for (x, y) in labels:
component = uf.find(labels[(x, y)])
labels[(x, y)] = component
print (format(x) +","+ format(y)+ " " +format(labels[(x, y)]))
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')
【问题讨论】:
-
它使用两遍连通分量算法来检测图像中的分量,因此 uf 是数组文件,标签位于每个坐标 x 和坐标 y 上,该值称为标签,因此“==0”表示一个标记为“0”的形状,依此类推
-
你能解决吗
标签: python numpy for-loop if-statement python-imaging-library