thermalcam_decoder/cvview.py
yair-mantis e15ec35ddd viewer
2023-12-29 02:26:24 +02:00

83 lines
2.9 KiB
Python

import os
import cv2
import argparse
# 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
# 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
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)
# Show the new image
if not show_pixel_values(img_path):
break # Exit the loop if the new image cannot be loaded
cv2.destroyAllWindows()