import os os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0" # Windows 下禁用硬件加速,避免兼容性问题 import cv2 import orisys import numpy as np from scipy.ndimage import gaussian_filter def main(): # 方式1: 从视频文件读取 # sensor = orisys.Sensor("./s1.mp4", isstitch=True, cuda=True, verbose=True) # 方式2: 从摄像头读取(取消注释使用) sensor = orisys.Sensor(0, isstitch=True, cuda=True, verbose=True) print("\n按 'q' 退出程序,按 'r' 重置光流追踪器") while True: # 步骤 2.1: 获取并拼接图像 sensor.get_img() # 步骤 2.2: 计算形变场 sensor.compute_deformation() fps, flow,vnormal, depth = sensor.read_info( sensor.info.FPS, sensor.info.VRAW, sensor.info.VNORMAL, sensor.info.DEPTH ) arrows = orisys.util.draw_arrows( sensor.img.copy(), # 使用 copy() 避免修改原图 vnormal, threshold=5, grid_spacing=10, arrow_scale=1.0 ) cv2.imshow("形变场 Deformation", arrows) mag = np.sqrt(vnormal[:, :, 0]**2 + vnormal[:, :, 1]**2) mag = gaussian_filter(mag, sigma=12) mag_div = np.gradient(mag, axis=1) + np.gradient(mag, axis=0) mag_div = np.where(mag_div>0, mag_div, 0) mag_img = cv2.applyColorMap( (mag_div/0.2 * 255.0).astype('uint8'), cv2.COLORMAP_JET ) cv2.imshow("magnitude map", mag_img) # print(np.median(mag), np.min(mag), np.max(mag)) print(np.median(mag_div), np.min(mag_div), np.max(mag_div)) # ===================================================================== # 4. 键盘控制 # ===================================================================== key = cv2.waitKey(1) & 0xFF if key == ord("q"): print("\n退出程序...") break if key == ord("r"): # 重置光流追踪器(当追踪漂移时使用) sensor.reset() print("光流追踪器已重置") # ========================================================================= # 5. 清理资源 # ========================================================================= sensor.disconnect() # 释放视频源和相关资源 cv2.destroyAllWindows() if __name__ == '__main__': main()