Created
December 27, 2023 02:43
-
-
Save pengowray/c7321d4cbcf2de62644e4df1b8c63659 to your computer and use it in GitHub Desktop.
turn a video into a brightness graph
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
import cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# turn a video into a brightness graph | |
video_path = 'data/yaoxz7sqop8c1.mp4' | |
video_capture = cv2.VideoCapture(video_path) | |
def apply_gamma_correction(image, gamma=0.5): | |
""" Apply gamma correction to an image. """ | |
inv_gamma = 1.0 / gamma | |
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8") | |
return cv2.LUT(image, table) | |
# Check if the video was opened successfully | |
if not video_capture.isOpened(): | |
raise Exception("Error opening video file") | |
# Get the frame rate of the video | |
frame_rate = video_capture.get(cv2.CAP_PROP_FPS) | |
# Initialize a list to store original brightness values | |
brightness_values = [] | |
# Process the video frame by frame to calculate brightness | |
while True: | |
ret, frame = video_capture.read() | |
if not ret: | |
break | |
# Apply gamma correction | |
gamma_corrected_frame = apply_gamma_correction(frame, gamma=0.5) | |
frame = gamma_corrected_frame | |
# Convert the frame to grayscale | |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# Calculate the average brightness | |
average_brightness = np.mean(gray_frame) | |
brightness_values.append(average_brightness) | |
# Release the video capture object | |
video_capture.release() | |
# Set the figure width to accommodate one pixel per bar | |
num_frames = len(brightness_values) | |
fig_width = num_frames / plt.rcParams['figure.dpi'] | |
# Create the bar graph | |
plt.figure(figsize=(fig_width, 4), tight_layout=True) | |
plt.bar(range(num_frames), brightness_values, width=1.0, color='blue', align='edge') | |
plt.title('Brightness Levels Over Time') | |
#plt.xlabel('Frame Number') | |
plt.ylabel('Brightness') | |
plt.xlim(0, num_frames) # setting x-axis limits | |
plt.ylim(min(brightness_values), max(brightness_values)) # setting y-axis limits | |
# Determine label interval based on the total duration | |
total_duration = num_frames / frame_rate | |
label_interval_seconds = 5 # Adjust this value based on the total duration | |
label_step = int(frame_rate * label_interval_seconds) | |
# Labeling the x-axis with adjusted intervals | |
frame_numbers = np.arange(0, num_frames, step=label_step) | |
time_seconds = frame_numbers / frame_rate | |
plt.xticks(frame_numbers, [f"{time:.0f}s [{frame}f]" for frame, time in zip(frame_numbers, time_seconds)]) | |
# Save the bar graph as a PNG file | |
output_bar_graph_path = 'data/brightness_bar_graph5.png' | |
plt.savefig(output_bar_graph_path, dpi=plt.rcParams['figure.dpi']) # saving with specific dpi to maintain pixel width | |
# Show the path of the saved file | |
output_bar_graph_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment