使用sklearn.metrics Jaccard指数处理图像?

3

我正在尝试进行一些图像比较,首先通过查找Jaccard指数。我正在使用sklearn.metrics实现的Jaccard Index。使用下面仅有的一小组数字的示例,它按预期工作。

import numpy as np
from sklearn.metrics import jaccard_similarity_score

#The y_pred represents the values that the program has found 
y_pred = [0,0,1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,0,0,0,1,1,0,0,1,1,0,1,1,1]
#The y_true represents the values that are actually correct 
y_true = [1,0,0,1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,1,0,0,0,0,1,1,1,0,1,0,1,1,1]

iou = jaccard_similarity_score(y_true, y_pred)

尽管它出现了一个错误...
ValueError: unknown is not supported

当我输入两张图片,例如......
iou = jaccard_similarity_score(img_true, img_pred)

我不确定该怎么做,我试着使用OpenCV将图像转换为灰度,并将两张图片都astype(float),但无论哪种情况都没有成功。


3
在你的简单示例中,你使用了一维列表。我假设图像是二维NumPy数组。尝试使用 img_true.flatten() 看看会发生什么。 - Jason Stein
啊,好的,是的,这个方法可行@JasonStein,谢谢你! - Carson
jaccard_similarity_score已被弃用,现已更换为jaccard_score。 - mrtpk
2个回答

6

我作为答案发布,以便问题能够关闭:通过使用 img_true.flatten()img_pred.flatten() 来展平 img_trueimg_pred 解决了问题。


4

您可以使用ravel()将其转换为一维数组:

img_true=np.array(img_true).ravel()
img_pred=np.array(img_pred).ravel()
iou = jaccard_similarity_score(img_true, img_pred)

ravel和flatten在作为numpy数组的方法调用时执行相同的操作!您可以通过将任一答案标记为“接受”来关闭此问题。 - Jason Stein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接