MATLAB到Python代码snipp

2024-10-02 00:28:06 发布

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

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

谢谢


Tags: ofinstance代码numberopenmeanwidthsum
1条回答
网友
1楼 · 发布于 2024-10-02 00:28:06

试试这个:

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)

相关问题 更多 >

    热门问题