【问题标题】:Plotting two different image histograms as a single 2D histogram plot将两个不同的图像直方图绘制为单个 2D 直方图
【发布时间】:2019-12-15 12:02:17
【问题描述】:

我希望在 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]

【问题讨论】:

  • 听起来很有趣。您尝试过什么来解决您的问题?
  • 目前我有两个直方图,其中包含 bin 编号和每个图像的出现次数。 :例如 hist1 = np.histogram(img1, bins = 1000), hist2 = np.histogram(img2, bins = 1000) 。结果是两个一维数组...我唯一的想法是有一个 for 循环,可以将相应的值对汇总为大小为 1000*1000 的空白二维数组...但不太确定
  • 好的,这是我不明白的事情:为了让您从图像中制作散点图或 kde 图,您的各个图像之间必须存在某种关系。你如何选择一个图像中的哪个像素对应于另一个?是同一个位置吗?你能分享你的原始图像吗?
  • 图像本身在位置方面是相同的,但它们是使用不同的采集设置捕获的。不幸的是,我无法分享图片

标签: python image numpy histogram histogram2d


【解决方案1】:

如果您尝试研究两个变量的直方图以及它们在单个函数中的相互关系,请考虑阅读有关多变量正态分布的内容。这肯定适用于研究图像中像素的分布。 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 绘图不同的过程实际找到定义数据的多变量 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()

【讨论】:

    【解决方案2】:

    这是使用 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')
    

    【讨论】:

    • 这很好,在二维像素数组上使用 ravel 将确保图像之间的索引匹配,前提是两个图像大小相同。这样Img1Img2 将共享相同的索引用于seaborn 的KDE 计算,以正确解决两个变量之间的关系。
    • @kilozulu 我会争辩说,如果两个图像的大小不同,那么在联合图中绘制数据是没有意义的。
    猜你喜欢
    • 2015-01-10
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多