I Have a (much bigger) Matrix that looks like this one:
0 0 4 0 0 1
1 0 0 0 1 0
2 1 0 1 0 0
0 0 0 0 0 0
-1 0 -2 -3 0 0
0 0 -1 -2 0 -2
0 -5 0 -1 0 -1
That must turn into something like this one:
0 1 1 1 0 1
0 0 1 0 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
0 1 0 0 1 1
1 1 0 0 1 0
So, each cell in the first one mat stores how many neighbour (in a column-wise manner) pixels must be erased (the resulting one is a boolean mask for this purpose). If the value in the first matrix is positive, the pixels must be erased upwards, in the negative case, downwards.
I've already implemented this with a double for, which turns out to be prohibitively slow...
So, there's a way to do this with a numpy global method?
(I'm using python 3.8, but I can actually use any version)
EDIT:
The double for loop that i've used: Where "pixels_to_erase" is the first mat and "mask_array" is the second
for i in range(nRows):
for j in range(nCols):
signal = np.sign(pixels_to_erase[i,j])
if signal > 0:
upper_bound = i - pixels_to_erase
if upper_bound < 0:
upper_bound = 0
lower_bound = i
else:
upper_bound = i
lower_bound = i + pixels_to_erase
if lower_bound > max_row:
lower_bound = max_row
mask_array[upper_bound:lower_bound,j] = 0
(actually it's an adaptated part of a much bigger code, that I can post in a gist, if needed)