Mapping values from a joint histogram back into th

2019-08-10 14:19发布

问题:

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?

回答1:

Your histogram might be easier to think of as a crossplot. The x-axis corresponds to image B and the y-axis to image A.

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 and Hindex arrays. For example:

xmin, xmax = 0, 45
ymin, ymax = 0, 3
region = (A >= ymin) & (A <= ymax) & (B >= xmin) & (B <= xmax)

(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.)

import numpy as np
import matplotlib.pyplot as plt

A = np.random.random((10,10))
B = np.random.random((10,10))

region = (A > 0.5) & (B > 0.5)

fig, axes = plt.subplots(ncols=2)
for ax, image in zip(axes.flat,[A, B]):
    im = ax.imshow(image, cmap='copper')
    fig.colorbar(im, ax=ax, orientation='horizontal')

    mask = np.ma.masked_where(region, ~region)
    ax.imshow(mask, cmap='gray_r', interpolation='none', alpha=0.5)

plt.show()