计算两张图片的IOU

这篇博客介绍了一段Python代码,用于计算两张图像的交并比(Intersection over Union, IoU)得分。首先读取两张图像,然后将它们从uint8类型转换为整数,并进行减法操作得到差值数组。接着,通过布尔运算求得两图的交集和并集区域,从而计算IoU分数。代码适用于处理2D或3D图像,但需要注意图像的通道数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cv2
import os
import numpy as np
from pathlib import Path
import time
from PIL import Image
import numpy as np

#Read images
img1 = np.array(Image.open(032439.413.jpg"))#标注图
img2 = np.array(Image.open(32439.413.png"))#pytorch图片
#print(img1.shape)
#print(img2.shape)


#convert unit8 to int numbers, then subtract, then 2D to 1D array
#subArr = np.subtract(img1[:, :, 0].tolist(), img2.tolist()).flatten()

intersection = np.sum(np.logical_and(img1, img2[:, :, 0]))
union = np.sum(np.logical_or(img1, img2[:, :, 0]))


#Intersection over Union
iou_score = intersection / union

print(iou_score)

报错:


Img1 should be the .jpg file that has 3 channels

If your image is 2D, just delete one of the ,: from the code

Or just delete the [:,:,0]

If it's 2D, just img1, if it's 3D img1[:, :, 0]

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值