【问题标题】:How to display out of range values on image histogram?如何在图像直方图上显示超出范围的值?
【发布时间】:2016-11-10 03:28:36
【问题描述】:

我想使用numpy.histogram 绘制图像的 RGB 直方图。

(见下面我的函数draw_histogram

它适用于 [0, 255] 的常规范围:

import numpy as np
import matplotlib.pyplot as plt

im = plt.imread('Bulbasaur.jpeg')
draw_histogram(im, minimum=0., maximum=255.)

我想做的事:

我希望我使用的图像具有超出范围的值。有时它们会超出范围,有时则不会。我想使用 RGB 直方图来分析值超出范围的严重程度。

假设我希望这些值在区间 [-512, 512] 中最差。我仍然希望直方图在正确的位置显示范围内的强度,并将未填充的范围部分留空。例如,如果我再次绘制Bulbasaur.jpeg 的直方图,但范围为 [-512, 512],我希望看到相同的直方图,但沿“x”轴收缩(在下面直方图中的两条虚线之间)。

问题:

当我尝试为不规则范围绘制直方图时,出现问题:

import numpy as np
import matplotlib.pyplot as plt

im = plt.imread('Bulbasaur.jpeg')
draw_histogram(im, minimum=-512., maximum=512.)

我的draw_histogram() 代码:

def draw_histogram(im, minimum, maximum):

    fig = plt.figure()
    color = ('r','g','b')

    for i, col in enumerate(color):
        hist, bins = np.histogram(im[:, :, i], int(maximum-minimum), (minimum, maximum))
        plt.plot(hist, color=col)
        plt.xlim([int(minimum), int(maximum)])

    # Draw vertical lines to easily locate the 'regular range'
    plt.axvline(x=0, color='k', linestyle='dashed')
    plt.axvline(x=255, color='k', linestyle='dashed')

    plt.savefig('Histogram_Bulbasaur.png')
    plt.close(fig)

    return 0

问题

有谁知道正确绘制具有不规则范围的 RGB 直方图的方法?

【问题讨论】:

  • 删除评论,意识到问题所在。

标签: python numpy image-processing histogram rgb


【解决方案1】:

您应该将 x 值传递给 'plt.plot'

我变了:

plt.plot(hist, color=col)

到这里:

plt.plot(np.arange(minimum,maximum),hist, color=col)

随着这个变化,图表开始正常出现。本质上, plt.plot 试图从 np.hist 从 0 开始绘制你给它的 y 值。当你的预期范围从 0 开始时,这有效,但是当你想包含负数时, plt.plot 不应该从 0 开始,而是应该从最小值开始,因此使用 np.range 手动分配 x 值可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多