C# OpencvSharp获取Astra Pro奥比中光深度相机深度图

在某鱼花了90RMB,买了个Astra Pro奥比中光深度相机,性价比还是比较高的。通过封装官方的SDK,导出为dll给C#调用,效果如下

///////////AstraCamera.cs////////////

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Drawing;
using OpenCvSharp;
using OpenCvSharp.Extensions;

public class AstraCamera : IDisposable
{
    // DLL导入
    [DllImport("ColorReaderEventCPP.dll", CallingConvention = CallingConvention.StdCall)]
    private static extern void StartCamera();

    [DllImport("ColorReaderEventCPP.dll", CallingConvention = CallingConvention.StdCall)]
    private static extern void StopCamera();

    [DllImport("ColorReaderEventCPP.dll", CallingConvention = CallingConvention.StdCall)]
    private static extern bool GetLatestFrames(
        IntPtr rawDepthData, int rawDepthSize,
        IntPtr depthData, int depthSize,
        IntPtr colorData, int colorSize);

    [DllImport("ColorReaderEventCPP.dll", CallingConvention = CallingConvention.StdCall)]
    private static extern void GetFrameInfo(
        out int rawDepthWidth, out int rawDepthHeight, out int rawDepthType,
        out int depthWidth, out int depthHeight, out int depthType,
        out int colorWidth, out int colorHeight, out int colorType);

    // 图像属性
    public int RawDepthWidth;
    public int RawDepthHeight;
    public MatType RawDepthType { get; private set; }

    public int DepthWidth;
    public int DepthHeight;
    public MatType DepthType { get; private set; }

    public int ColorWidth;
    public int ColorHeight;
    public MatType ColorType { get; private set; }

    // 非托管内存缓冲区
    private IntPtr _rawDepthBuffer;
    private IntPtr _depthBuffer;
    private IntPtr _colorBuffer;
    private int _rawDepthBufferSize;
    private int _depthBufferSize;
    private int _colorBufferSize;

    // 当前帧
    public Mat RawDepthFrame { get; private set; }  // 原始深度图 (16位)
    public Mat DepthFrame { get; private set; }     // 伪彩色深度图
    public Mat ColorFrame { get; private set; }     // 彩色图

    // 更新事件
    public event Action FramesUpdated;

    // 后台线程
    private Thread _updateThread;
    private bool _isRunning;

    public AstraCamera()
    {
        // 获取帧信息
        GetFrameInfo(out RawDepthWidth, out RawDepthHeight, out int rawDepthType,
                    out DepthWidth, out DepthHeight, out int depthType,
                    out ColorWidth, out ColorHeight, out int colorType);

        // 转换为MatType
        RawDepthType = (MatType)rawDepthType;
        DepthType = (MatType)depthType;
        ColorType = (MatType)colorType;

        // 计算缓冲区大小
        _rawDepthBufferSize = RawDepthWidth * RawDepthHeight * sizeof(u

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值