【CV\segmentation】绘制CoNSeP数据集的四种颜色的实例分割掩码 || 代码合集

【start:231224】

引言

动机

在复现了细胞核分割分类网络Hover-Net(GRAHAM S,2019)后,我们发现,不管是用该模型在CoNSep数据集的测试集上进行推理得到的结果,还是直接把gt的label文件转化为标注得到的结果,其“实例分割轮廓”的颜色都和原数据集的“Overlap文件夹”中的gt的mask存在很大差异;

Overlap folder:
在这里插入图片描述

Overlap mask:
在这里插入图片描述

Our mask:
在这里插入图片描述

后来,我们阅读了CoNSeP数据集的官方文档:

raw Class values: 
	1 = other
	2 = inflammatory
	3 = healthy epithelial
	4 = dysplastic/malignant epithelial
	5 = fibroblast
	6 = muscle
	7 = endothelial

In our paper we combine classes 3 & 4 into the epithelial class and 5,6 & 7
into the spindle-shaped class.

new Class values: 
	1 = other
	2 = inflammatory
	3 = epithelial
	4 = spindle-shaped

发现,论文中展示的标准是“为七种细胞分配七种颜色”,而我们的模型训练的标准是“把七种细胞归纳为四种类型、四种颜色”

在这里插入图片描述

这样一来,实例分割轮廓的颜色的分配自然会是不同的;

所以,为了得到以“四种类型、四种颜色”为基准的结果(让gt show和inference result的颜色匹配上),我们需要自己写一些代码。

论文

【paper】[1] GRAHAM S, VU Q D, RAZA S E A, 等. Hover-Net: Simultaneous segmentation and classification of nuclei in multi-tissue histology images[J/OL]. Medical Image Analysis, 2019, 58: 101563[2023-08-14]. https://linkinghub.elsevier.com/retrieve/pii/S1361841519301045. DOI:10.1016/j.media.2019.101563.

方法

代码

为了展示CoNSeP数据集简化后的四种颜色的mask(七种细胞四种颜色),我们应该手动调整pred_type_map的值,然后将变量输入visualize_instances_map函数,具体如下:

import sys
sys.path.append('../')

import cv2
from misc.viz_utils import visualize_instances_map
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
import os, random

'''
为了展示CoNSeP数据集简化后的四种颜色的mask(七种细胞四种颜色),我们应该手动调整`pred_type_map`的值,然后将变量输入`visualize_instances_map`函数

raw Class values: 
	1 = other
	2 = inflammatory
	3 = healthy epithelial
	4 = dysplastic/malignant epithelial
	5 = fibroblast
	6 = muscle
	7 = endothelial

In our paper we combine classes 3 & 4 into the epithelial class and 5,6 & 7
into the spindle-shaped class.

new Class values: 
	1 = other
	2 = inflammatory
	3 = epithelial
	4 = spindle-shaped
'''

# 定义图像文件夹和标签文件夹路径
image_folder = '/home/linxq/dataset/nuclei/consep/CoNSeP/Test/Images/'
label_folder = '/home/linxq/dataset/nuclei/consep/CoNSeP/Test/Labels/'
# 获取图像文件夹中的所有文件名
image_files = os.listdir(image_folder)
# 从文件列表中随机选择一张图像
# random_image_name = random.choice(image_files)
random_image_name = 'test_11.png'
# 构建图像和标签文件的完整路径
image_path = os.path.join(image_folder, random_image_name)
label_path = os.path.join(label_folder, f"{os.path.splitext(random_image_name)[0]}.mat")
# 读取图像
img = cv2.imread(image_path)
# 读取标签数据
pred_data = sio.loadmat(label_path)

print(image_path, '\n', label_path)

# type_color_dict = {
#   0 : (0, 0, 0),
#   1 : (255, 0, 0),
#   2 : (0, 255, 0),
#   34 : (0, 0, 255),
#   567 : (0, 255, 255)
# }
nuc_color = [
    (0, 0, 0),
    (255, 0, 0),
    (0, 255, 0),
    (0, 0, 255),
    (0, 255, 255)
]

pred_inst_type = np.squeeze(pred_data['inst_type'])
pred_type_map = np.squeeze(pred_data['type_map']).astype(int)
pred_inst_map = pred_data['inst_map']

# 将数组中的所有3和4替换为34,将所有5,6,7替换为567
pred_type_map = np.where(np.isin(pred_type_map, [3, 4]), 34, np.where(np.isin(pred_type_map, [5, 6, 7]), 567, pred_type_map))
pred_type_map = np.where(np.isin(pred_type_map, 34), 3, np.where(np.isin(pred_type_map, 567), 4, pred_type_map))

print("Length of pred_inst_type:", len(pred_inst_type))
print("Shape of pred_type_map:", pred_type_map.shape)
print("Shape of pred_inst_map:", pred_inst_map.shape)
print("Length of nuc_color:", len(nuc_color))
print("Unique values in pred_type_map:", np.unique(pred_type_map))

# overlaid = visualize_instances_map(img, pred_inst_map, type_colour=nuc_color)
overlaid = visualize_instances_map(img, pred_inst_map, type_map=pred_type_map, type_colour=nuc_color, line_thickness=2)

plt.imshow(overlaid)
plt.show()

返回:

/home/linxq/dataset/nuclei/consep/CoNSeP/Test/Images/test_11.png 
 /home/linxq/dataset/nuclei/consep/CoNSeP/Test/Labels/test_11.mat
Length of pred_inst_type: 1077
Shape of pred_type_map: (1000, 1000)
Shape of pred_inst_map: (1000, 1000)
Length of nuc_color: 5
Unique values in pred_type_map: [0 1 2 3 4]

图示

在这里插入图片描述

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值