Created
November 14, 2023 18:15
-
-
Save treideme/a0466c5821a820894b53eb3843275c38 to your computer and use it in GitHub Desktop.
Display the brightness distribution from input images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simple script to check the brightness distribution from images. | |
Ensure you have opencv and matplotlib installed, via | |
pip install opencv-python matplotlib | |
""" | |
__author__ = "Thomas Reidemeister <thomas@labforge.ca>" | |
import sys | |
import cv2 | |
import matplotlib.pyplot as plt | |
def plot_histogram(image_path): | |
# Read the image | |
image = cv2.imread(image_path, cv2.IMREAD_COLOR) | |
if image is None: | |
print("Could not open or find the image") | |
return | |
# Convert to grayscale | |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
# Calculate the histogram | |
hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256]) | |
# Plot the histogram | |
plt.figure() | |
plt.title("Grayscale Histogram") | |
plt.xlabel("Bins") | |
plt.ylabel("# of Pixels") | |
plt.plot(hist, color="gray") | |
plt.xlim([0, 256]) | |
plt.legend(['Brightness']) | |
# Display the histogram | |
plt.show() | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print("Usage <script> <image>", file=sys.stderr) | |
sys.exit(1) | |
plot_histogram(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment