MemoryError,执行工作代码时

时间:2016-11-12 02:36:48

标签: python numpy

我真的需要你的帮助:

def mask_filling(mask, height, width, effective=1):
    """
    Filling the body of the mask borders with one's and returns it:

    111111      111111   
    1000001     1111111
    10000001 -> 11111111
    0100001     1111111

    Choose effective = 1 ONLY IF its a state true, that marker's borders are very continius
    """
    print("Message")
    new_batch = []
    new_batch = np.array(new_batch)
    new_mask = np.zeros(height, width)
    mask = scipy.ndimage.binary_fill_holes(mask).astype(int)
    for i, row in enumerate(mask):
        if sum(row) > 0:
            top_one = np.argmax(row)
            bot_one = width - np.argmax(row[::-1]) - 1
            new_mask[i, top_one:bot_one] = 1
    if(effective):
        return new_mask
    else:
        for i, row in enumerate(mask.T):
            if sum(row) > 0:
                top_one = np.argmax(row)
                bot_one = height - np.argmax(row[::-1]) - 1
                mask[i, top_one:bot_one] = 1
        return new_mask

def Mask(batch, color_thresholds=[160, 70, 70], effective=1, cut = 1):  # batch(batch_size, height, width, n_channels)
    """
    Calculates a binary mask of the marked area. If the marker wasn't clear enough, borders may be interpolated.
    :return: An 2-D array
    """
    height, width = batch.shape[1], batch.shape[2]
    print(height, width)
    red, green, blue = batch[:, :, :, 0], batch[:, :, :, 1], batch[:, :, :, 2]
    mask = (red > color_thresholds[0]) & (green < color_thresholds[1]) & (blue < color_thresholds[2])
    print(mask.shape)
    for i in batch:
        new_batch[i] = mask_filling(mask[i], height, width, effective)
    return new_batch

功能可行。当我用ONE图像调用它们时,它们工作正常。但是当我创建一批ONE图像时:

    batch = []
    im = Image.open('eyes/1.jpg')
    batch.append(np.array(im))
    #im = Image.open('/home/vladislav/Документы/SkinHack/SkinHack/data/first_data/17074039702030.tif')
    #batch.append(np.array(im))
    batch = np.array(batch)    #its shape is (1, 1200, 1920, 3)
    ans = Mask(batch)

它崩溃了这条线:

---> 42 new_batch[i] = mask_filling(mask[i], height, width, effective)元素总数少于最大值。我真的需要让它作为功能工作。 的回溯:

emoryError                               Traceback (most recent call last)
<ipython-input-54-cfedf1a31556> in <module>()
----> 1 ans = Mask(batch)

<ipython-input-51-2d70010a2518> in Mask(batch, color_thresholds, effective, cut)
     40     print(mask.shape)
     41     for i in batch:
---> 42         new_batch[i] = mask_filling(mask[i], height, width, effective)
     43     return new_batch

由于

1 个答案:

答案 0 :(得分:0)

你正在把我当作一个整数 - 用它来索引另一个列表,但我不是一个整数,它是批量存储的任何元素......看起来像一个图像。尝试:

for i, im in enumerate(batch):
    new_mask[i] = mask_filling(mask[i], height, width, effective)