- Home /
EmguCv inside Unity
Hey guys, I am using EmguCv wrapper inside Unity to develop some game. Right now, Iam confronted with problem of CONVERTATION IplImage(Image) to Texture2D(unity texture). I've read a lot of posts before and implemented one idea: Unfortunately it workes only for histogram drawing. What I've done there: Firstly, I convert Image to Image, then I got ManagedArray of bytes from Image and set the data to newly created Texture2D:
CvInvoke.cvCvtColor(img.Ptr, copy_img, COLOR_CONVERSION.CV_BGR2RGBA); Array bytes = copy_img.ManagedArray; int h = bytes.GetLength(0); int w = bytes.GetLength(1);
for (int i = 0; i
t.SetPixel(y, x, new UnityEngine.Color((float)r, (float)g, (float)b, (float)a));
} }
Hi, I want to integrate EmguCv in Unity, but I don't know how to do it. I'm new to this all, could you give me a hand? Thanks.
Answer by canert · Jan 10, 2015 at 11:33 AM
Maybe, you can use this code block for taking image to texture..
using UnityEngine;
using System.Collections;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.IO;
private Capture cap;
Texture2D camera;
void Start()
{
cap = new Capture(0);
}
void Update()
{
using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
{
if (nextFrame != null)
{
System.Drawing.Bitmap bitmapCurrentFrame = nextFrame.ToBitmap ();
MemoryStream m = new MemoryStream ();
bitmapCurrentFrame.Save (m, bitmapCurrentFrame.RawFormat);
if(camera!=null){
GameObject.Destroy(camera);
}
camera = new Texture2D (400, 400);
camera.LoadImage (m.ToArray ());
renderer.material.mainTexture = camera;
}
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Unity OpenCV integration 1 Answer
Emgu/OpenCV with Unity Free 1 Answer