从所有图像中裁剪扇区并在循环中使用不同的名称保存它们

时间:2017-08-06 08:02:49

标签: python-3.x scikit-image

我正在尝试从所有图像裁剪扇区(图像包含圆形对象)并尝试在循环中使用不同的名称保存它们。这是我的代码与python 3.6和scikit图像

for filename in os.listdir("directory1"):
    image=io.imread(os.path.join("path1",filename))
'''get coordinates for polygon & circle,create masks for circle and polygon'''
'''(r0,c0),(r1,c1),(r2,c2) are circle center , coord's of sector''' 
  for k in range(7):
    imagevar=image
    rr, cc = draw.polygon([r0, r1[K], r1[K+1], r0],
                      [c0, c1[K], c1[K+1], c0], shape=mask_poly.shape)            
    mask_poly[rr, cc] = 1
    image_mask = mask_circle & mask_poly
    mk=numpy.logical_not(image_mask)
    imagevar[mk]=0
    path2='*pathforsamples*' #different from path1
    NewfileNameToSave = *path2*+fileNameToSave + '_'+ str(K+1) + '.jpg'
    io.imsave(NewfileNameToSave,imagevar)
    mask_poly[rr, cc] = 0

文件名没有问题,但我的图像有问题。我从第1开始获得第一个部门 迭代,但从第二次迭代,我得到一个黑色图像和一个警告说“filename_2.jpg是低对比度图像”。我猜第一个扇区正用于剩余的迭代,即使我在循环开始时分配了“imagevar = image”。那么,有没有办法在“imsave”或代码中的任何错误之后卸载图像?

1 个答案:

答案 0 :(得分:0)

为我的问题找到了解决方案。在第二个for循环中,我们可以使用imread

再次读取图像
相关问题