python - histogram of gray scale values in numpy image -
i loaded image numpy array , want plot color values in histogram.
import numpy np skimage import io skimage import color img = io.imread('img.jpg') img = color.rgb2gray(img) unq = np.unique(img) unq = np.sort(unq)
when inspect value of unq
see like
array([ 5.65490196e-04, 8.33333333e-04, 1.13098039e-03, ..., 7.07550980e-01, 7.09225490e-01, 7.10073725e-01])
which has still values matplotlib
idea loop on unq
, remove every value deviates x
it's predecessor.
dels = [] in range(1, len(unq)): if abs(unq[i]-unq[i-1]) < 0.0003: dels.append(i) unq = np.delete(unq, dels)
though method works inefficient not uses numpy's optimized implementations.
is there numpy feature me?
just noticed algorithm looses information how color occurs. let me try fix this.
if want compute histogram, can use np.histogram
:
bin_counts, bin_edges = np.histogram(img, bins, ...)
here, bins
either number of bins, or vector specifying upper , lower bin edges.
if want plot histogram, easiest way use plt.hist
:
bin_counts, bin_edges, patches = plt.hist(img.ravel(), bins, ...)
note used img.ravel()
flatten out image array before computing histogram. if pass 2d array plt.hist()
, treat each row separate data series, not want here.
Comments
Post a Comment