Skip to content

Instantly share code, notes, and snippets.

@sabopy
Created November 29, 2021 19:47
import matplotlib.pyplot as plt
from skimage import exposure, img_as_float
from skimage.color import rgb2gray
import numpy as np
# read image
img = plt.imread("sumiremaru.jpg")
img1 = rgb2gray(img_as_float(img))
img1.shape
#(810, 810)
fig, ax = plt.subplots(1,1,figsize=(5,5))
ax.imshow(img1,cmap="gray")
ax.set_title("sumire-maru")
ax.axis("off")
plt.tight_layout()
plt.savefig("expo_histo_1.jpg",dpi=100)
plt.show()
#make histo data
expo_hist,expo_bin = exposure.histogram(img1,nbins=10)
np_hist,np_bin = np.histogram(img1,bins=10)
plt_hist, plt_bins, patches = ax.hist(img1.flatten(), bins=10)
len(expo_bin),len(np_bin),len(plt_bins)
#(10, 11, 11)
expo_hist==np_hist
#array([ True, True, True, True, True, True, True, True, True,
# True])
expo_hist==plt_hist
#array([ True, True, True, True, True, True, True, True, True,
# True])
#show
fig, ax = plt.subplots(dpi=100)
ax.bar(np_bin[:-1], np_hist,width=np.diff(np_bin)[0],alpha=.5,label="np.histogram,bins[:-1]")
ax.bar(expo_bin, expo_hist,width=np.diff(expo_bin)[0],alpha=.5,label="exposure.histogram")
ax.bar(np_bin[1:], np_hist,width=np.diff(np_bin)[0],alpha=.5,label="np.histogram,bins[1:]")
plt_hist, plt_bins, patches = ax.hist(img1.flatten(),bins=10,alpha=.5,color="C3",label="plt.hist")
for direction in ["right", "top"]:
ax.spines[direction].set_visible(False)
ax.legend(bbox_to_anchor=(1, 0.8))
ax.set(xlabel="bins",ylabel="Frequency")
plt.savefig("expo_histo_2.jpg",dpi=100)
plt.show()
## offset
offset=-110000
fig, ax = plt.subplots(dpi=100)
ax.bar(np_bin[:-1], np_hist,width=np.diff(np_bin)[0],alpha=.75,label="np.histogram, bins[:-1]")
ax.bar(expo_bin, expo_hist,bottom=offset,width=np.diff(expo_bin)[0],alpha=.75,label="exposure.histogram")
ax.bar(np_bin[1:], np_hist,bottom=2*offset,width=np.diff(np_bin)[0],alpha=.75,label="np.histogram, bins[1:]")
plt_hist, plt_bins, patches = ax.hist(img1.flatten(),bins=10,bottom=-1*offset,alpha=.75,color="C3",label="plt.hist")
for direction in ["right", "top"]:
ax.spines[direction].set_visible(False)
ax.legend(bbox_to_anchor=(1, 0.8))
ax.set(xlabel="bins",ylabel="Frequency",yticklabels="")
plt.savefig("expo_histo_3.jpg",dpi=100)
plt.show()
## normalize
expo_hist2,expo_bin2 = exposure.histogram(img1,nbins=10,normalize=True)
expo_hist2.sum()
#1.0
fig, ax = plt.subplots(dpi=100)
ax.bar(expo_bin2, expo_hist2,width=expo_bin2[1]-expo_bin2[0],alpha=.5,color="C1",edgecolor="k",label="exposure.histogram")
ax.set_title("normalize=True")
for direction in ["right", "top"]:
ax.spines[direction].set_visible(False)
ax.legend()
ax.set(xlabel="bins",ylabel="Frequency")
plt.savefig("expo_histo_4.jpg",dpi=100)
plt.show()
fig, ax = plt.subplots(dpi=100)
plt_hist, plt_bins, patches = ax.hist(img1.flatten(),bins=10,color="C3",edgecolor="k",density=True,label="plt.hist")
ax.set_title("normalize=True")
for direction in ["right", "top"]:
ax.spines[direction].set_visible(False)
ax.legend()
ax.set(xlabel="bins",ylabel="Frequency")
plt.savefig("expo_histo_5.jpg",dpi=100)
plt.show()
plt_hist.sum()
#10.008340283569641
plt_hist.sum()*np.diff(plt_bins)[0]
#1.0
np_hist,np_bin = np.histogram(img1,bins=10,density=True)
np_hist.sum()
#10.008340283569641
#version
import matplotlib,skimage
print(matplotlib.__version__)
print(skimage.__version__)
print(np.__version__)
3.5.0
0.18.3
1.21.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment