MATLAB到Python代码片段

时间:2017-09-22 09:03:20

标签: python matlab

我有以下 MATLAB 代码,并希望在Python中重写它:

mean_ssd = sum(sum((imSrc1 - imSrc2).^2))/numel(imSrc1);

我在 Python 中所做的是以下内容:

width, height = Image.open(open(im1)).size
number_of_pixels = width * height    
mean_ssd = sum[math.pow(sum[(imSrc1 - imSrc2)], 2)] / number_of_pixels

但是我在Python中遇到以下错误:

TypeError: unsupported operand type(s) for -: 'instance' and 'instance'

如何在Python中重写MATLAB代码?

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个,而不是:

import numpy as np

width, height = np.shape(imSrc1)[1], np.shape(imSrc1)[0]
number_of_pixels = width * height    

mean_ssd = ((imSrc1 - imSrc2)**2).sum() / float(number_of_pixels)