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.
np.histogram(img, bins)(orplt.hist(img.ravel(), bins)if you just want to plot it)?