I got two binary images of the same size, both contain black blobs.
Essentially, I need to find the positions of all black pixels from both images that are within a specific distance from all black pixels from the other image. This is slow in the most obvious approach. Best case would be some implemented function in numpy.
The result should be two lists or arrays, one for each image, conatining the positions of black pixels that are in proximity of black pixels from the other image.
So when I choose one black pixel and crop the image, with the defined distance arround that pixel, it is garanteed that this exact crop, applied to the other image, also contains black pixels.
Example:
import numpy as np
distance = 2
img1 = np.array([[255, 255, 255, 255, 255, 255, 255, 255],
[255, 0, 0, 0, 255, 255, 255, 255],
[255, 0, 0, 0, 255, 255, 255, 255],
[255, 0, 0, 0, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255]]).astype('uint8')
img2 = np.array([[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 0, 0, 0, 255],
[255, 255, 255, 255, 0, 0, 0, 255],
[255, 255, 255, 255, 0, 0, 0, 255],
[255, 255, 255, 255, 255, 255, 255, 255]]).astype('uint8')
img1_row, img1_col = np.where(img1 == 0)
img2_row, img2_col = np.where(img2 == 0)
img1_positions = np.column_stack((img1_row, img1_col ))
img2_positions = np.column_stack((img2_row, img2_col ))
img1_pos_list = []
img2_pos_list = []
for img1_pos in img1_positions:
for img2_pos in img2_positions:
if abs(img1_pos[0] - img2_pos[0]) <= distance:
if abs(img1_pos[1] - img2_pos[1]) <= distance:
if not any((img1_pos == x).all() for x in img1_pos_list):
img1_pos_list.append(img1_pos )
if not any((img2_pos == x).all() for x in img2_pos_list):
img2_pos_list.append(img2_pos )
print(img1_pos_list)
# [[2, 2],[2, 3],[3, 2],[3, 3]]
print(img2_pos_list)
# [[4, 4],[4, 5],[5, 4],[5, 5]]
So img1_pos_list
contains the 4 positions of the indices of black pixels from the bottom right corner of the blob, and img2_pos_list
contains the indices of black pixels from the top left corner of the blob. These are the only black pixels of each image that are within the distance=2
to any black pixel from the other image.
Applying this to a dataset of 50000+ images, for images size 100x100 and bigger is too slow. Is there a faster way?