Picamera2+OpenCV+matplotlib Python exercise to display histogram, run on Raspberry Pi

Previous exercise of Python/OpenCV run on Raspberry Pi to capture images from Camera Module 3 using Picamera2 and display using OpenCV. The exercise plot the histogram using matplotlib.


To install OpenCV and matplotlib on Raspberry Pi OS, read the HERE.

picam2_cv2_histogram.py
"""
Picamera2 + OpenCV exercise:
Run on Raspberry Pi 5 + Camera Module 3,
capture image using capture_array() and display using cv2.imshow().
- plot histogram of the images.

Press:
S/s to save
Q/q to quit

My cameras:
Picamera2(0) - Camera Module 3 NoIR Wide
Picamera2(1) - Camera Module 3

"""

import cv2
import picamera2
from libcamera import controls
import platform
from importlib.metadata import version
import time

import matplotlib
import matplotlib.pyplot as plt

print("Python:", platform.python_version())
print(picamera2.__name__ + ":", version(picamera2.__name__))
print(cv2.__name__ + ":", cv2.__version__)
print(matplotlib.__name__, matplotlib.__version__)
print()
time.sleep(1)

cv2.startWindowThread()

picam2 = picamera2.Picamera2(1)
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (800, 600)}))

# Enabe Auto-focus, currently for Camera Module 3 only.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})

picam2.start()

while True:
    im = picam2.capture_array()
    im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)  # convert to gray

    cv2.imshow("Camera", im)
    
    hist = cv2.calcHist([im],[0],None,[256],[0,256])
    plt.plot(hist)
    plt.title("Histogram GRAY")
    plt.draw()
    plt.pause(0.0001)
    plt.clf()

    key = cv2.waitKey(1)
    if key==ord('s'):
        timeStamp = time.strftime("%Y%m%d-%H%M%S")
        targetPath="/home/pi/Desktop/img" + "_"+timeStamp+".jpg"
        cv2.imwrite(targetPath, im)
        print("- Saved:", targetPath)

    elif key==ord('q'):
        print("Quit")
        break

cv2.destroyAllWindows()
plt.close()


picam2_cv2_histogram_rgb.py
"""
Picamera2 + OpenCV exercise:
Run on Raspberry Pi 5 + Camera Module 3,
capture image using capture_array() and display using cv2.imshow().
- plot R, G, B histogram of the images.

Press:
S/s to save
Q/q to quit

My cameras:
Picamera2(0) - Camera Module 3 NoIR Wide
Picamera2(1) - Camera Module 3

"""

import cv2
import picamera2
from libcamera import controls
import platform
from importlib.metadata import version
import time

import matplotlib
import matplotlib.pyplot as plt

print("Python:", platform.python_version())
print(picamera2.__name__ + ":", version(picamera2.__name__))
print(cv2.__name__ + ":", cv2.__version__)
print(matplotlib.__name__, matplotlib.__version__)
print()
time.sleep(1)

cv2.startWindowThread()

picam2 = picamera2.Picamera2(1)
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (800, 600)}))

# Enabe Auto-focus, currently for Camera Module 3 only.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})

picam2.start()

# define colors to plot the histograms 
colors = ('b','g','r')

while True:
    im = picam2.capture_array()
    cv2.imshow("Camera", im)
    
    for i,color in enumerate(colors):
        hist = cv2.calcHist([im],[i],None,[256],[0,256])
        plt.plot(hist, color = color) 
    
    plt.title("Histogram R, G, B")
    plt.draw()
    plt.pause(0.0001)
    plt.clf()

    key = cv2.waitKey(1)
    if key==ord('s'):
        timeStamp = time.strftime("%Y%m%d-%H%M%S")
        targetPath="/home/pi/Desktop/img" + "_"+timeStamp+".jpg"
        cv2.imwrite(targetPath, im)
        print("- Saved:", targetPath)

    elif key==ord('q'):
        print("Quit")
        break

cv2.destroyAllWindows()
plt.close()


ref:
https://www.geeksforgeeks.org/python-opencv-cv2-calchist-method/


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator