病理图像(WSI)处理
2023-12-18 19:25:58
最近在做肺腺癌分型相关的项目,记录一下对病理图像(WSI)处理的一些方式。
滑动窗口切片
def crop_img(ori_img, mask_img, cropsize, overlap, image_filename_, label_dataset_path, unlabel_dataset_path):
"""
裁剪图像为指定格式并保存成tiff
输入为array形式的数组
"""
num = 0
mask_threshold = 300
height = ori_img.shape[0]
width = ori_img.shape[1]
print(height)
print(width)
# 从左上开始裁剪
for i in range(int(height / (cropsize * (1 - overlap)))): # 行裁剪次数
for j in range(int(width / (cropsize * (1 - overlap)))): # 列裁剪次数
ori_cropped = ori_img[
int(i * cropsize * (1 - overlap)): int(i * cropsize * (1 - overlap) + cropsize),
int(j * cropsize * (1 - overlap)): int(j * cropsize * (1 - overlap) + cropsize),
:,
] # max函数是为了防止i,j为0时索引为负数 # 通道不裁剪
mask_cropped = mask_img[
int(i * cropsize * (1 - overlap)): int(i * cropsize * (1 - overlap) + cropsize),
int(j * cropsize * (1 - overlap)): int(j * cropsize * (1 - overlap) + cropsize),
] # max函数是为了防止i,j为0时索引为负数 # 通道不裁剪
if np.sum(mask_cropped) < mask_threshold:
ori_path = os.path.join(unlabel_dataset_path,
image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(unlabel_dataset_path,
image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
else:
ori_path = os.path.join(label_dataset_path,
image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(label_dataset_path,
image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
print(ori_path)
cv2.imwrite(ori_path, ori_cropped)
cv2.imwrite(mask_path, mask_cropped)
num = num + 1
# 向前裁剪最后的列
for i in range(int(height / (cropsize * (1 - overlap)))):
ori_cropped = ori_img[
int(i * cropsize * (1 - overlap)): int(i * cropsize * (1 - overlap) + cropsize),
int(j * cropsize * (1 - overlap)): int(j * cropsize * (1 - overlap) + cropsize),
:,
]
mask_cropped = mask_img[
int(i * cropsize * (1 - overlap)): int(i * cropsize * (1 - overlap) + cropsize), # 所有行
width - cropsize: width, # 最后256列
]
if np.sum(mask_cropped) < mask_threshold:
ori_path = os.path.join(unlabel_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(unlabel_dataset_path,
image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
else:
ori_path = os.path.join(label_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(label_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
print(ori_path)
cv2.imwrite(ori_path, ori_cropped)
cv2.imwrite(mask_path, mask_cropped)
num = num + 1
# 向前裁剪最后的行
for j in range(int(width / (cropsize * (1 - overlap)))):
ori_cropped = ori_img[
int(i * cropsize * (1 - overlap)): int(i * cropsize * (1 - overlap) + cropsize),
int(j * cropsize * (1 - overlap)): int(j * cropsize * (1 - overlap) + cropsize),
:,
]
mask_cropped = mask_img[
height - cropsize: height, # 最后256行
int(j * cropsize * (1 - overlap)): int(j * cropsize * (1 - overlap) + cropsize), # 所有列
]
if np.sum(mask_cropped) < mask_threshold:
ori_path = os.path.join(unlabel_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(unlabel_dataset_path,
image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
else:
ori_path = os.path.join(label_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(label_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
print(ori_path)
cv2.imwrite(ori_path, ori_cropped)
cv2.imwrite(mask_path, mask_cropped)
num = num + 1
# 裁剪右下角
ori_cropped = ori_img[
height - cropsize: height,
width - cropsize: width,
:, # 通道不裁剪
]
mask_cropped = mask_img[
height - cropsize: height,
width - cropsize: width,
]
if np.sum(mask_cropped) < mask_threshold:
ori_path = os.path.join(unlabel_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(unlabel_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
else:
ori_path = os.path.join(label_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.jpg')
mask_path = os.path.join(label_dataset_path, image_filename_.split('/')[-1][:-4] + '_' + str(num) + '.png')
print(ori_path)
cv2.imwrite(ori_path, ori_cropped)
cv2.imwrite(mask_path, mask_cropped)
文章来源:https://blog.csdn.net/qq_38784098/article/details/124786578
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!