我正在将代码从 Matlab 转换为 Python。 Matlab中的代码为:
x = find(sEdgepoints > 0 & sNorm < lowT);
sEdgepoints(x)=0;
两个数组的大小相同,我基本上是在创建一个掩码。
I read here https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html/numpy 中的 nonzero() 相当于 find(),所以我使用了它。在Python中,我有 sEdgepoints 的 dstc 和 sNorm 的 dst 。我也直接输入了lowT = 60。所以,代码是
x = np.nonzero(dstc > 0 and dst < 60)
dstc[x] = 0
但是,我收到以下错误:
Traceback (most recent call last):
File "C:\Python27\Sheet Counter\customsobel.py", line 32, in <module>
x = np.nonzero(dstc > 0 and dst < 60)
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
我读到了 a.any()/a.all() 的用法在这篇文章中 https://stackoverflow.com/questions/34472814/use-a-any-or-a-all,我不确定这将如何运作。所以,我有两个问题:
1. 如果是,使用哪个数组?
2.如果我是正确的但不起作用,如何转换代码?
and
进行布尔运算,而 numpy 希望您进行按位运算,因此您必须使用&
i.e
x = np.nonzero((dstc > 0) & ( dst < 60))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)