Question

I'm trying to use AForge in Unity and I have trouble converting the input data. I have a 2-dimensional array storing pixel values that I need to convert to UnmanagedImage. I came up with the following code, but I am not sure if its the most efficient way:

img =  UnmanagedImage.Create(sizx,sizy, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

for (int i =0;i<sizx;i++)
    for (int j =0;j<sizy;j++){
        int index = i+j*sizx;
        img.SetPixel(i,j, new AForge.Imaging.RGB(t[index].r, t[index].g, t[index].b).Color); 
}

Any help appreciated!

Était-ce utile?

La solution

I've ended up using unsafe code and accessing the UnmanagedImage.ImageData directly:

unsafe
    {
        byte* dst = (byte*)data.Scan0.ToPointer();


        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++, src++, dst += pixelSize)
            {
                dst[RGB.A] = input[src].A;
                dst[RGB.R] = input[src].R;
                dst[RGB.G] = input[src].G;
                dst[RGB.B] = input[src].B;
            }

            dst += offset;
        }
    }

As it cannot be done in Unity, I had to modify the AForge library.

Autres conseils

If you have your image represented as a byte[] (where each value represents a pixel color value from 0 to 255), then you can use UnmanagedImage.FromByteArray to create an UnmanagedImage from those values.

This method should work on Unity.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top