-1

I have a 6600X5100 numpy array which represents a black & white image. I want to clear this image from black pixels noise- remove all black pixle lines (vertically and horizontally) that are shorter than 2 pixels.

So if I have something like this:

[0,  0,  0,   0,   0, 255]
[0, 255,255, 255, 255, 0 ]
[0, 255,255, 255,  0,  0 ]
[0, 255,255 ,255,  0, 255]
[0, 255,255, 255,  0, 255]
[0,  0,  0,   0,   0,  0 ]

The output array will be like this:

[0,  0,  0,   0,   0,  0 ]
[0, 255,255, 255,  0 , 0 ]
[0, 255,255, 255,  0,  0 ]
[0, 255,255 ,255,  0,  0 ]
[0, 255,255, 255,  0,  0 ]
[0,  0,  0,   0,   0,  0 ]

Performance is critical here so a simple loop over the array won't do. Is there a way to quickly find and replace subarray inside an array? So if [0, 255, 255, 0] or [0, 255, 0] is in the image array, replace those parts with 0.

Or if you have a better solution for this task, I will be grateful.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Phoenix
  • 73
  • 1
  • 7
  • Is using [`scikit`](http://scikit-image.org/) or [`scipy`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html) functions acceptable? – Dev-iL Jul 02 '18 at 15:43
  • `scikit` is fine – Phoenix Jul 02 '18 at 15:50
  • Possible duplicate of [Searching a sequence in a NumPy array](https://stackoverflow.com/questions/36522220/searching-a-sequence-in-a-numpy-array) – RedEyed Jul 02 '18 at 15:58

2 Answers2

4

You may want to look at the morphological filters of scikit-image.

You can define simple filters and use the opening function to clean up the image. You will have to play with the filters to get them exactly as you need them, but the library is very fast.

import numpy as np
from skimage.morphology import opening

img = np.array([[0,  0,  0,   0,   0, 255],
                [0, 255,255, 255, 255, 0 ],
                [0, 255,255, 255,  0,  0 ],
                [0, 255,255 ,255,  0, 255],
                [0, 255,255, 255,  0, 255],
                [0,  0,  0,   0,   0,  0 ]])


# horizontal and vertical filters
hf = np.array([[0,0,0,0,0],
               [0,1,1,1,0],
               [0,0,0,0,0]])
vf = hf.T

# apply each filter in turn
out = opening(opening(img, hf),vf)

out
# returns:
array([[  0,   0,   0,   0,   0,   0],
       [  0, 255, 255, 255,   0,   0],
       [  0, 255, 255, 255,   0,   0],
       [  0, 255, 255, 255,   0,   0],
       [  0, 255, 255, 255,   0,   0],
       [  0,   0,   0,   0,   0,   0]])
James
  • 32,991
  • 4
  • 47
  • 70
2

My solution is similar to the existing one, but I use 2d-convolutions:

import numpy as np
from scipy.signal import convolve2d as conv2

in_arr = np.array([
    [0,  0,  0,   0,   0, 255],
    [0, 255,255, 255, 255, 0 ],
    [0, 255,255, 255,  0,  0 ],
    [0, 255,255 ,255,  0, 255],
    [0, 255,255, 255,  0, 255],
    [0,  0,  0,   0,   0,  0 ]])

padded = np.pad(in_arr, 1, mode='constant', constant_values=0)

# Create a kernel
kern = np.ones((1, 3))

# Perform convolution
mask = np.logical_and((conv2(in_arr, kern,   mode='same') // 255) >= 2,
                      (conv2(in_arr, kern.T, mode='same') // 255) >= 2)

# Apply mask:
out_arr = in_arr * mask

Which also yields the desired result.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99