【问题标题】:MATLAB to Python conversion: vectors, arrays, index elementsMATLAB 到 Python 的转换:向量、数组、索引元素
【发布时间】:2019-05-06 02:59:42
【问题描述】:

大家好!我目前正在将 MATLAB 项目转换为 Python 2.7。我正在尝试转换行

h =  [  im(:,2:cols)  zeros(rows,1) ] - [  zeros(rows,1)  im(:,1:cols-1)  ];

当我尝试转换它时

h = np.concatenate((im[1,range(2,cols)], np.zeros((rows, 1)))) -  
np.concatenate((np.zeros((rows, 1)),im[1,range(2,cols - 1)] ))

IDLE 返回不同的错误,例如

ValueError: all the input arrays must have same number of dimensions

我对 Python 很陌生,如果您能推荐其他方法,我将不胜感激。太感谢了!这是我要转换的函数。

function [gradient, or] = canny(im, sigma, scaling, vert, horz)
xscaling = vert; yscaling = horz;
hsize = [6*sigma+1, 6*sigma+1];   % The filter size.

gaussian = fspecial('gaussian',hsize,sigma);
im = filter2(gaussian,im);        % Smoothed image.

im = imresize(im, scaling, 'AntiAliasing',false);

[rows, cols] = size(im);

h =  [  im(:,2:cols)  zeros(rows,1) ] - [  zeros(rows,1)  im(:,1:cols-1)  ];

我还想问在 Python 中主要用于索引和数组的 ':' 运算符的等价物。 : 运算符有什么等价物吗?

我开始的 Python 转换代码:

def canny(im=None, sigma=None, scaling=None, vert=None, horz=None):

xscaling = vert
yscaling = horz

hsize = (6 * sigma + 1), (6 * sigma + 1) # The filter size.

gaussian = gauss2D(hsize, sigma)
im = filter2(gaussian, im) # Smoothed image.
print("This is im")
print(im)
print("This is hsize")
print(hsize)
print("This is scaling")
print(scaling)
#scaling = 0.4
#scaling = tuple(scaling)

im = cv2.resize(im,None, fx=scaling, fy=scaling )  
[rows, cols] = np.shape(im)

【问题讨论】:

标签: python matlab opencv type-conversion data-conversion


【解决方案1】:

假设您的数据在列表列表中。试试这个:

a = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
im = np.array(a, dtype=float)
rows = 3
cols = 3
h = (np.hstack([im[:, 1:cols], np.zeros((rows, 1))])
   - np.hstack([np.zeros((rows, 1)), im[:, :cols-1]]))

MATLAB 的horzcat(即[A B])等价于np.hstackvertcat[A; B])等价于np.vstack

numpy 中的Array indexing 与 MATLAB 非常接近,只是 numpy 中的索引从 0 开始,范围 p:q 表示“p 到 q-1”。

另外,数组的存储顺序默认为row-major,如果需要,您可以使用列优先顺序(参见this)。在 MATLAB 中,数组以列优先顺序存储。要签入 Python,请键入例如 np.isfortran(im)。如果返回 true,则数组的顺序与 MATLAB 相同(Fortran 顺序),否则为行优先(C 顺序)。当您想要优化循环或将数组传递给 C 或 Fortran 例程时,这一点很重要。

理想情况下,尝试尽快将所有内容放入 np.array 中,并且不要使用列表(它们占用更多空间并且处理速度要慢得多)。还有一些怪癖:例如,1.0 / 0.0 会抛出异常,但 np.float64(1.0) / np.float64(0.0) 会返回 inf,就像在 MATLAB 中一样。


来自 cmets 的另一个例子:

d1 = [ im(2:rows,2:cols) zeros(rows-1,1); zeros(1,cols) ] - ...
     [ zeros(1,cols); zeros(rows-1,1) im(1:rows-1,1:cols-1) ];

d2 = [ zeros(1,cols); im(1:rows-1,2:cols) zeros(rows-1,1); ] - ... 
     [ zeros(rows-1,1) im(2:rows,1:cols-1); zeros(1,cols) ];

对于这个,你可以使用np.block,而不是np.vstack和np.hstack。

im = np.ones((10, 15))
rows, cols = im.shape

d1 = (np.block([[im[1:rows, 1:cols], np.zeros((rows-1, 1))],
                [np.zeros((1, cols))]]) -
      np.block([[np.zeros((1, cols))],
                [np.zeros((rows-1, 1)), im[:rows-1, :cols-1]]]))

d2 = (np.block([[np.zeros((1, cols))],
                [im[:rows-1, 1:cols], np.zeros((rows-1, 1))]]) -
      np.block([[np.zeros((rows-1, 1)), im[1:rows, :cols-1]],
                [np.zeros((1, cols))]]))

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 我尝试并使用了您在答案中输入的 d2,但我仍然收到错误“ValueError:操作数无法与形状一起广播 (78,103) (78,104) 我还图像托管空闲屏幕截图和我的代码也方便代码阅读ibb.co/xYdybcx
  • @PauloEstrella 问题来自X = (h + (d1 + d2) / 2.0) * xscaling。检查参数的形状(尤其是h)。在您掌握 numpy 数组的维度和索引之前,我建议您不要只是运行程序,而是在交互式 Python 会话中逐行检查每一行,并检查每个变量的形状和内容,如果有疑问,子表达式。然后,您会立即找到失败的时间和原因。
  • @Jean-ClaudeArbaut Arbaut 我刚刚打印了变量 h、v、d1、d2 的值,结果发现变量 'h' 是返回形状 '(112L , 127L)' 而其余变量返回 '(111L, 128L)'
  • @Jean-ClaudeArbaut 可能是因为您为变量“h”创建的代码行?
【解决方案2】:

使用 np.zeros((Nrows,1)) 您正在生成一个二维数组,其中包含 Nrows 具有 1 个元素的一维数组。然后,使用 im[1,2:cols] 您将获得 cols-2 元素的一维数组。您应该将 np.zeros((rows,1)) 更改为 np.zeros(rows)。

此外,在第二个 np.concatenate 中,当您从 'im' 获取子数组时,您应该采用与第一个连接相同数量的元素。请注意,您少取了一个元素:range(2,cols) VS range(2,cols-1)。

【讨论】:

  • 你应该从 MATLAB 代码开始,因为 Python 的尝试显然是不等价的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 2018-05-18
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 2015-06-22
相关资源
最近更新 更多