传递运行时错误的Unity和OpenCv图像

问题描述

我是一名初学者程序员,将图像从Unity传递到openCv时遇到了一些严重的问题。我正在尝试建立某种模拟,其中将在Unity中生成的图像传递给openCv,在执行某些图像处理后,我想返回的数据将影响自动驾驶机器人。 我遵循了本教程:https://amin-ahmadi.com/2017/05/24/how-to-use-opencv-in-unity-example-project/。我即将完成项目的那部分。我可以将图像从Unity传递到openCv并进行一些操作,但是在行img_orginal.copyTo(img_masked,mask);中出现“运行时错误”。我通过imshow指令测试了.dll,其他类似操作也都正常运行。我怀疑问题与内存处理有关,但我不知道如何解决

统一部件C#代码

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;


public class autonomusDrive : MonoBehavIoUr
{
    public int resWidth = 1920;
    public int resHeight = 1080;
    public GameObject cam;
    public float samplingTime = 0.5f;
    float currentTime;

    void Update()
    {
        currentTime += Time.deltaTime;
        if(currentTime>=samplingTime)
        {
            if (cam.activeInHierarchy)
            { 
                    RenderTexture rt = new RenderTexture(resWidth,resHeight,24,RenderTextureFormat.ARGB32);
                    cam.GetComponent<Camera>().targetTexture = rt;
                    Texture2D screenShot = new Texture2D(resWidth,TextureFormat.RGBA32,false);
                    cam.GetComponent<Camera>().Render();
                    RenderTexture.active = rt;
                    screenShot.ReadPixels(new Rect(0,resWidth,resHeight),0);
                unsafe
                {
                    Color32[] rawImg = screenShot.GetPixels32();                    
                    System.Array.Reverse(rawImg);
                    OpenCVInterop.processImage(rawImg,screenShot.width,screenShot.height);
                }     
            }
            currentTime = 0;
        }
    }
}

internal static class OpenCVInterop
{
    [DllImport("RunningRobotSimulationopenCVPart")]
    public unsafe static extern void processImage(Color32[] raw,int width,int height);
}

OpenCv C部分代码

#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>


using namespace std;
using namespace cv;

struct Color32
{
    uchar r;
    uchar g;
    uchar b;
    uchar a;
};

extern "C" void __declspec(dllexport) __stdcall processImage(Color32* raw,int height)
    {
        Mat frame(height,width,CV_8UC4,raw);
        imshow("frame",frame);
        Mat img_orginal;
        cvtColor(frame,img_orginal,COLOR_BGRA2RGB);
        imshow("img_orginal",img_orginal);
        cvtColor(img_orginal,COLOR_BGR2GRAY);
        imshow("img_gray",img_orginal);
        
        //creating ROI mask
        int img_X = img_orginal.cols;
        int img_Y = img_orginal.rows;
        Mat mask(img_Y,img_X,CV_8UC3,Scalar(0,0));
        Point points[6] = { Point(0,img_Y),Point(img_X,img_Y * 2 / 3),Point(img_X * 2 / 3,img_Y * 4 / 7),Point(img_X * 1 / 3,Point(0,img_Y * 2 / 3) };
        fillConvexpoly(mask,points,6,Scalar(255,255,255),8,0);
        namedWindow("Mask",WINDOW_AUTOSIZE);       
        imshow("Mask",mask);
        Mat img_masked = img_orginal.clone();
        namedWindow("cloned",WINDOW_AUTOSIZE);     
        imshow("cloned",mask);
        img_orginal.copyTo(img_masked,mask);       //THERE IS PROBLEM 
        imshow("frame",img_orginal);
}

我附上错误屏幕(可见一些用于查找此错误原因的窗口) enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...