So, I am currently trying to figure out a more optimal solution to determine connected components in an image. Currently, I have an array with coordinates that have certain values. I want to create groups of these coordinates based on if they are touching. I am using a numpy array, and currently I have to check if each value (top left, top middle, top right, middle-left, middle right, bottom left, bottom middle, bottom right) is in that array. I do so via this code:
for x in range (0, groupCoords.shape[0]):
global tgroup
xCoord = groupCoords.item((x,0))
yCoord = groupCoords.item((x,1))
new = np.array([[xCoord, yCoord]])
if np.equal(Arr,[xCoord, yCoord+1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord,yCoord+1]], axis=0)
new = np.append(new, [[xCoord,yCoord+1]], axis=0)
index = np.argwhere((Arr == [xCoord,yCoord+1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord, yCoord-1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord, yCoord-1]],axis=0)
new = np.append(new, [[xCoord,yCoord-1]], axis=0)
index = np.argwhere((Arr == [xCoord,yCoord-1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord+1, yCoord]).all(1).any():
tgroup = np.append(tgroup, [[xCoord+1,yCoord]],axis=0)
new = np.append(new, [[xCoord+1,yCoord]], axis=0)
index = np.argwhere((Arr == [xCoord+1,yCoord]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord+1, yCoord+1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord+1,yCoord+1]],axis=0)
new = np.append(new, [[xCoord+1,yCoord+1]], axis=0)
index = np.argwhere((Arr == [xCoord+1,yCoord+1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord+1, yCoord-1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord+1,yCoord-1]],axis=0)
new = np.append(new, [[xCoord+1,yCoord-1]], axis=0)
index = np.argwhere((Arr == [xCoord+1,yCoord-1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord-1, yCoord]).all(1).any():
tgroup = np.append(tgroup, [[xCoord-1,yCoord]],axis=0)
new = np.append(new, [[xCoord-1,yCoord]], axis=0)
index = np.argwhere((Arr == [xCoord-1,yCoord]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord-1, yCoord+1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord-1,yCoord+1]],axis=0)
new = np.append(new, [[xCoord-1,yCoord+1]], axis=0)
index = np.argwhere((Arr == [xCoord-1,yCoord+1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord-1, yCoord-1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord-1,yCoord-1]],axis=0)
new = np.append(new, [[xCoord-1,yCoord-1]], axis=0)
index = np.argwhere((Arr == [xCoord-1,yCoord-1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
However, this clearly takes a significant amount of time if the image is large. I had the idea to just create an boolean matrix with dimensions of the width and height of the image, and then assign the value "true" to values in the matrix which correspond to pixels in the image (the image is black-white).
I was wondering, is it possible to, instead of having to check each value like that, determine if their are elements labled "true" directly surrounding another "true" value?
This is what the input array would look like:
[
[0 0]
[0 1]
[0 2]
[10 2]
]
The output would look like
[
[0 0]
[0 1]
[0 2]
]
The function I am hoping to refine would check if the "true" values are touching, and create a 'network' of all values touching (it would keep running with the new values found).
[[T, T, F, F], [F, F, F, T], [F, F, F, T]], the output would be something like[[(0, 0), (0, 1)], [(1, 3), (2, 3)]]?