【问题标题】:Get area between pixel wise masks and WSI patches获取像素级蒙版和 WSI 补丁之间的区域
【发布时间】:2018-06-15 14:38:42
【问题描述】:

所以基本上我有一个看起来像这样的 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

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

【问题讨论】:

    标签: python numpy opencv pillow


    【解决方案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)

    【讨论】:

    • 非常感谢您的帮助,我认为这是实现我想要的正确方法。但在这里我很难获得正确的绝对位置:
    • 这里是要点:gist.github.com/EKami/1cdc53877efc79c77d6388b8c3f3ac53 这看起来很简单,但我对数字真的很不放心:(。知道哪里出了问题吗?
    • @E-Kami 我编辑了答案以显示遮罩中补丁坐标的计算。
    猜你喜欢
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多