Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- # create mouse global coordinates
- x_mouse = 0
- y_mouse = 0
- # mouse event function
- def mouse_events(event, x, y, flags, param):
- if event == cv2.EVENT_MOUSEMOVE:
- global x_mouse, y_mouse
- x_mouse = x
- y_mouse = y
- # open the camera (change index if needed)
- cap = cv2.VideoCapture(1, cv2.CAP_DSHOW) # try 0 if 1 doesn't work
- # set resolution (adjust to your camera's spec)
- cap.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
- cv2.namedWindow("gray8")
- cv2.setMouseCallback("gray8", mouse_events)
- while True:
- ret, frame = cap.read()
- if not ret:
- print("❌ Failed to grab frame")
- break
- # Convert to grayscale
- gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # Get intensity at pointer
- intensity = gray_frame[y_mouse, x_mouse]
- # Normalize to 8-bit
- gray8_frame = np.zeros_like(gray_frame, dtype=np.uint8)
- gray8_frame = cv2.normalize(gray_frame, gray8_frame, 0, 255, cv2.NORM_MINMAX)
- gray8_frame = np.uint8(gray8_frame)
- # Apply thermal colormap
- gray8_frame = cv2.applyColorMap(gray8_frame, cv2.COLORMAP_INFERNO)
- # Draw pointer and intensity
- cv2.circle(gray8_frame, (x_mouse, y_mouse), 2, (255, 255, 255), -1)
- cv2.putText(gray8_frame, f"Intensity: {intensity}",
- (x_mouse - 40, y_mouse - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
- # Show result
- cv2.imshow("gray8", gray8_frame)
- # Exit on 'q'
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement