繁体   English   中英

MATLAB到Python的代码片段

[英]MATLAB to Python code snippet

我有以下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代码?

谢谢。

试试这个,代替:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM