play.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import cv2
  2. import numpy as np
  3. from calculator import region_ndti, colorful_ndti_matrix
  4. import threading
  5. def main(video_path, video_mask_path, video_id):
  6. records = []
  7. cap = cv2.VideoCapture(video_path)
  8. if not cap.isOpened():
  9. print("错误:无法打开视频文件。")
  10. print("可能的原因:文件路径错误,或系统缺少必要的解码器。")
  11. exit()
  12. scale = 2
  13. mask = cv2.imread(video_mask_path, cv2.IMREAD_GRAYSCALE)
  14. if mask is None:
  15. raise RuntimeError('mask open failed')
  16. mask = cv2.resize(mask, (mask.shape[1] // scale, mask.shape[0] // scale))
  17. fps = cap.get(cv2.CAP_PROP_FPS)
  18. print("fps:", fps)
  19. while True:
  20. ret, frame = cap.read() # ret指示是否成功读取帧,frame是图像数据
  21. if not ret:
  22. print("视频播放完毕。")
  23. break
  24. # 判断是否为夜间模式
  25. if np.mean(np.abs(frame[:, :, 0] - frame[:, :, 1])) < 0.1:
  26. is_night = True
  27. else:
  28. is_night = False
  29. frame = cv2.resize(frame, (frame.shape[1] // scale, frame.shape[0] // scale))
  30. # 计算掩膜区域的NDTI值
  31. roi_ndti = region_ndti(frame, mask, is_night)
  32. # 给ndti着色
  33. color_ndti = colorful_ndti_matrix(roi_ndti)
  34. # 仅保留感兴趣区的计算结果
  35. roi_index = mask > 0
  36. roi_ndti_fla = roi_ndti[roi_index]
  37. # 3σ原则
  38. # mean_value = np.mean(roi_ndti_fla)
  39. # std_value = np.std(roi_ndti_fla)
  40. # up_limit = mean_value + 3 * std_value
  41. # down_limit = mean_value - 3 * std_value
  42. # roi_ndti_fla = roi_ndti_fla[(up_limit >= roi_ndti_fla) & (roi_ndti_fla >= down_limit)]
  43. text = f'pixs:{int(np.sum(roi_ndti_fla))} mean:{roi_ndti_fla.mean():.3f} std:{roi_ndti_fla.std():.3f} {'night' if is_night else 'day'}'
  44. cv2.putText(frame, text, (10, 25), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA)
  45. cv2.imshow(f'original_{video_id}', frame)
  46. cv2.imshow(f'ndti_color_{video_id}', color_ndti)
  47. cv2.waitKey(30)
  48. # 退出逻辑
  49. key = cv2.waitKey(10) & 0xFF
  50. if key == ord('q'):
  51. break
  52. if __name__ == '__main__':
  53. # main(video_path=r'D:\code\water_turbidity_det\data\night\4_video_20251120_1800.dav',
  54. # video_mask_path=r'D:\code\water_turbidity_det\draw_mask\mask\4_device_capture.png',
  55. # video_id=1)
  56. t1 = threading.Thread(target=main,
  57. args=(r'D:\code\water_turbidity_det\data\records_202512031553\video4_20251129120320_20251129123514.mp4',
  58. r'D:\code\water_turbidity_det\draw_mask\mask\4_device_capture.png',
  59. 4))
  60. # t2 = threading.Thread(target=main,
  61. # args=(r'D:\code\water_turbidity_det\data\day_202511211129\4_video_202511211127.dav',
  62. # r'D:\code\water_turbidity_det\draw_mask\mask\4_device_capture.png',
  63. # 4))
  64. # t1 = threading.Thread(target=main,
  65. # args=(r'D:\code\water_turbidity_det\data\records_202512031553\video1_20251129055729_20251129063102.mp4',
  66. # r'D:\code\water_turbidity_det\draw_mask\mask\1_device_capture.png',
  67. # 1))
  68. #
  69. # t2 = threading.Thread(target=main,
  70. # args=(r'D:\code\water_turbidity_det\data\records_202512031553\video4_20251129053218_20251129060528.mp4',
  71. # r'D:\code\water_turbidity_det\draw_mask\mask\4_device_capture.png',
  72. # 4))
  73. t1.start()
  74. # t2.start()
  75. t1.join()
  76. # t2.join()
  77. cv2.destroyAllWindows()