2014-09-26 63 views
1

我有兩個尺寸爲166乘256像素的8位灰度圖像。我計算了它們之間的聯合直方圖,並找到了一些有趣的聚類,我想要映射它們在圖像空間中的值以找到相應的位置。因此,對於兩個圖像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數組映射回原始圖像中的這些值?或者我接近「反向映射」問題都是錯誤的?

回答

1

您的直方圖可能更容易被認爲是交會圖。 x軸對應於圖像B並且y軸對應圖像A

換句話說,您好奇的地區可能是圖片A中恆定較小值的大面積區域。 (也許是邊界或背景值?)

要「倒退」使用布爾索引,而不是IMGindexHindex陣列。例如:

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

(雖然,在這種情況下,你很可能只用region = A <= 3脫身。)

要通過「老齡化出」一切,你會做這樣的事情突出這些領域(我使用的是隨機數據,這是一個比較複雜一點比它是,但希望它給你一些想法。)

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

enter image description here

+0

「x軸對應於圖像B,y軸對應圖像A.」我認爲這是另一回事?當我這樣做時,ImageJ指定圖像A是x軸,圖像B是y軸,我得到的是同一個圖。除此之外,這非常美麗非常感謝你! – elefun 2014-09-27 17:53:53

+0

另外有一種方法可以使背景圖像(A和B)灰度,但在銅中有掩模? – elefun 2014-09-27 20:57:22