将联合直方图的值映射回图像空间 [英] Mapping values from a joint histogram back into the image spaces

查看:76
本文介绍了将联合直方图的值映射回图像空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个8位灰度图像,其尺寸为166 x 256像素.我计算了它们之间的联合直方图,发现了一些有趣的簇,我想针对这些簇将图像空间中的值映射回来以找到对应的位置.因此,对于两个图像A和B(已经通过numpy数组访问了值)

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) 

现在让我们说这产生了下图: 在x介于0到〜45之间以及y介于0到〜2-3之间的区域中发生了一些事情.这可能是一个空间问题,但是如何使用我存储的IMGindex和Hindex数组将这些值映射回原始图像中呢?还是我处理回映射"问题全错了?

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?

推荐答案

您的直方图可能更容易视为交叉图. x轴对应于图像B,y轴对应于图像A.

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.

换句话说,您感兴趣的区域可能是图像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?)

要向后移动,请使用布尔索引,而不要使用IMGindexHindex数组.例如:

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)

(不过,在这种情况下,您可能只需要region = A <= 3就可以摆脱.)

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

这篇关于将联合直方图的值映射回图像空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆