所以基本上我有一个看起来像这样的 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 中打开,因为它会占用太多内存。
有任何想法吗?
谢谢你。
最佳答案
您可以应用以下算法:
(x, y, width, height)
在 WSI 中,计算其相对于掩码位置的坐标:(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
在这种情况下,您将忽略补丁,因为它完全位于 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/