获取像素明智掩码和WSI补丁之间的区域

时间:2018-06-14 11:26:31

标签: python numpy opencv pillow

所以基本上我有一个WSI(整个幻灯片图像),如下所示:

enter image description here

我有一个png面具,看起来像这样:

enter image description here

及其在WSI上的位置(x:1098,y:2116,宽度:167,高度:378)

现在我要做的是拿WSI,从WSI创建尺寸为96x96的补丁,对于每个补丁,我想检查掩模文件下的白色区域是否至少存在2/3创建的补丁。 例如,这是我创建补丁的伪代码:

self.crop_size = 96
is_fit = False
while True:
    patch_x = 0
    while True:
        patches.append((patch_x, patch_y, self.crop_size, self.crop_size, is_fit))
        if patch_x + self.crop_size > width:
            patch_x = width - self.crop_size
            patches.append((patch_x, patch_y, self.crop_size, self.crop_size, is_fit))
            break
        else:
            patch_x += self.crop_size
    if patch_y + self.crop_size > height:
        patch_y = height - self.crop_size
        patches.append((patch_x, patch_y, self.crop_size, self.crop_size, is_fit))
        break
    else:
        patch_y += self.crop_size

现在对于每个补丁(我认为补丁是我在patches.append()中插入的元组)我希望能够将True设置为is_fit,如果至少有2/3的白色面罩的区域存在于贴片中。 请注意,这里我被授权从代码中打开掩码文件,但不会打开WSI,因为它会占用太多内存。 有任何想法吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

您可以应用以下算法:

  • 对于WSI中的每个补丁(x, y, width, height),计算其相对于遮罩位置的坐标:(x2, y2, width2, height2)。这里有一些计算与minmax有关,但没有什么是不可能的。

  • 对于每个补丁,请计算比率cv2.countNonZero(mask[y2:y2+height2, x2:x2 + width2]) / (self.crop_size * self.crop_size)。如果此比率高于2/3,则可以将修补程序设置为isFit = True

为了获得掩码中补丁的位置,我们假设掩码是WSI中坐标为(x_m, y_m, width_m, height_m)的矩形。 然后补丁(x, y, width, height)将在掩码中具有以下坐标:

  • x2 = max(x - x_m, 0)此值可能高于width_m,在这种情况下,您会忽略补丁,因为补丁完全在掩码之外。

  • y2 = max(y - y_m, 0)此值可能高于height_m,在这种情况下,您会忽略补丁,因为补丁完全在掩码之外。

  • width2 = min(self.crop_size, width_m - x2)

  • height2 = min(self.crop_size, height_m - y2)

相关问题