thermalcam_decoder/cvview.py

142 lines
5.1 KiB
Python
Raw Normal View History

2023-12-29 02:26:24 +02:00
import os
import cv2
import argparse
2023-12-29 17:30:40 +02:00
import numpy as np
from datetime import datetime
2023-12-29 02:26:24 +02:00
# Set up the argument parser
parser = argparse.ArgumentParser(description="Visualize image files and display pixel values on hover.")
parser.add_argument('path', help='The path to an image file.')
# Parse the arguments
args = parser.parse_args()
img_path = args.path
2023-12-29 17:30:40 +02:00
def calibrate(x):
x = x.astype(float)
x /= 256.0
#print('{}..{}'.format(x.max(), x.min()))
ret = ((-1.665884e-08) * x**4.
+ (1.347094e-05) * x**3.
+ (-4.396264e-03) * x**2.
+ (9.506939e-01) * x
+ (-6.353247e+01))
#print('{}..{}'.format(ret.max(), ret.min()))
ret = np.where(ret > 0, ret, 0)
#print('{}..{}'.format(ret.max(), ret.min()))
ret = ret.astype('b')
#print('{}..{}'.format(ret.max(), ret.min()))
return ret
# Global variables for the last mouse position
last_x, last_y = 0, 0
img, calibrated_img = None, None
2023-12-29 02:26:24 +02:00
# Function to get the frame index from the filename
def get_frame_index(filename):
return os.path.splitext(os.path.basename(filename))[0][-5:]
2023-12-29 02:26:24 +02:00
# Function to modify the numeric part of the filename
def modify_filename(filename, frame_increment=1):
2023-12-29 02:26:24 +02:00
directory, basename = os.path.split(filename)
basename_no_ext, ext = os.path.splitext(basename)
print(f"Modifying filename {basename_no_ext} in directory {directory}.")
if len(basename_no_ext) < 5 or not basename_no_ext[-5:].isdigit():
2023-12-29 02:26:24 +02:00
raise ValueError("Filename does not end with five digits.")
num_part = basename_no_ext[-5:]
num = int(num_part) + frame_increment
# Handle rollover
num = num % 100000 # Modulo 100000 for 5 digits
new_name = f"{basename_no_ext[:-5]}{num:05d}{ext}"
2023-12-29 02:26:24 +02:00
new_path = os.path.join(directory, new_name)
if not os.path.exists(new_path):
print(f"No file found at {new_path}.")
return filename # Return the original filename if the new file does not exist
return new_path
# Function to display the image and pixel values along with the frame index
def show_pixel_values(image_path):
global img, calibrated_img, last_x, last_y
def mouse_event(event, x, y, flags, param):
global last_x, last_y
if event == cv2.EVENT_MOUSEMOVE:
last_x, last_y = x, y
update_display(x, y)
img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
if img is None:
print(f"Failed to load image at {image_path}. Check the file path and integrity.")
return False
calibrated_img = calibrate(img) # Calibrate the image for display
cv2.namedWindow('Image')
cv2.setMouseCallback('Image', mouse_event)
update_display(last_x, last_y) # Initial display update
return True
# Function to update the display with pixel values
def update_display(x, y):
global img, calibrated_img
original_pixel_value = img[y, x]
calibrated_pixel_value = calibrated_img[y, x]
text_original = f'Original: {original_pixel_value}, Loc: ({x},{y})'
text_calibrated = f'Calibrated: {calibrated_pixel_value}'
img_text = img.copy()
frame_index = get_frame_index(img_path)
cv2.putText(img_text, f'Frame: {frame_index}', (10, img_text.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(img_text, text_original, (5, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(img_text, text_calibrated, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
cv2.imshow('Image', img_text)
return img_text # Return the image with text for saving
def save_frame(img_text):
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
save_path = f"frame_{current_time}.png"
cv2.imwrite(save_path, img_text)
print(f"Frame saved as {save_path}")
2023-12-29 02:26:24 +02:00
# Ensure the provided path is a valid file
if not os.path.isfile(img_path):
print("The provided path is not a valid file.")
exit(1)
# Initially display the image
if not show_pixel_values(img_path):
exit(1)
# Main loop to navigate through images
while True:
key = cv2.waitKey(0)
if key == 27: # ESC key to exit
break
elif key in [91, 93, ord('{'), ord('}')]: # Keys for frame navigation
if key == 91: # '[' key
img_path = modify_filename(img_path, frame_increment=-1)
elif key == 93: # ']' key
img_path = modify_filename(img_path, frame_increment=1)
elif key == ord('{'): # Shift + '['
img_path = modify_filename(img_path, frame_increment=-50)
elif key == ord('}'): # Shift + ']'
img_path = modify_filename(img_path, frame_increment=50)
if not show_pixel_values(img_path):
break # Exit if the new image cannot be loaded
else:
update_display(last_x, last_y) # Update display with last known mouse position
elif key == ord('s'): # 's' key for saving
# Update the display to get the latest overlay and save it
img_text_with_overlays = update_display(last_x, last_y)
save_frame(img_text_with_overlays)
continue # Skip the frame reload if saving
cv2.destroyAllWindows()