Dice系数计算

二维图像dice系数计算

多个图片对应计算dice系数,文件顺序应相同

# -- coding: utf-8 --
import cv2
import os
import numpy as np
pre_path = './data/predict/'  #预测文件夹
tar_path = './data/test/label/'  #标签图
pre_files = os.listdir(pre_path)
tar_files= os.listdir(tar_path)
d = []
for (pre_file,tar_file) in zip(pre_files,tar_files):
    s1 = cv2.imread(pre_path + pre_file, 0)   #pre
    s2 = cv2.imread(tar_path + tar_file, 0)
    row, col = s2.shape[0], s2.shape[1]
    s = []
    for r in range(row):
        for c in range(col):
            if s1[r][c] == s2[r][c]:#计算图像像素交集
                s.append(s1[r][c])
    m1 = np.sum(s)
    m2 = np.sum(s1.flatten()) + np.sum(s2.flatten())
    d.append(2*m1/m2)
#     print(2*m1/m2)
print(d)

你可能感兴趣的:(计算机视觉,深度学习,python)