I am trying to create an affinity matrix for an image. I am trying to use simple pixel value differences for now, my image is 84×84, flattened it is a vector of size 7056, which gives me affinity matrix A of size 7056×7056. To fill in the values of the affinity matrix, I started with the obvious the method:
import matplotlib.pyplot as plt
import numpy as np
Img = plt.imread("twoObj.bmp")
Img2 = Img.flatten()
(n,) = Img2.shape
print n
A = np.zeros((n,n))
for i in range(n):
for j in range(n):
A[i,j] = np.abs(Img2[i] - Img2[j])
but this was taking too long to execute. Is there any built-in way in Numpy, or available libraries to run this faster?
It may seem a bit mystical, but
should do what you want.
I’ll post a bit more explanation when I get off work (remind me if I don’t!).