Search code examples
c#unity-game-engineaforge

BlobCounter unsupported pixel format


I am getting current exception:

UnsupportedImageFormatException: Unsupported pixel format of the source image.
AForge.Imaging.BlobCounter.BuildObjectsMap (AForge.Imaging.UnmanagedImage image)
AForge.Imaging.BlobCounterBase.ProcessImage (AForge.Imaging.UnmanagedImage image)
AForge.Imaging.BlobCounterBase.ProcessImage (System.Drawing.Imaging.BitmapData imageData)
AForge.Imaging.BlobCounterBase.ProcessImage (System.Drawing.Bitmap image)
cam.blobCounter (System.Drawing.Bitmap videoOutput, AForge.Imaging.BlobCounter bc) (at Assets/Scripts/cam.cs:127)
cam.Update () (at Assets/Scripts/cam.cs:69)

Which is caused by my blobCounter not accepting my current image format. To fix this I used the convertion method: Bitmap yellowClone = AForge.Imaging.Image.Clone(originalBm, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

But I still get the error(Despite trying every format available).

For context, here is my code, with originalFeedTexture being a WebCam feed:

byte[] bytes = originalFeedTexture.EncodeToJPG();
        using (var ms = new MemoryStream(bytes))
        {
            originalBm = new Bitmap(ms);
        }

        Bitmap yellowClone = AForge.Imaging.Image.Clone(originalBm, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

yellow = new Bitmap(yellowClone);
yellowFilter(yellow);
BlobCounter bc = new BlobCounter();
        blobCounter(yellowClone, bc);
        Rectangle[] rects = bc.GetObjectsRectangles();

        if (bc.ObjectsCount >= 1)
        {
            Debug.Log("Swedes");
        }

My yellowFilter function:

 void yellowFilter(Bitmap videoOutput)
    {
        HSLFiltering yellowHslFilter = new HSLFiltering();
        yellowHslFilter.Hue = new IntRange(40, 70);
        yellowHslFilter.Saturation = new DoubleRange(0.3f, 0.9f);
        yellowHslFilter.Luminance = new DoubleRange(0.3f, 0.8f);

        yellowHslFilter.ApplyInPlace(videoOutput);
    }

And my blobCounter function:

void blobCounter(Bitmap videoOutput, BlobCounter bc)
    {
        bc.ObjectsOrder = ObjectsOrder.Size;
        bc.ProcessImage(videoOutput);
    }

EDIT: As I forgot to mention, the error is on following line: blobCounter(yellowClone, bc);


Solution

  • I fixed the issue by changing my version number of AForge.net. I tested on multiple versions, and it seems when using this exact code, the issue only appears on the 2.0.0 version.