python - 获取像素级蒙版和 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 打开,因为它会占用太多内存。有任何想法吗?谢谢你。
解决方案
您可以应用以下算法:
对于 WSI 中的每个补丁
(x, y, width, height)
,计算其相对于掩码位置的坐标:(x2, y2, width2, height2)
。这里有一些计算要做min
,max
但没有什么是不可能的。对于每个补丁,计算比率
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)
推荐阅读
- php - 如何通过创建 .po 和 .mo 文件来翻译自定义插件
- database - 使用原子更新时是否需要显式锁定
- python - 将 pandas 列中的字典转换为数据框
- c++ - 类的 C++ 循环依赖(单例)
- swift - 包装函数
- sql - 如何找到表中多行的完美匹配?
- json - 使用 400 或 401 在 Axios 中发布请求 - 不记名令牌
- javascript - 如何将内容传递给客户端 html 页面中的元素?
- javascript - 每次我尝试运行它时,我的“来自电子表格 - 提交表单”触发器都会被禁用 - 我如何找出原因?
- gem5 - gem5:RuntimeError:在配置层次结构中找到循环