我正在解决二进制分割问题。在测试之后,我使用Dice系数来计算测试掩模(地面实况)和相应的预测掩模之间的分割精度。我发现,在我的硬盘中保存之后,预测的掩码不是二进制的,而是大小为256X256X3。事实也是如此,即大小为256X256X3。在布尔转换之后,似乎所有像素值都被转换为true,用于预测的蒙版(第二个图像,img2)。为什么这样?这段代码是否正确计算了它们之间的Dice co-eff的值?
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def dice_coef(img, img2):
if img.shape != img2.shape:
raise ValueError("Shape mismatch: img and img2 must have to be of the same shape.")
else:
intersection = np.logical_and(img, img2)
value = (2. * intersection.sum()) / (img.sum() + img2.sum())
return value
img = cv2.imread('1.png')
print(img)
img = np.asarray(img).astype(np.bool)
print(img)
print(img.shape)
print('_'*40)
img2 = cv2.imread('2.png')
print(img2)
img2 = np.asarray(img2).astype(np.bool)
print(img2)
print(img2.shape)
print('%'*40)
value = dice_coef(img, img2)
print(value)
输出:
[[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[191 191 191]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[255 255 255]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[255 255 255]]
...,
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...,
[157 157 157]
[255 255 255]
[255 255 255]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[255 255 255]
[255 255 255]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[255 255 255]
[255 255 255]]]
[[[False False False]
[False False False]
[False False False]
...,
[False False False]
[False False False]
[ True True True]]
[[False False False]
[False False False]
[False False False]
...,
[False False False]
[False False False]
[ True True True]]
[[False False False]
[False False False]
[False False False]
...,
[False False False]
[False False False]
[ True True True]]
...,
[[False False False]
[False False False]
[False False False]
...,
[ True True True]
[ True True True]
[ True True True]]
[[False False False]
[False False False]
[False False False]
...,
[False False False]
[ True True True]
[ True True True]]
[[False False False]
[False False False]
[False False False]
...,
[False False False]
[ True True True]
[ True True True]]]
(256, 256, 3)
________________________________________
[[[227 227 227]
[243 243 243]
[247 247 247]
...,
[ 2 2 2]
[ 5 5 5]
[ 8 8 8]]
[[233 233 233]
[255 255 255]
[250 250 250]
...,
[ 2 2 2]
[ 4 4 4]
[ 6 6 6]]
[[206 206 206]
[252 252 252]
[253 253 253]
...,
[ 1 1 1]
[ 2 2 2]
[ 4 4 4]]
...,
[[ 5 5 5]
[ 4 4 4]
[ 2 2 2]
...,
[214 214 214]
[254 254 254]
[230 230 230]]
[[ 6 6 6]
[ 5 5 5]
[ 4 4 4]
...,
[171 171 171]
[207 207 207]
[196 196 196]]
[[ 8 8 8]
[ 7 7 7]
[ 5 5 5]
...,
[ 81 81 81]
[ 94 94 94]
[134 134 134]]]
[[[ True True True]
[ True True True]
[ True True True]
...,
[ True True True]
[ True True True]
[ True True True]]
[[ True True True]
[ True True True]
[ True True True]
...,
[ True True True]
[ True True True]
[ True True True]]
[[ True True True]
[ True True True]
[ True True True]
...,
[ True True True]
[ True True True]
[ True True True]]
...,
[[ True True True]
[ True True True]
[ True True True]
...,
[ True True True]
[ True True True]
[ True True True]]
[[ True True True]
[ True True True]
[ True True True]
...,
[ True True True]
[ True True True]
[ True True True]]
[[ True True True]
[ True True True]
[ True True True]
...,
[ True True True]
[ True True True]
[ True True True]]]
(256, 256, 3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0.704061302682
答案 0 :(得分:0)
骰子系数等于图像元素数+图像2的交集元素数的2倍,在您的情况下,函数sum不给您元素数而是总和,只是由于 numpy 的逻辑交集不会为您提供相等的像素(请参阅上面的文档),我建议您像这样修改代码:
def dice_coef(img, img2):
if img.shape != img2.shape:
raise ValueError("Shape mismatch: img and img2 must have to be of the same shape.")
else:
lenIntersection=0
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if ( np.array_equal(img[i][j],img2[i][j]) ):
lenIntersection+=1
lenimg=img.shape[0]*img.shape[1]
lenimg2=img2.shape[0]*img2.shape[1]
value = (2. * lenIntersection / (lenimg + lenimg2))
return value