Reputation: 15752
I loaded an image into a numpy array and want to plot its color values in a histogram.
import numpy as np
from skimage import io
from skimage import color
img = io.imread('img.jpg')
img = color.rgb2gray(img)
unq = np.unique(img)
unq = np.sort(unq)
When we inspect the value of unq
we will see something like
array([ 5.65490196e-04, 8.33333333e-04, 1.13098039e-03, ...,
7.07550980e-01, 7.09225490e-01, 7.10073725e-01])
which has still too much values for matplotlib
so my idea was to loop over unq
and remove every value which deviates only x
from it's predecessor.
dels = []
for i in range(1, len(unq)):
if abs(unq[i]-unq[i-1]) < 0.0003:
dels.append(i)
unq = np.delete(unq, dels)
Though this method works it is very inefficient as it does not uses numpy's optimized implementations.
Is there a numpy feature would could do this for me?
Just noticed that my algorithm looses information about how often a color occurs. Let me try to fix this.
Upvotes: 4
Views: 16383
Reputation: 74182
If you just want to compute the histogram, you can use np.histogram
:
bin_counts, bin_edges = np.histogram(img, bins, ...)
Here, bins
could either be the number of bins, or a vector specifying the upper and lower bin edges.
If you want to plot the histogram, the easiest way would be to use plt.hist
:
bin_counts, bin_edges, patches = plt.hist(img.ravel(), bins, ...)
Note that I used img.ravel()
to flatten out the image array before computing the histogram. If you pass a 2D array to plt.hist()
, it will treat each row as a separate data series, which is not what you want here.
Upvotes: 13