i want to save RGB,DEPTH,IR iMAGE ,but⚠️ 無法解析 color frame 的格式,and i can only save depth image, no any rgb and ir image can saved
import cv2
import numpy as np
import os
import sys
sys.path.append(r"C:\pyorbbecsdk\install\lib")
import pyorbbecsdk as obs
def main():
save_dir = "dataset"
os.makedirs(save_dir, exist_ok=True)
idx = 1
pipeline = obs.Pipeline()
pipeline.start()
print("按下空格键拍照,按下 q 结束。")
try:
while True:
frames = pipeline.wait_for_frames(100)
if frames is None:
continue
color_frame = frames.get_color_frame()
depth_frame = frames.get_depth_frame()
ir_frame = frames.get_ir_frame()
color_img = None
ir_img = None
# 彩色帧处理
if color_frame:
color_data = np.frombuffer(color_frame.get_data(), dtype=np.uint8)
height, width = color_frame.get_height(), color_frame.get_width()
try:
color_img = color_data.reshape((height, width, 3)) # 假設是 RGB888
except:
try:
color_img = color_data.reshape((height, width, 4)) # 可能是 BGRA
color_img = cv2.cvtColor(color_img, cv2.COLOR_BGRA2BGR)
except:
print("⚠️ 無法解析 color frame 的格式")
color_img = None
if color_img is not None:
cv2.imshow('RGB', color_img)
# 深度帧处理
if depth_frame:
depth_data = np.frombuffer(depth_frame.get_data(), dtype=np.uint16)
depth_img = depth_data.reshape((depth_frame.get_height(), depth_frame.get_width()))
depth_vis = cv2.applyColorMap(cv2.convertScaleAbs(depth_img, alpha=0.03), cv2.COLORMAP_JET)
cv2.imshow('Depth', depth_vis)
# 红外帧处理
if ir_frame:
ir_data = np.frombuffer(ir_frame.get_data(), dtype=np.uint16)
ir_img = ir_data.reshape((ir_frame.get_height(), ir_frame.get_width()))
ir_vis = cv2.convertScaleAbs(ir_img, alpha=0.03)
cv2.imshow('IR', ir_vis)
key = cv2.waitKey(1) & 0xFF
if key == ord(' '):
rgb_path = os.path.join(save_dir, f"rgb{idx:03d}.png")
depth_path = os.path.join(save_dir, f"depth{idx:03d}.png")
ir_path = os.path.join(save_dir, f"ir{idx:03d}.png")
if color_img is not None:
cv2.imwrite(rgb_path, color_img)
if depth_frame:
depth_normalized = cv2.normalize(depth_img, None, 0, 65535, cv2.NORM_MINMAX)
cv2.imwrite(depth_path, depth_normalized)
if ir_img is not None:
cv2.imwrite(ir_path, ir_img)
print(f"✅ 成功儲存:{rgb_path}, {depth_path}, {ir_path}")
idx += 1
elif key == ord('q'):
break
finally:
pipeline.stop()
cv2.destroyAllWindows()
if name == "main":
main()