最近在做肺腺癌分型相关的项目,记录一下对病理图像(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)