Python:从行高效创建二值图像

标签 python numpy scipy scikit-image

给定一条线,我想快速创建一个二值图像,其中两个区域由线分隔。我正在这样做

rows, cols = pix_arr.shape
arr = [ func(i) for i in range(0,rows*cols)]
arr = np.array(arr,dtype = 'bool')
arr = arr.reshape(pix_arr.shape)

func(i) 是

func = lambda i: (i / cols) - m*(i % cols)-c < 0

pix_arr 是一些 2D numpy 数组。 mc 是直线方程中的斜率和常数。

我得到诸如

之类的输出

enter image description here

有没有更快的方法?

最佳答案

您可以简单地执行以下操作:

row_idx = np.arange(rows)[:, None]
col_idx = np.arange(cols)

binary_img = (row_idx - m * col_idx) > c

这将使您接近您想要的结果,但根据您如何定义直线方程的轴,您可能需要将结果颠倒过来。

关于Python:从行高效创建二值图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22717406/

相关文章:

python - 计算python中每个逗号后面的数字以及第一个逗号之前的数字

python - 在Python中移动矩阵行的最快方法

java - 导入错误: No module named py4j. java_gateway

Python 相当于 MATLAB 的 Legendre 函数

c++ - NumPy 的 C++ : How to iterate over PyArrayObject without a segfault

python - 来自 2D 数组的 4D 数组

python - Pandas 在每行的列上进行整合

python - 查询奇怪的行为。 Google App Engine 数据存储

python - 使用曲线拟合的 Scipy 奇怪结果

python - 从逐笔报价数据到 Renko 图表