TMA项目技术总结

1. 读文件:

读取单张图片 方法一:

mask_ori = cv2.imread('/Users/gongzhengze/Desktop/xxx/mask_ori.png')
tmp = np.array(mask_ori)[:, :, :3]
mask_ori = cv2.cvtColor(tmp, cv2.COLOR_RGB2GRAY) / 255

读一个黑白二值图,用cv2读进来的文件是三通道的,shape=(2048,2048,3),需要用上面的方法转成二值图,此时shape=(2048,2048),mask_ori.type: <class 'numpy.ndarray'>

读取单张图片 方法二:

img = plt.imread('/Users/gongzhengze/Desktop/xxx/mask_he.png')

这样读进来的图片 直接就是二值图,shape=(2048,2048), img.type: <class 'numpy.ndarray'>

读取单张图片 方法三:

from PIL import Image

img2 = Image.open('/Users/gongzhengze/Desktop/xxx/mask_he.png')
img2 = np.array(img2)

这样读进来的二值图,转换成array后,shape也是(2048,2048), img2.type: <class 'numpy.ndarray'>

读取整个文件夹下.png格式的图片:

import glob
import os

wsi_paths = glob.glob(os.path.join(MASK_HE_PATH, '*.png'))

for wsi_path in wsi_paths:
    # obtain the wsi path
    name = wsi_path.split('/')[-1]
    pid = name[:-4]
    # print(name)
    print('pid:', pid)
    list_of_pid.append(pid)

    mask_he = cv2.imread(MASK_HE_PATH + str(pid) + '.png')
    # print('mask_he.shape:',mask_he.shape)
    # print('mask_he.type:',type(mask_he))

保存文件:

cv2.imwrite('/Users/gongzhengze/Downloads/aaa.png', img1_bg)

或者:

plt.imsave('/Users/gongzhengze/Downloads/aaa.png', bitwiseAnd * 255, cmap='gray')

2. 在mask_he图的边界模版下,计算mask_ori图和mask_gen图的各种评测指标(accuracy,f1score,recall,precision)

def Evaluation(mask_he, mask_ori, mask_gen):
    TP = 0
    FP = 0
    FN = 0
    TN = 0

    for i in range(0, 2048):
        for j in range(0, 2048):
            if mask_he[i, j] != 0:
                # count_he = count_he + 1
                if mask_ori[i, j] == 1 and mask_gen[i, j] == 1:
                    TP = TP + 1
                if mask_ori[i, j] == 0 and mask_gen[i, j] == 1:
                    FP = FP + 1
                if mask_ori[i, j] == 1 and mask_gen[i, j] == 0:
                    FN = FN + 1
                if mask_ori[i, j] == 0 and mask_gen[i, j] == 0:
                    TN = TN + 1

    # accuracy: 正确分类的样本数与总样本数之比(预测为垃圾短信中真正的垃圾短信的比例)
    # precision:判定为正例中真正正例数与判定为正例数之比(所有真的垃圾短信被分类求正确找出来的比例)
    # recall:被正确判定为正例数与总正例数之比
    # f1_score:准确率与召回率的调和平均f1_score

    if TP != 0 or TN != 0 or FP != 0 or FN != 0:
        accuracy = (TP + TN) / (TP + FP + FN + TN)
    else:
        accuracy = 0

    if TP != 0 or FP != 0:
        precision = TP / (TP + FP)
    else:
        precision = 0

    if TP != 0 or FN != 0:
        recall = TP / (TP + FN)
    else:
        recall = 0

    if precision != 0 or recall != 0:
        f1_score = 2 * precision * recall / (precision + recall)
    else:
        f1_score = 0

    return accuracy, precision, recall, f1_score

3. 把数字存到列表里面,好几列数据合并在一个列表里面,并保存成本地文件

list_of_pid = []
list_of_accuracy = []
list_of_precision = []
list_of_recall = []
list_of_f1_score = []

list_of_pid.append(pid)
list_of_accuracy.append(accuracy)
list_of_precision.append(precision)
list_of_recall.append(recall)
list_of_f1_score.append(f1_score)

list_of_pid = pd.DataFrame(list_of_pid, index=None)
list_of_accuracy = pd.DataFrame(list_of_accuracy, index=None)
list_of_precision = pd.DataFrame(list_of_precision, index=None)
list_of_recall = pd.DataFrame(list_of_recall, index=None)
list_of_f1_score = pd.DataFrame(list_of_f1_score, index=None)

list_total = [list_of_pid, list_of_accuracy, list_of_precision, list_of_recall, list_of_f1_score]

list_total = pd.concat(list_total, axis=1, keys=['pid', 'accuracy', 'precision', 'recall', 'f1_score'])
list_total.to_csv('/Users/gongzhengze/Downloads/tmp/list_total.csv', encoding='gbk')

4. 读入图像,是array形式的,把array形式的矩阵转换成tensor

gen_mask = Image.open(gen_mask)

transf = transforms.ToTensor()
gen_mask = transf(gen_mask)  # tensor数据格式是torch(C,H,W)

6. IHC2mask.py 

颜色反卷积:

def sep_stain(img):
    M = np.array([[1.88000259, -1.01536655, -0.55496979],
                  [-0.0735982, 1.13316777, -0.12641597],
                  [-0.59521871, -0.48168478, 1.57247275]])
    im = np.reshape(-1 * np.log((img / 255.) + 1e-5), (-1, 3))
    im = np.matmul(im, M)
    # im = im.view(img.size())
    ret = np.reshape(im, img.shape)
    ret[ret >= 4] = 4 - 0.00001
    ret[ret <= 0] = 0.00001
    ret = 255 * ret / 4
    return np.array(ret, dtype=np.uint8)

高斯模糊:

kernel_size = (3, 3)
sigma = 1.5  # 概括地讲,高斯矩阵的尺寸越大,标准差越大,处理过的图像模糊程度越大。
maskB_blur = cv2.GaussianBlur(maskB, kernel_size, sigma)
# print('maskB_blur:', maskB_blur.shape)

两张图像相交:

maskAB = cv2.bitwise_and(img_mask, maskB_blur)

图像增强 阈值分割:

maskC = cv2.threshold(maskAB, 1, 255, cv2.THRESH_BINARY)[1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值