I have two 8 bit grayscale images which are 166 by 256 pixels in dimension. I computed the joint histogram between them and found a few interesting clusters for which I want to map back the values in image space to locate where this corresponds. So for two images A and B (to which the values have already been accessed via numpy arrays)
import numpy as np
import matplotlib.pyplot as plt
rows, cols = A.shape[0], B.shape[1]
N = 256 # bins
#### Numpy's method
#H,xedge,yedge = np.histogram2d(A, B, bins=(N,N))
#### Manually
H1 = np.zeros((N, N), dtype=float)
Hindex = []
IMGindex = []
for i,j in product(range(rows), range(cols)):
H1[A[i,j], B[i,j]] = H1[A[i,j], B[i,j]] + 1
IMGindex.append((i,j))
Hindex.append((A[i,j], B[i,j]))
img = plt.imshow(H1.T, origin='low', interpolation='nearest')
img.set_cmap('hot')
plt.colorbar()
plt.show(img)
Now let's say this produces the following figure: There's something going on in the region where x is between 0 and ~45 and where y is between 0 and ~2-3. This may be kind of a spacey question, but how do I map back those values in the original images using the IMGindex and Hindex arrays I stored?? Or am I approaching the "back-mapping" problem all wrong?
Your histogram might be easier to think of as a crossplot. The x-axis corresponds to image
B
and the y-axis to imageA
.In other words the region you're curious about is probably a large area of a constant low value in image
A
. (Perhaps a border or background value?)To go "backwards" use boolean indexing, not the
IMGindex
andHindex
arrays. For example:(Though, in this case, you could probably get away with just
region = A <= 3
.)To highlight these areas by "graying-out" everything else, you might do something like this: (I'm using random data and this is a bit more complicated than it has to be, but hopefully it gives you some ideas.)