본문 바로가기
programming | development/unity

Unity + Realsense F200

by foooo828 2022. 6. 14.

뎁스이미지 표시하기

1. Intel® RealSense™ Depth Camera Manager 설치

가장 최근 업데이트된 RealsenseSDK 2.0 는 F200를 지원 안함

** DCM 설치 후 SDK를 설치

https://www.intel.com/content/www/us/en/download/18309/intel-realsense-depth-camera-manager.html

2. Realsense 2016 R2 설치

R3은 모듈식, R2는 sdk 전체 설치 이므로 R2를 권장

2016 R2는 더이상 공홈에서 제공하지 않으므로 아래 다이렉트 링크에서 다운로드

Realsense 2016 R2 direct link (좌클릭으로 열기가 안되므로 복사해서 주소창에 넣어야 다운이 가능함)

http://registrationcenter-download.intel.com/akdlm/irc_nas/vcp/9078/intel_rs_sdk_offline_package_10.0.26.0396.exe

 

3. Unity dll 파일

C:\Program Files (x86)\Intel\RSSDK\framework\Unity\UnityToolkit\Assets

sdk 설치경로로 가서 유니티툴킷 찾기

에셋폴더의 Plugin, Plugin.Manages 폴더를 복사하여 본인 프로젝트 에셋 폴더에 붙여넣기

 

4. 코드작성

흐름

1. 초기화처리

사용할 스트림 함수를 활성화

 SDK  초기화

스트림과 함수 설정

 

2. 데이터 업데이트 처리

스트림과 함수 데이터 업데이트, 이용, 표시

 

3. 종료처리 

SDK 종료

using System;
using UnityEngine;

public class SensorManager : MonoBehaviour
{
    [Range(200, 1200)] [Header("Recognition Range : 200mm ~ 1200mm")]
    public int distance = 500;

    private PXCMSenseManager senseManager;
    private int _depthWidth = 640;
    private int _depthHeight = 480;
    private int _depthFps = 30;
    private pxcmStatus _status;
    private short[] _depthBuffer;

    private Texture2D _depthViewerTex;
    public Material depthViewerMat;


    private void Start()
    {
        Initialize();
        _depthViewerTex = SetTexture(_depthViewerTex, TextureFormat.R8);
    }

    private void Update()
    {
        UpdateFrame();
    }

    Texture2D SetTexture(Texture2D texture, TextureFormat format)
    {
        texture = new Texture2D(_depthWidth, _depthHeight, format, false);
        texture.wrapMode = TextureWrapMode.Clamp;
        texture.filterMode = FilterMode.Point;
        return texture;
    }

    void Initialize()
    {
        // 센스 매니저 인스턴스 생성
        senseManager = PXCMSenseManager.CreateInstance();

        // 뎁스 스트림 활성화
        _status = senseManager.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, _depthWidth, _depthHeight,
            _depthFps);
        if (_status < pxcmStatus.PXCM_STATUS_NO_ERROR)
        {
            throw new Exception("뎁스 스트림 활성화 실패");
        }

        // SDK 초기화
        _status = senseManager.Init();
        if (_status < pxcmStatus.PXCM_STATUS_NO_ERROR)
        {
            throw new Exception("초기화 실패");
        }

        senseManager.QueryCaptureManager().QueryDevice();
    }

    void UpdateFrame()
    {
        // 프레임 가져오기
        pxcmStatus ret = senseManager.AcquireFrame(true);
        if (ret < pxcmStatus.PXCM_STATUS_NO_ERROR)
        {
            return;
        }

        // 프레임데이터 가져오기
        PXCMCapture.Sample sample = senseManager.QuerySample();

        if (sample != null)
        {
            UpdateDepthData(sample.depth);
        }
        else
        {
            Debug.Log("sample is null");
        }

        senseManager.ReleaseFrame();
    }

    private void UpdateDepthData(PXCMImage depthFrame)
    {
        if (depthFrame == null) return;
        pxcmStatus sts = depthFrame.AcquireAccess(PXCMImage.Access.ACCESS_READ,
            PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out PXCMImage.ImageData data);

        if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
        {
            throw new Exception("Depth 이미지 가져오기 실패");
        }

        // Depth데이터 가져오기
        var info = depthFrame.QueryInfo();
        var length = info.width * info.height;
        _depthBuffer = data.ToShortArray(0, length);

        byte[] pixel = new byte[length];

        // 설정한 거리 범위 내의 값만 바꿔줌
        for (int i = 0; i < length; i++)
        {
            pixel[i] = (byte) ((_depthBuffer[i] > 200 && _depthBuffer[i] < distance) ? 255 : 0);
        }

        //텍스처 만들기
        _depthViewerTex.LoadRawTextureData(pixel);
        _depthViewerTex.Apply();

        depthViewerMat.mainTexture = _depthViewerTex;

        //데이터 해제
        depthFrame.ReleaseAccess(data);
    }
}

 

참고 : 인텔 리얼센스 SDK 센서 프로그래밍

'programming | development > unity' 카테고리의 다른 글

Unity AnimationCurve 활용  (0) 2023.04.18
삼각함수로 원 그리기  (0) 2022.07.15
HDRP Raytracing 세팅  (0) 2022.03.14
Unity + OpenCV OpticalFlowPyrLK + Visual Effect Graph  (0) 2022.03.10
Component Inspector 항목들  (0) 2022.02.26

댓글