I am trying to generate all pairs of coordinates for pixels in an image with colors, without the pairs repeating (order doesn't matter, so ((1,1,1), (2,2,2) is the same as ((2,2,2), (1,1,1)) and we want to include this pair only once). Also it's important to me that the coordinates are stored in a numpy array.
Let's assume i have a 10x10 image. This means that the image has 100 pixels with 3 color channels what equals to 300 coordinates. This gives us 300*299/2 unique pairs of coordinates. Both using itertools.combinations()
or normal python iteration , and then converting to np.array, is painstakingly slow with bigger images (on my pc for 32x32x3 image it takes 5s).
I am able to create a list of all pixels using
all_pixels = np.array(np.meshgrid(range(10), range(10), range(3))).T.reshape(-1, 3)
but that's because we don't have to consider repetitions. Doing that but trying to create pairs of pixels gives me duplicates. I guess i could remove duplicates in some smart way but i have no idea how to do it in an efficient way.
Any help will be greatly appreciated.
It's a bit crude , but for reference this is how i do this now:
start = time.time()
x, y, z = shape
all_pixels = []
for i in range(x):
for j in range(y):
if z > 1:
for k in range(z):
all_pixels.append([i, j, k])
else:
all_pixels.append([i, j])
first_pix = []
second_pix = []
for i in range(len(all_pixels)):
first = all_pixels[i]
for j in all_pixels[i+1:]:
second = j
first_pix.append(first)
second_pix.append(second)
print("generation of pixels took " + str(time.time() - start))
return np.array(first_pix), np.array(second_pix)