python - 获取像素级蒙版和 WSI 补丁之间的区域

标签 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() 中插入的元组补丁)我希望能够设置 Trueis_fit如果至少 2/3 的蒙版白色区域存在于补丁中。
请注意,在这里我有权从代码中打开掩码文件,但不能从 WSI 中打开,因为它会占用太多内存。
有任何想法吗?
谢谢你。

最佳答案

您可以应用以下算法:

  • 对于每个补丁(x, y, width, height)在 WSI 中,计算其相对于掩码位置的坐标:(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在这种情况下,您将忽略补丁,因为它完全位于 mask 之外。
  • y2 = max(y - y_m, 0)此值可以高于 height_m在这种情况下,您将忽略补丁,因为它完全位于 mask 之外。
  • width2 = min(self.crop_size, width_m - x2)
  • height2 = min(self.crop_size, height_m - y2)
  • 关于python - 获取像素级蒙版和 WSI 补丁之间的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856449/

    相关文章:

    python - 如何定义带点的二维数组并分别限制 x 和 y

    python - OpenCV-车道检测 'numpy.ndarray'错误

    python - 从扫描文档中的图表中提取数据

    python - 如何在python中使用cv2知道文件中的总帧数

    python - 我想水平屏蔽多个图像

    python - 多处理仅运行该函数而不是整个文件

    python - 用于在 python 中循环数组 - 来自 matlab

    python - 使 3-d numpy 数组的每个第 n 个切片连续

    python - 从时间序列中删除特定日期(2 月 29 日)的最有效方法

    python - 谷歌照片 API + python : Working non-deprecated example