thermalcam_decoder/cvview.py
2023-12-29 17:30:40 +02:00

109 lines
3.6 KiB
Python

import os
import cv2
import argparse
import numpy as np
# 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
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
class state:
calibrate = False
# Function to display the image and pixel values along with the frame index
def show_pixel_values(image_path):
def mouse_event(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE:
pixel_value = img[y, x]
text = f'Value: {pixel_value}, Location: ({x},{y})'
img_text = img.copy()
# Overlay the frame index
frame_index = get_frame_index(image_path)
cv2.putText(img_text, f'Frame: {frame_index}', (10, img_text.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(img_text, text, (50, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
cv2.imshow('Image', img_text)
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
if state.calibrate:
img = calibrate(img)
cv2.namedWindow('Image')
cv2.setMouseCallback('Image', mouse_event)
cv2.imshow('Image', img)
return True
# Function to get the frame index from the filename
def get_frame_index(filename):
return os.path.splitext(os.path.basename(filename))[0][-4:]
# Function to modify the numeric part of the filename
def modify_filename(filename, increment=True):
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) < 4 or not basename_no_ext[-4:].isdigit():
raise ValueError("Filename does not end with five digits.")
num_part = basename_no_ext[-4:]
num = int(num_part) + (1 if increment else -1)
new_name = f"{basename_no_ext[:-4]}{num:04d}{ext}"
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
# 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 == 91: # '[' key
img_path = modify_filename(img_path, increment=False)
elif key == 93: # ']' key
img_path = modify_filename(img_path, increment=True)
elif key == ord('c'):
state.calibrate = not state.calibrate
# Show the new image
if not show_pixel_values(img_path):
break # Exit the loop if the new image cannot be loaded
cv2.destroyAllWindows()