Given a line I would like to quickly create a binary image with two regions separated by the line. I am doing it as

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) is

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

and pix_arr is some 2D numpy array. m and c are the slope and constant in the equation for a line.

I get output such as

enter image description here

Is there a faster way of doing this?

有帮助吗?

解决方案

You can simply do something like:

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

binary_img = (row_idx - m * col_idx) > c

This will get you close to what you want, though depending on how you define the axes for your line equation, you may need to flip the result upside down.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top