我正在将代码从Matlab转换为Python。Matlab中的代码是:
x = find(sEdgepoints > 0 & sNorm < lowT);
sEdgepoints(x)=0;两个数组都是相同大小的,我基本上是在创建一个掩码。
我读过这里,numpy中的非零()等同于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() 在这个岗位上的用法,我不知道它将如何工作。因此,我有两个问题: 1。如果是,使用哪个数组? 2。如果我是正确的,它不工作,如何转换代码?
发布于 2017-07-08 15:24:23
and执行布尔操作,numpy要求您按位操作,因此您必须使用&,即
x = np.nonzero((dstc > 0) & ( dst < 60))发布于 2017-07-08 15:17:07
试试np.argwhere() (并注意到不等式周围的()的重要性):
>>X=np.array([1,2,3,4,5])
>>Y=np.array([7,6,5,4,3])
>>ans = np.argwhere((X>3) & (Y<7))
>>ans
array([[3],
[4]])发布于 2017-07-08 15:18:38
你可以自己实现它,就像:
x = [[i,j] for i, j in zip(sEdgepoints , sNorm ) if i > 0 and j < lowT]将给出与匹配约束相对应的列表。我想这可能不是你要找的。
也许看看熊猫模型,它比普通的蟒蛇或矮胖:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mask.html更能让伪装更舒适。
https://stackoverflow.com/questions/44987577
复制相似问题