I am performing erosion on an image. The image has been padded accordingly. In a nutshell, I have a cross element(+) that I put on every pixel of the image and pick the lowest value for that pixel from pixel above, below, right, left and itself.
It is inefficient and I can't figure out a vectorized version. It must be possible since all calculations are done independently of each other.
for y in range(t,image.shape[0]-b):
for x in range(l,image.shape[1]-r):
a1 = numpy.copy(str_ele)
for filter_y in range(a1.shape[0]):
for filter_x in range(a1.shape[1]):
if (not (numpy.isnan(a1[filter_y][filter_x]))):
a1[filter_y][filter_x] = str_ele[filter_y][filter_x]*image[y+(filter_y-str_ele_center_y)][x+(filter_x-str_ele_center_x)]
eroded_image[y][x] = numpy.nanmin(a1)
basically:
Every pixel in final image = min(pixel, above, below, left, right) from the original image
for y in range(len(eroded_image)):
for x in range(len(eroded_image[1])):
eroded_image2[y][x] = numpy.nanmin(str_ele*image2[y:y+len(str_ele),x:x+(len(str_ele[1]))])
This is what I now have. Still 2 loops.
If
image
is a NaN-padded array, and you are eroding with a cross-shaped footprint, you could remove the for-loops by stacking slices ofimage
(to effectively shift the image up, left, right, and down) and then applynp.nanmin
to the stack of slices.yields
For the small example
image
above,erode
appears to be around 33x faster thanorig
: