OpenCV-cuda cv2.cuda.createStereoConstantSpaceBP
我在查看OpenCV-cuda版支持的立体匹配算法时,发现其中包含了’createStereoBM’, ‘createStereoBeliefPropagation’, ‘createStereoConstantSpaceBP’, 'createStereoSGM’这四个模型,但是实际使用时发现了很多坑,多次搜索+询问gpt才发现是版本的坑。
gpu_left = cv2.cuda_GpuMat(); gpu_left.upload(left_raw)
gpu_right = cv2.cuda_GpuMat(); gpu_right.upload(right_raw)
gpu_left_gray = cv2.cuda.cvtColor(gpu_left, cv2.COLOR_BGR2GRAY)
gpu_right_gray = cv2.cuda.cvtColor(gpu_right, cv2.COLOR_BGR2GRAY)
gpu_left_rect = cv2.cuda.remap(gpu_left_gray, left_map1_gpu, left_map2_gpu, interpolation=cv2.INTER_LINEAR)
gpu_right_rect = cv2.cuda.remap(gpu_right_gray, right_map1_gpu, right_map2_gpu, interpolation=cv2.INTER_LINEAR)
print(f"gpu_left_rect type : {gpu_left_rect.type()}, channels: {gpu_left_rect.channels()}, size: {gpu_left_rect.size()}")
print(f"gpu_right_rect type : {gpu_right_rect.type()}, channels: {gpu_right_rect.channels()}, size: {gpu_right_rect.size()}")
try:
if gpu_left_rect is None or gpu_left_rect.empty():
print("Error: gpu_left_rect is empty before creating gpu_disp.")
sys.exit("Exiting due to empty rectified left image.")
except Exception as e:
print(f"Error creating GpuMat for disparity output: {e}")
sys.exit("Exiting due to GpuMat creation error for disparity.")
# --- 调用 compute 方法,将 gpu_disp 作为输出参数 ---
try:
gpu_disp = stereo_csbp.compute(gpu_left_rect, gpu_right_rect)
# 有些接口可能还需要一个 stream 参数:
# stereo_sgbm.compute(gpu_left_rect, gpu_right_rect, gpu_disp, cv2.cuda.Stream_Null())
print("Disparity computation presumably successful (no immediate error).")
print("Downloading disparity from UMat to CPU...")
disp_16s = gpu_disp.download()
disp = disp_16s.astype(np.float32) / 16.0
except cv2.error as e:
print(f"OpenCV Error during CSBP compute: {e}")
# 进一步分析错误
sys.exit("Exiting due to OpenCV error during compute.")
except Exception as e:
print(f"Generic Error during CSBP compute: {e}")
sys.exit("Exiting due to generic error during compute.")
运行后报错,提示:
gpu_left_rect type : 0, channels: 1, size: (640, 480)
gpu_right_rect type : 0, channels: 1, size: (640, 480)
OpenCV Error during CSBP compute: OpenCV(4.10.0) /home/laplace/下载/opencv_contrib/opencv_contrib-4.10.0/modules/cudastereo/src/stereocsbp.cpp:333: error: (-213:The function/feature is not implemented) Not implemented in function 'compute'
Exiting due to OpenCV error during compute.
尚未找到合适的解决方法,只能考虑换一个模型…
目前我正在入门学习立体匹配算法(SGBM和深度学习方法),不过实际使用下来,在背景不固定的场景下,使用SGBM求深度Z的精度较高,但是视差效果图没有深度学习模型(我实验过RAFT-Stereo和StereoBase)平滑,欢迎一起入门探讨一下,希望大佬们可以指点一下深度学习模型的参数调整,我一直觉得深度学习模型的视差效果图更平滑,得到的深度信息Z应该也是更加精准的。
但目前我用200块买来的双目摄像头,在1.5m左右,使用SGBM求得的误差可以控制在3cm以内,但是采用深度学习模型的误差至少有5cm,我是否需要调整参数?还是需要通过数据集微调训练?
后续
通过github提issue、上网搜、gpt等,总结应该是我在安装opencv-cuda编译时出现的某些错误导致,无奈还是换模型吧,全局立体匹配算法的实时性也不高.
后续换成了StereoSGM的gpu版本,推理速度从1.7ms缩减到1.3ms(cpu和gpu版本对比),有提升但不多,主要是换了模型马上就跑通了。