Skip to content
View PeterWindows's full-sized avatar

Block or report PeterWindows

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Maximum 250 characters. Please don’t include any personal information such as legal names or email addresses. Markdown is supported. This note will only be visible to you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
PeterWindows/README.md

双目立体深度测量系统

基于大恒 MER2-302-37GM 网口工业相机 + OpenCVSharp4 的实时深度图采集显示程序。

环境要求

组件 版本
Visual Studio 2022
.NET Framework 4.8
OpenCVSharp4 4.9.x(NuGet)
大恒 Galaxy SDK 最新版(官网下载)

部署步骤

1. 安装大恒 Galaxy SDK

  1. 从大恒官网下载 Galaxy SDK(Windows 版)并安装。
  2. 默认安装路径通常为 C:\Program Files\Daheng Imaging\GalaxySDK\
  3. 将以下文件复制到本项目的 libs\ 目录(手动创建该目录):
    • GxIAPICSharp.dll
    • GxIAPI.dll(运行时依赖,需放在 bin\Debug\bin\Release\,或在系统 PATH 中)

注意:GxIAPI.dll 是 C++ 核心库,需与 GxIAPICSharp.dll 在同一目录,或已在系统 PATH 中。

2. 安装 NuGet 包

在 Visual Studio 程序包管理器控制台 中执行:

Install-Package OpenCvSharp4 -Version 4.9.0.20240103
Install-Package OpenCvSharp4.runtime.win -Version 4.9.0.20240103
Install-Package OpenCvSharp4.Extensions -Version 4.9.0.20240103

3. 配置相机网络

使用大恒 IP 配置工具(GxIPConfig.exe)确保:

  • 左相机 IP:192.168.100.110
  • 右相机 IP:192.168.100.111
  • PC 网口 IP:192.168.100.x(同一网段)
  • 建议将网卡 MTU 设置为 9000(Jumbo Frame),提升大分辨率传输稳定性

4. 编译运行

  1. 用 Visual Studio 2022 打开 StereoDepthViewer.csproj
  2. 选择 Release | AnyCPU 配置。
  3. 构建并运行。

界面说明

┌─────────────────────────────────────────────────────────┬──────────────────┐
│  左相机(校正)    │  右相机(校正)  │  深度图(JET)  │  ▶ 开始采集    │
│                   │                  │                  │  ■ 停止采集    │
│                   │                  │                  │                  │
│  [实时校正图]     │  [实时校正图]    │  [伪彩色深度图] │  SGBM 参数     │
│                   │                  │                  │  ──────────────  │
│                   │                  │  鼠标悬停显示→  │  视差数: 256   │
│                   │                  │  X/Y/Z 坐标(mm) │  块大小: 7     │
│                   │                  │                  │  曝光: 5.0ms   │
│                   │                  │                  │                  │
│                   │                  │                  │  鼠标位置(mm)  │
│                   │                  │                  │  X=... Y=... Z=│
├───────────────────────────────────────────────────────────┤                  │
│  状态: 已连接 | 分辨率: 2048×1536                        │  FPS: 12.3     │
└──────────────────────────────────────────────────────────┴──────────────────┘
  • 左/右相机图:经双目校正后的灰度图(极线对齐)
  • 深度图:JET 伪彩色,蓝色=近,红色=远(无效区域为黑色)
  • 鼠标悬停:移到深度图上,右侧面板实时显示该像素的 X/Y/Z 三维坐标(单位:mm)

标定参数说明

标定参数内置于 StereoProcessor.cs,来自 MATLAB 标定结果:

参数
fx 1794.579 px
fy 1795.301 px
cx 1002.910 px
cy 826.142 px
基线长度 ~521.8 mm
畸变系数 全零(未提供,如有请在 StereoProcessor.cs 中填写)

重要:MATLAB 标定通常同时输出畸变系数(RadialDistortion、TangentialDistortion)。 如果你有这些数值,请在 StereoProcessor.csBuildCameraMatrices() 中填入 _distCoeffs1_distCoeffs2,格式为 [k1, k2, p1, p2, k3]


性能调优

参数 建议 说明
NumDisparities 128~512 越大覆盖距离范围越广,但越慢
BlockSize 5~11(奇数) 越大噪声越小但边缘越模糊
显示分辨率 640×480 MainForm.cs 中的 DisplayWidth/Height
处理帧率 受 CPU 限制 SGBM 为 CPU 密集型,RTX 4070 Ti GPU 加速需使用 CUDA 版 OpenCV

启用 GPU 加速(可选)

OpenCVSharp4 默认使用 CPU。若需利用 RTX 4070 Ti:

  1. 下载含 CUDA 支持的 OpenCV 自行编译版本。
  2. StereoSGBM 替换为 cuda::StereoSGM(需要重写处理器部分)。

文件结构

StereoDepthViewer/
├── StereoDepthViewer.csproj   # VS2022 项目文件
├── Program.cs                  # 程序入口
├── MainForm.cs                 # 主窗体逻辑
├── MainForm.Designer.cs        # UI 布局(自动生成风格)
├── GalaxyCamera.cs             # 大恒相机封装
├── StereoProcessor.cs          # 双目标定/校正/深度计算
├── packages.config             # NuGet 包列表
├── App.config                  # 运行时配置
└── libs/
    ├── GxIAPICSharp.dll        # 大恒 SDK C# 包装(手动复制)
    └── GxIAPI.dll              # 大恒 SDK 核心(手动复制)

Popular repositories Loading

  1. PeterWindows PeterWindows Public

    Config files for my GitHub profile.

    C#

  2. machinelearning-samples.zh-cn machinelearning-samples.zh-cn Public

    Forked from feiyun0112/machinelearning-samples.zh-cn

    ML.NET 示例中文版,英文原版请访问:https://github.com/dotnet/machinelearning-samples

    PowerShell