Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从行高效创建二进制图像_Python_Numpy_Scipy_Scikit Image - Fatal编程技术网

Python:从行高效创建二进制图像

Python:从行高效创建二进制图像,python,numpy,scipy,scikit-image,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

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

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数组
m
c
是直线方程中的斜率和常数

我得到如下输出:


有更快的方法吗?

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

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

binary_img = (row_idx - m * col_idx) > c
这将使您接近您想要的结果,尽管根据您为直线方程定义轴的方式,您可能需要将结果倒置

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

binary_img = (row_idx - m * col_idx) > c