Find and replace subarray in python array
Find and replace subarray in python array
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.
scikit
scipy
scikit is fine– Phoenix
Jul 2 at 15:50
scikit
Possible duplicate of Searching a sequence in a NumPy array
– RedEyed
Jul 2 at 15:58
2 Answers
2
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.
opening
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]])
Thanks. I used it with 'closing' instead of 'opening' since 255 represents white in my image.
– Phoenix
Jul 3 at 11:52
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.
Really interesting solution!
– RedEyed
Jul 3 at 7:33
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is using
scikitorscipyfunctions acceptable?– Dev-iL
Jul 2 at 15:43