我设法将EmguCV集成到Unity3D中并编写了一个有一些小问题的转换器
第1步将Unity3D纹理转换为OpenCV图像
public static Image<Bgr, byte> UnityTextureToOpenCVImage(Texture2D tex){
return UnityTextureToOpenCVImage(tex.GetPixels32 (), tex.width, tex.height);
}
public static Image<Bgr, byte> UnityTextureToOpenCVImage(Color32[] data, int width, int height){
byte[,,] imageData = new byte[width, height, 3];
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imageData[x,y,0] = data[index].b;
imageData[x,y,1] = data[index].g;
imageData[x,y,2] = data[index].r;
index++;
}
}
Image<Bgr, byte> image = new Image<Bgr, byte>(imageData);
return image;
}
第2步将OpenCV图像转换回Unity3D纹理
public static Texture2D OpenCVImageToUnityTexture(Image<Bgr, byte> openCVImage, GameObject check){
return OpenCVImageToUnityTexture(openCVImage.Data, openCVImage.Width, openCVImage.Height, check);
}
public static Texture2D OpenCVImageToUnityTexture(byte[,,] data, int width, int height, GameObject check){
Color32 [] imageData = new Color32[width*height];
int index = 0;
byte alpha = 255;
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
imageData[index] = new Color32((data[x,y,2]),
(data[x,y,1]),
(data[x,y,0]),
alpha);
check.SetActive(true);
index++;
}
}
Texture2D toReturn = new Texture2D(width, height, TextureFormat.RGBA32, false);
toReturn.SetPixels32(imageData);
toReturn.Apply ();
toReturn.wrapMode = TextureWrapMode.Clamp;
return toReturn;
}
编译器不会抛出任何错误,但有些错误始终存在。看看自己:cats。 左侧是原始图像,右侧是转换后的图像。你可以看到有更多的猫,那应该是...... 有人有任何线索吗?
由于在所有像素中迭代两次,它也很慢。有没有更好的解决方案?
修改
这是我绘制GUITextures的代码:
public GameObject catGO;
GUITexture guitex;
Texture catTex;
void Start () {
guitex = GetComponent<GUITexture> ();
catTex = catGO.GetComponent<GUITexture> ().texture;
Image<Bgr, byte> cvImage = EmguCVUnityInterop.UnityTextureToOpenCVImage((Texture2D)catTex);
Texture2D converted = EmguCVUnityInterop.OpenCVImageToUnityTexture(cvImage);
guitex.texture = converted;
}
答案 0 :(得分:0)
首先你在2 for for循环中写了check.SetActive(true)所以它的宽度*高度倍。启用GameObject是一项代价高昂的操作。
其次,通过每个像素进行迭代是另一项代价高昂的操作。例如,如果您有图像2560x1600,那么您有超过4百万次迭代。
尝试将TextureWrapMode更改为Repeat(我知道这听起来很愚蠢:])