-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.

3
  • Is using scikit or scipy functions acceptable? Commented Jul 2, 2018 at 15:43
  • scikit is fine Commented Jul 2, 2018 at 15:50
  • Possible duplicate of Searching a sequence in a NumPy array Commented Jul 2, 2018 at 15:58

2 Answers 2

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used it with 'closing' instead of 'opening' since 255 represents white in my image.
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.

1 Comment

Really interesting solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.