首页 > 解决方案 > 获取像素级蒙版和 WSI 补丁之间的区域

问题描述

所以基本上我有一个看起来像这样的 WSI(整个幻灯片图像):

在此处输入图像描述

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

在此处输入图像描述

以及它在 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

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

标签: pythonnumpyopencvpillow

解决方案


您可以应用以下算法:

  • 对于 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.

为了获得补丁在掩码中的位置,我们假设掩码是一个矩形,其坐标(x_m, y_m, width_m, height_m)位于 WSI 中。然后一个补丁(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)


推荐阅读