I need to extract non-zero pixels from RGBA Image. Code below works, but because I need to deal with really huge images, some speed up will be salutary. Getting “f_mask” is the longest task. Is it possible to somehow make things work faster? How to delete rows with all zero values ([0, 0, 0, 0]) faster?
import numpy as np
import time
img_size = (10000, 10000)
img = np.zeros((*img_size, 4), np.uint16) # Make RGBA image
# Put some values for pixels. Values might be floats
img[0][1] = [1, 2, 3, 4]
img[1][0] = [0, 0, 0, 10]
img[1][2] = [6, 7, 8, 0]
def f_img_to_pts(f_img): # Get non-zero rows with values from whole img array
f_shp = f_img.shape
f_newshape = (f_shp[0]*f_shp[1], f_shp[2])
f_pts = np.reshape(f_img, f_newshape)
f_mask = ~np.all(f_pts == 0, axis=1)
f_pts = f_pts[f_mask]
return f_pts
t1 = time.time()
pxs = f_img_to_pts(img)
t2 = time.time()
print('PIXELS EXTRACTING TIME: ', t2 - t1)
print(pxs)