如何在图像旁边绘制直方图?

时间:2021-10-14 17:07:25

I would like to evaluate an idea for finding the "right" rotation of documents. The idea is to apply an edge detector and to count the white values per row and per column. In order to evaluate it, I want to plot this count (or a histogram with a small bin size) next to the image. How can I do that?

我想评估一下找到文件的“正确”旋转的想法。其思想是应用边缘检测器,并计算每一行和每列的白值。为了评估它,我想在图像旁边绘制这个计数(或一个小的容器大小的直方图)。我该怎么做呢?

What I did

#!/usr/bin/env python

import scipy.misc
import numpy as np
import scipy.ndimage
import seaborn as sns

im = scipy.misc.imread("morc-0102.jpg", mode="L")
height, width = im.shape
k = np.array([[1, -1],
              [1, -1]])
im = scipy.ndimage.convolve(im, k, mode='constant', cval=0.0)

# Calculate histogram
hist_height = np.zeros(height)
hist_width = np.zeros(width)
for y in range(height):
    for x in range(width):
        hist_height[y] += im[y][x]
        hist_width[x] += im[y][x]
sns.distplot(hist_height, kde=False, rug=False)
sns.distplot(hist_width, kde=False, rug=False)
sns.plt.show()
scipy.misc.imshow(im)

What I want

A visualization similar to this: At the right of the image the (normalized) counts hist_height are plotted, at the bottom the (normalized) counts hist_width are plotted.

一个类似于此的可视化:在图像的右边,(规范化的)计数hist_height被绘制,在底部(标准化的)计数hist_width被绘制。

如何在图像旁边绘制直方图?

I think I have seen something very similar before. I know how to do this manually by resizing the canvas and plotting each line "by hand", but I guess libaries like matplotlib / seaborn support this directly?

我想我以前见过很相似的东西。我知道如何通过调整画布和绘制每一行“手动”来手动操作,但是我猜像matplotlib / seaborn这样的libaries会直接支持它吗?

1 个解决方案

#1


0  

How about adding two bar diagrams as subplots? Like this e.g.

如何将两个条形图添加为子图?像这样的。

import matplotlib.pyplot as plt
import numpy as np

# remove upper corner plot
ax1 = plt.subplot2grid((3, 3), (0, 2))
ax1.axis('off')

# plot some example image
imageExample = np.random.uniform(0,1,(5,5))
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax2.imshow(imageExample)
ax2.axis('off')

# use 'barh' for horizontal bar plot
horzSum = np.sum(imageExample,1)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2, sharey=ax2)
ax3.barh(range(len(horzSum)), horzSum)
ax3.axis('off')

vertSum = np.sum(imageExample, 0)
ax4 = plt.subplot2grid((3, 3), (0, 0), colspan=2, sharex=ax2)
ax4.bar(range(len(vertSum)), vertSum)
ax4.axis('off')

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

如何在图像旁边绘制直方图?

Setting width of barand barhto 1 and color='r'you get something very similar to what you aim for except for the blank spaces that you can probably get rid of somehow.

设置barand barhto 1和color='r'的宽度,你会得到与你的目标非常相似的东西,除了你可能会摆脱的空白区域。

如何在图像旁边绘制直方图?

#1


0  

How about adding two bar diagrams as subplots? Like this e.g.

如何将两个条形图添加为子图?像这样的。

import matplotlib.pyplot as plt
import numpy as np

# remove upper corner plot
ax1 = plt.subplot2grid((3, 3), (0, 2))
ax1.axis('off')

# plot some example image
imageExample = np.random.uniform(0,1,(5,5))
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax2.imshow(imageExample)
ax2.axis('off')

# use 'barh' for horizontal bar plot
horzSum = np.sum(imageExample,1)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2, sharey=ax2)
ax3.barh(range(len(horzSum)), horzSum)
ax3.axis('off')

vertSum = np.sum(imageExample, 0)
ax4 = plt.subplot2grid((3, 3), (0, 0), colspan=2, sharex=ax2)
ax4.bar(range(len(vertSum)), vertSum)
ax4.axis('off')

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

如何在图像旁边绘制直方图?

Setting width of barand barhto 1 and color='r'you get something very similar to what you aim for except for the blank spaces that you can probably get rid of somehow.

设置barand barhto 1和color='r'的宽度,你会得到与你的目标非常相似的东西,除了你可能会摆脱的空白区域。

如何在图像旁边绘制直方图?