元语境:
我目前正在开发一款利用opencv替代普通输入(键盘,鼠标等)的游戏.我通过DllImports在C++中使用Unity3D的C#脚本和opencv.我的目标是在我的游戏中创建一个来自opencv的图像.
代码上下文:
正如通常在OpenCV中所做的那样,我使用Mat来表示我的图像.这是我导出图像字节的方式:
cv::Mat _currentFrame;
...
extern "C" byte * EXPORT GetRawImage()
{
return _currentFrame.data;
}
Run Code Online (Sandbox Code Playgroud)
这就是我从C#导入的方式:
[DllImport ("ImageInputInterface")]
private static extern IntPtr GetRawImage ();
...
public static void GetRawImageBytes (ref byte[] result, int arrayLength) {
IntPtr a = GetRawImage ();
Marshal.Copy(a, result, 0, arrayLength);
FreeBuffer(a);
}
Run Code Online (Sandbox Code Playgroud)
从我理解OpenCV的方式来看,我希望在uchar指针中序列化时,以这种方式构造字节数组:
b1, g1, r1, b2, g2, r2, ...
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法将此BGR数组转换为RGB数组:
public static void BGR2RGB(ref byte[] buffer) {
byte swap;
for (int i = 0; i < buffer.Length; i = i + 3) { …
Run Code Online (Sandbox Code Playgroud) 我想在c#中使用c ++代码来使用CLR进行统一.
该程序在统一之外正常工作,但在引擎内部它给我一个错误:
"cs0227:不安全的代码需要指定'unsafe'命令行选项"
我真的很困惑,因为该项目在visual studio中成功构建(没有任何错误或警告).我已激活" 允许 不安全 "按钮.
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class newspawn_real : MonoBehaviour {
void Start () {
unsafe
{
fixed (int * p = &bam[0, 0, 0])
{
CppWrapper.CppWrapperClass controlCpp = new CppWrapper.CppWrapperClass();
controlCpp.allocate_both();
controlCpp.fill_both();
controlCpp.fill_wrapper();
}
Run Code Online (Sandbox Code Playgroud)