Jump to content

Slim

Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by Slim

  1. Hi,

    The forum seems not very active. I am also struggling, but maybe what I got so far can help someone.

    The problem with OpenCV is that you need the image to be in CPU memory. So first you need to copy it somehow from GPU.

    I used AsyncGPUReadbackRequest for it. Now I am stuck on how to Apply this modified Texture back to SRWorks.

    Here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Vive.Plugin.SR;
    using OpenCvSharp;
    using UnityEngine.Rendering;
    
    public class OpenCVMat : MonoBehaviour
    {
        Texture2D left;
        public Texture2D leftCPU;
        public Texture2D gray;
        Texture2D right;
        int frameIndex;
        int timeIndex;
        Matrix4x4 poseLeft;
        Matrix4x4 poseRight;
    
        AsyncGPUReadbackRequest request;
        private bool initialized;
    
    
        void OnCompleteReadback(AsyncGPUReadbackRequest request)
        {
            if (request.hasError)
            {
                Debug.Log("GPU readback error detected.");
                return;
            }
    
            var tex = new Texture2D(ViveSR_DualCameraImageCapture.UndistortedImageWidth, ViveSR_DualCameraImageCapture.UndistortedImageHeight, TextureFormat.RGBA32, false);
            tex.LoadRawTextureData(request.GetData<uint>());
            tex.Apply();
            Graphics.CopyTexture(tex, leftCPU);
            Destroy(tex);
        }
    
        void Update()
        {
            if(ViveSR.FrameworkStatus == FrameworkStatus.WORKING)
            {
                if (!initialized)
                {
                    InitTextures();
                    initialized = true;
                }
    
                if(initialized && request.done)
                {
                    Mat mat = OpenCvSharp.Unity.TextureToMat(leftCPU);
                    Mat grayMat = new Mat();
                    Cv2.CvtColor(mat, grayMat, ColorConversionCodes.BGRA2GRAY);
                    gray = OpenCvSharp.Unity.MatToTexture(grayMat);
    
                    grayMat.Dispose();
                    mat.Dispose();
    
                    ViveSR_DualCameraImageCapture.GetUndistortedTexture(out left, out right, out frameIndex, out timeIndex, out poseLeft, out poseRight);
                    request = AsyncGPUReadback.Request(left, 0, TextureFormat.RGBA32, OnCompleteReadback);
                }
            }
        }
    
        void InitTextures()
        {
            left = new Texture2D(ViveSR_DualCameraImageCapture.UndistortedImageWidth, ViveSR_DualCameraImageCapture.UndistortedImageHeight, TextureFormat.RGBA32, false);
            leftCPU = new Texture2D(ViveSR_DualCameraImageCapture.UndistortedImageWidth, ViveSR_DualCameraImageCapture.UndistortedImageHeight, TextureFormat.RGBA32, false);
            gray = new Texture2D(ViveSR_DualCameraImageCapture.UndistortedImageWidth, ViveSR_DualCameraImageCapture.UndistortedImageHeight, TextureFormat.RGBA32, false);
            right = new Texture2D(ViveSR_DualCameraImageCapture.UndistortedImageWidth, ViveSR_DualCameraImageCapture.UndistortedImageHeight, TextureFormat.RGBA32, false);
        }
    }

     

×
×
  • Create New...