Question

I am using the Capture class from EmguCV to put the image from the camera on a Texture2D using LoadImage function. Before trying to do this, i was using the SetPixel function but it was too slow.

When I execute the code below, a red question mark on a white background appears instead of the image of the camera.

What am I doing wrong here ?

public class testEmguCV : MonoBehaviour
{
    private Capture capture;

    void Start() 
    {
        capture = new Capture();
    }

    void Update()
    {
        Image<Gray, Byte> currentFrame = capture.QueryGrayFrame();
        Texture2D camera = new Texture2D(400, 400);
        if (currentFrame != null)
        {
            camera.LoadImage(currentFrame.Bytes);
            renderer.material.mainTexture = camera;
        }
     }
}
Was it helpful?

Solution

Here are the modifications I did to correct the problem:

public class testEmguCV : MonoBehaviour
{
    private Capture capture;

    void Start() 
    {
        capture = new Capture();
    }

    void Update()
    {
        Image<Gray, Byte> currentFrame = capture.QueryGrayFrame();
        Bitmap bitmapCurrentFrame = currentFrame.ToBitmap();
        MemoryStream m = new MemoryStream();
        bitmapCurrentFrame.Save(m, bitmapCurrentFrame.RawFormat);

        Texture2D camera = new Texture2D(400, 400);
        if (currentFrame != null)
        {
            camera.LoadImage(m.ToArray());
            renderer.material.mainTexture = camera;
        }
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top