I want to plot the RGB histograms of an image using numpy.histogram
.
(See my function draw_histogram
below)
It works well for a regular range of [0, 255] :
import numpy as np
import matplotlib.pyplot as plt
im = plt.imread('Bulbasaur.jpeg')
draw_histogram(im, minimum=0., maximum=255.)
What I want to do :
I expect the images I use to have out of range values. Sometimes they will be out of range, sometimes not. I want to use the RGB histogram to analyse how bad the values are out of range.
Let's say I expect the values to be at worst in the interval [-512, 512]. I still want the histogram to display the in-range intensities at the right spot, and leave blank the unpopulated range sections. For example, if I draw the histogram of Bulbasaur.jpeg
again but with range [-512, 512], I expect to see the same histogram but contracted along the "x" axis (between the two dashed lines in the histogram below).
The problem :
When I try to draw the histogram for an unregular range, something goes wrong :
import numpy as np
import matplotlib.pyplot as plt
im = plt.imread('Bulbasaur.jpeg')
draw_histogram(im, minimum=-512., maximum=512.)
My code for 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
Question
Does anyone know a way of properly drawing RGB histogram with unregular ranges?