如何用Python从Optris红外相机生成的.ravi文件提取指定位置温度数据
提取Optris RAVI文件中指定位置的温度数据(Python方案)
RAVI格式是Optris红外相机封装的AVI变种,除视频帧外还包含每帧的原始温度数据(存储在temp chunk中)。av2仅处理视频流,无法读取温度元数据,以下是两种可行解决方案:
方案1:使用第三方专用库(推荐)
optris_reader是针对Optris RAVI文件的Python解析库,可直接读取温度数据并完成校准转换:
安装与使用
pip install optris-reader
import optris_reader # 初始化阅读器 reader = optris_reader.RaviReader("your_target_file.ravi") # 获取相机校准参数(用于将原始数据转换为实际温度) calibration = reader.get_calibration() frame_total = reader.get_frame_count() # 遍历每帧提取指定位置温度 target_x, target_y = 10, 20 # 目标像素坐标(x为列,y为行) for idx in range(frame_total): # 获取当前帧的原始温度数组(形状:(高度, 宽度)) raw_temp = reader.get_temperature_frame(idx) # 转换为摄氏度 actual_temp = calibration.convert(raw_temp) # 读取指定位置温度 pos_temp = actual_temp[target_y, target_x] print(f"Frame {idx}: Position ({target_x},{target_y}) = {pos_temp:.2f}°C") reader.close()
方案2:手动解析RAVI文件(无依赖方案)
RAVI基于RIFF/AVI格式,温度数据存储在temp chunk中,为连续的16位小端整数。需自行解析文件结构并完成温度转换:
import av import struct import numpy as np # 读取视频流信息 container = av.open("your_target_file.ravi") video_stream = container.streams.video[0] frame_count = video_stream.frames width = video_stream.codec_context.width height = video_stream.codec_context.height # 定位并读取temp chunk数据 temp_chunk_data = b"" with open("your_target_file.ravi", "rb") as f: # 验证RIFF头 riff_header = f.read(12) assert riff_header[:4] == b"RIFF" and riff_header[8:12] == b"AVI " # 遍历所有chunk寻找temp块 while True: chunk_id = f.read(4) if not chunk_id: break chunk_size = struct.unpack("<I", f.read(4))[0] if chunk_id == b"temp": temp_chunk_data = f.read(chunk_size) break else: # 跳过当前chunk f.seek(chunk_size, 1) # 拆分每帧温度数据并转换 # 注意:offset和slope需从相机校准参数中获取(可通过Optris工具或文件元数据提取) cal_offset = 1000 # 示例值,需替换为实际值 cal_slope = 0.01 # 示例值,需替换为实际值 frame_temp_byte_size = height * width * 2 # 每帧温度数据字节数(每个像素2字节) for idx in range(frame_count): # 截取当前帧的原始温度字节数据 start_idx = idx * frame_temp_byte_size end_idx = start_idx + frame_temp_byte_size frame_raw_bytes = temp_chunk_data[start_idx:end_idx] # 转换为16位无符号整数数组 raw_temp_array = np.frombuffer(frame_raw_bytes, dtype=np.uint16).reshape((height, width)) # 计算实际温度(摄氏度) actual_temp = (raw_temp_array - cal_offset) * cal_slope # 读取指定位置温度 target_x, target_y = 10, 20 pos_temp = actual_temp[target_y, target_x] print(f"Frame {idx}: Position ({target_x},{target_y}) = {pos_temp:.2f}°C") container.close()
关键注意事项
- 校准参数(
offset和slope)必须使用对应相机的实际值,否则温度计算会严重偏差。可通过Optris官方软件(如Optris PI)查看,或从RAVI文件的元数据chunk中解析获取。 - 手动解析AVI结构时需处理RIFF格式的细节(如chunk对齐),上述代码为简化版本,复杂场景建议参考RIFF/AVI规范完善解析逻辑。
内容的提问来源于stack exchange,提问作者sreevani i
相关产品推荐
相关产品推荐

