'How to determine bin size in histogram with Python?

I have a histogram in Python and would like to output the bin size (the number of values corresponding to each bin). The histogram displays the number of pixels for each pixel value (intensity) from a grayscale image.

import cv2
import matplotlib.pyplot as plt 
import numpy as np

img = cv2.imread("image.jpg", 0)

#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Creating the histogram

hist = cv2.calcHist([img],[0],None,[256],[0,256])

# Plot the figure

plt.figure()
plt.axis("off")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_GRAY2RGB))

plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Pixel Value (Intensity)")
plt.ylabel("Number of Pixels")
plt.plot(hist)
plt.xlim([0, 256])

How could I code it so that I have some sort of output telling me how many counts there are for each pixel value between 0 and 255?

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source