平均绝对误差 - Python

2024-05-20 14:38:11 发布

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

我是Python新手

我必须实现一个函数,可以计算两个图像之间的MAE

这是我学到的MAE公式:enter image description here

这是我的代码:

def calculateMAE(imageA, imageB):
    """
    Calculate MAE between 2 images
    np: numpy

    """
    mae = np.sum(imageB.astype("float") - imageA.astype("float"))
    mae /= float(imageA.shape[0] * imageA.shape[1] * 255) 

    if (mae < 0):
        return mae * -1
    else:
        return mae

有人能告诉我我的功能是否正确吗? 提前谢谢!


Tags: 函数代码图像returndefnpfloat公式
1条回答
网友
1楼 · 发布于 2024-05-20 14:38:11

平均绝对误差中的绝对符号在sum中的每个条目中,因此在对其求和之后,您无法检查是否mae < 0需要将其放入sum中!

所以你应该

mae = np.sum(np.absolute((imageB.astype("float") - imageA.astype("float")))

其中np.absolute(matrix)按元素顺序计算绝对值。

相关问题 更多 >