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

2024-04-29 08:02:10 发布

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

我正在尝试进行一些图像比较,首先从查找Jaccard索引开始。我用的是sklearn.metrics公司使用下面的示例实现Jaccard索引,只需要一个小数组,它的工作方式与预期的一样。在

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)

虽然它给出了一个错误。。。在

^{pr2}$

当我给它输入两个图像,比如。。。。在

iou = jaccard_similarity_score(img_true, img_pred)

我不知道该怎么做,我试着用OpenCV将图像转换为灰度,并使两个图像都是astype(float),但在这两种情况下都没有成功。在


Tags: the图像importtruethatsklearnmetricsscore
2条回答

作为答案发帖,这样问题就可以结束了:扁平化img_true和{}通过img_true.flatten()和{}来解决

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

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

相关问题 更多 >