簡體   English   中英

將兩個不同的圖像直方圖繪制為單個 2D 直方圖

[英]Plotting two different image histograms as a single 2D histogram plot

我希望在 x 軸上繪制一個 uint16 圖像的直方圖,在 y 軸上繪制另一個 uint16 圖像的直方圖,以便我獲得它們之間關系的顏色圖作為 2D 圖。

這是我追求的那種情節

我試圖形成兩個單獨的直方圖,然后在循環中構建二維數組,但是這是失敗的。

first = np.histogram(img1, bins = 1000)
first = first[0]


second = np.histogram(img2, bins = 1000)
second = second[0]


empty_array = np.zeros((1000,1000), dtype = np.float64)

for i in range(1000):
    for j in range(1000):
        empty_array[i,j] = first[j] + second[1000-j]

如果您正在嘗試研究兩個變量的直方圖以及它們在單個函數中的相互關系,請考慮閱讀多變量正態分布。 這肯定適用於研究圖像中像素的分布。 https://juanitorduz.github.io/multivariate_normal/

看起來這就是你想要做的?:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)
sns.set_context("notebook")
sns.set_style("darkgrid")


# %% Construct normal distribution data
n = 100
hist1 = np.random.normal(0,1,n)
hist2 = np.random.normal(0,1,n)

# %% Plot distributions on their own axis
sns.jointplot(x=hist1, y=hist2, kind="kde", space=0)

多變量正態的 KDE 圖

與 KDE 圖不同的過程,它實際找到定義數據的多變量 PDF,然后繪制 PDF。 這一次hist2有不同的分布比hist1這使得在等高線圖不同的傳播:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)
sns.set_context("notebook")
sns.set_style("darkgrid")
from scipy.stats import multivariate_normal as mvn

# %% Create test data for multivariate PDF
n = 1000
hist1 = np.random.normal(0,1,n)
hist2 = np.random.normal(0,2,n)

# %% Calculate mean and covariance of data
mean = [hist1.mean(), hist2.mean()]
cov_mat = np.cov( np.array([hist1, hist2]) )

# %% Create multivariate function with calculated means and covariance
mv_norm_f = mvn(mean=mean, cov=cov_mat)

# %% Setup ranges of variables for PDF function
range = np.linspace(-1,1,n)
x, y = np.meshgrid(range, range, indexing='xy')
xy = np.empty(x.shape + (2,))
xy[:, :, 0] = x
xy[:, :, 1] = y
print(x.shape)
print(xy.shape)

# %% Call PDF function on ranges of variables
z = mv_norm_f.pdf( xy )

# %% Shaded contour plot the PDF
plt.figure()

plt.contourf(x, y, z)

plt.xlabel("X")
plt.ylabel("Y")
plt.colorbar()
plt.grid('on')
plt.show()

多變量 PDF 的陰影等高線圖

這是使用 seaborn 的解決方案,正如@kilozulu 已經建議的那樣。 我不會使用已經分箱的數據來生成這個圖,因為你會失去兩個圖像之間數據點的關聯。 相反,直接輸入像素意圖:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

#dummy images
img1 = np.random.normal(0,10,(100,100))
img2 = np.random.normal(0,10,(100,100))

# make jointplot with linearised images:
sns.jointplot(img1.ravel(), img2.ravel(), kind='kde')

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM