Smart Ptz Smart Ptz - 1 month ago 15
Python Question

rebuild numpy array, need faster method

I have image numpy array (640,480,3) where X,Y is coordinates and (255 255 255) is a color mask for point

I try to get new 2d array(x,y), where X and Y is coordinates for point when color > zero..

I try this code
and it works, but it takes too much processor time

enter code here
for x in range(edges.shape[0]):
for y in range(edges.shape[1]):
if edges[[x],[y],[0]]!=0:
new.append([x,y])

Answer

You could slice the first element of last axis, compare it against 0 and then use np.argwhere to get those indices, which would be the x, y coordinates in a (N,2) shaped array.

Thus, the implementation would be simply -

new = np.argwhere( edges[...,0]!=0 )
Comments