如何得到二值图像的混淆矩阵?

2024-09-29 03:26:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为两个二值图像生成一个混淆矩阵。这些是从GeoTiff图像的两个波段中提取的(使用二进制阈值),尽管我认为这些信息应该是不相关的。你知道吗

dataset = rasterio.open('NDBI.tif')

VH_26Jun2015 = dataset.read(1)
VH_30Sep2015 = dataset.read(3)
GND_Truth = dataset.read(7)

VH_diff = VH_26Jun2015 - VH_30Sep2015
ret,th1 = cv2.threshold(VH_diff,0.02,255,cv2.THRESH_BINARY)

print(confusion_matrix(GND_Truth,th1)

错误1:我使用了上面的代码,遇到了这里提到的问题ValueError: multilabel-indicator is not supported for confusion matrix 我尝试了问题和其他地方提到的argmax(axis=1)解决方案,但得到了一个1983x1983大小的矩阵。(此错误1可能与上述问题中的人遇到的错误相同)。你知道吗

print(confusion_matrix(GND_Truth.argmax(axis=1),th1.argmax(axis=1)))
Output: 
[[8 2 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

我检查了GND_Truthth1的内容,并验证它们是二进制的。你知道吗

numpy.unique(GND_Truth)
Output: 
array([0., 1.], dtype=float32)

错误2:然后我尝试ravel()在传递到confusion_matrix时展平我的二值图像,如下图所示,但结果是3x3矩阵,而我期望的是2x2矩阵。你知道吗

print(confusion_matrix(GND_Truth.ravel().astype(int),th1.ravel().astype(int)))
Output: 
[[16552434        0  2055509]
 [ 6230317        0  1531602]
 [       0        0        0]]

转换数据astype(int)并没有真正起作用。你能告诉我是什么导致了这两个错误吗?你知道吗


Tags: 图像readoutput错误矩阵matrixdatasetprint