【发布时间】:2016-01-28 05:33:36
【问题描述】:
在使用 numpy 的优化的同时,opencv 和 python 有没有一种方法可以对图像中的每个像素应用任意函数?
例如,我想获取单通道图像的 sigmoid:
out_image[:,:] = 1 / (1 + math.exp(-in_img[:,:]))
但这不起作用(不足为奇):TypeError: only length-1 arrays can be converted to Python scalars。
有没有办法诱使 numpy 做这样的事情?
我当然可以遍历像素,这在 C++ 中可能很好,但在 python 中它非常慢。
【问题讨论】:
-
尝试用
np.exp替换math.exp。 -
这可以作为这个特定问题的解决方案。我是否还可以定义一个对单个像素进行操作的任意函数,并诱使 numpy 快速运行它?或者我写的任何函数都需要直接对 numpy 数组进行操作?