Advertisement
ElijahIronfist

Thermal Camera on Windows v1 (+Celcius Approx)

Jun 14th, 2025 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # create mouse global coordinates
  5. x_mouse = 0
  6. y_mouse = 0
  7.  
  8. # mouse event function
  9. def mouse_events(event, x, y, flags, param):
  10.     if event == cv2.EVENT_MOUSEMOVE:
  11.         global x_mouse, y_mouse
  12.         x_mouse = x
  13.         y_mouse = y
  14.  
  15. # open the camera (change index if needed)
  16. cap = cv2.VideoCapture(1, cv2.CAP_DSHOW)  # try 0 if 1 doesn't work
  17.  
  18. # set resolution (adjust to your camera's spec)
  19. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
  20. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
  21.  
  22. cv2.namedWindow("gray8")
  23. cv2.setMouseCallback("gray8", mouse_events)
  24.  
  25. while True:
  26.     ret, frame = cap.read()
  27.     if not ret:
  28.         print("❌ Failed to grab frame")
  29.         break
  30.  
  31.     # Convert to grayscale
  32.     gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  33.  
  34.     # Get intensity at pointer
  35.     intensity = gray_frame[y_mouse, x_mouse]
  36.  
  37.     # Normalize to 8-bit
  38.     gray8_frame = np.zeros_like(gray_frame, dtype=np.uint8)
  39.     gray8_frame = cv2.normalize(gray_frame, gray8_frame, 0, 255, cv2.NORM_MINMAX)
  40.     gray8_frame = np.uint8(gray8_frame)
  41.  
  42.     # Apply thermal colormap
  43.     gray8_frame = cv2.applyColorMap(gray8_frame, cv2.COLORMAP_INFERNO)
  44.  
  45.     # Draw pointer and intensity
  46.     cv2.circle(gray8_frame, (x_mouse, y_mouse), 2, (255, 255, 255), -1)
  47.     cv2.putText(gray8_frame, f"Intensity: {intensity}",
  48.                 (x_mouse - 40, y_mouse - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
  49.  
  50.     # Show result
  51.     cv2.imshow("gray8", gray8_frame)
  52.  
  53.     # Exit on 'q'
  54.     if cv2.waitKey(1) & 0xFF == ord('q'):
  55.         break
  56.  
  57. cap.release()
  58. cv2.destroyAllWindows()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement