我有两个8位灰度图像,尺寸为166 x 256像素。我计算了它们之间的联合直方图,发现了一些有趣的聚类,我想将它们映射回图像空间中的值以找到它对应的位置。因此对于两个图像A和B(已经通过numpy数组访问了值)
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)
现在让我们说这会产生下图: 在x介于0和~45之间的区域中发生了一些事情,其中y介于0和~2-3之间。这可能是一个空间问题,但我如何使用我存储的IMGindex和Hindex数组映射原始图像中的这些值?或者我接近“反映”问题都错了?
答案 0 :(得分:1)
您的直方图可能更容易被视为交叉图。 x轴对应于图像B
,y轴对应于图像A
。
换句话说,您感兴趣的区域可能是图像A
中持续低值的大区域。 (也许是边界或背景值?)
向前"向后"使用布尔索引,而不是IMGindex
和Hindex
数组。例如:
xmin, xmax = 0, 45
ymin, ymax = 0, 3
region = (A >= ymin) & (A <= ymax) & (B >= xmin) & (B <= xmax)
(尽管如此,在这种情况下,你可能只能使用region = A <= 3
。)
通过&#34;灰化&#34;突出显示这些区域其他一切,你可能会做这样的事情:(我使用随机数据,这比它必须要复杂一点,但希望它能给你一些想法。)
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()