numpy / scipy中的平方差异(SSD)总和 [英] Sum of Square Differences (SSD) in numpy/scipy

查看:855
本文介绍了numpy / scipy中的平方差异(SSD)总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python和Numpy / Scipy来实现图像处理算法。分析器告诉我在下面的函数(经常调用)中花费了大量时间,它告诉我两个图像之间的平方差异总和

I'm trying to use Python and Numpy/Scipy to implement an image processing algorithm. The profiler tells me a lot of time is being spent in the following function (called often), which tells me the sum of square differences between two images

def ssd(A,B):
    s = 0
    for i in range(3):
        s += sum(pow(A[:,:,i] - B[:,:,i],2))
    return s

我怎样才能加快这个速度?谢谢。

How can I speed this up? Thanks.

推荐答案

只需

s = numpy.sum((A[:,:,0:3]-B[:,:,0:3])**2)

(我预计可能只是总和((AB)** 2)如果形状总是(,3))

(which I expect is likely just sum((A-B)**2) if the shape is always (,,3))

您还可以使用sum方法:((AB)** 2).sum ()

You can also use the sum method: ((A-B)**2).sum()

对吗?

这篇关于numpy / scipy中的平方差异(SSD)总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆