AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Red cross on output image in UWP app

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Red cross on output image in UWP app

Postby Sarnapa » Sun Apr 08, 2018 10:51 am

To be brief, I'm trying to implement some motion detection algorithm and in my case I'm working on UWP using portable version of AForge library to processing image. Avoiding this issue, I have problem with conversion SoftwareBitmap object (that I get from MediaFrameReader) to Bitmap object (and vice versa) that I use in my code associated with motion detection. In consequence of this conversion, I get proper image with big red cross in the foreground. Code below:

Code: Select all
private async void FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
    var frame = sender.TryAcquireLatestFrame();
    if (frame != null && !_detectingMotion)
    {
        SoftwareBitmap aForgeInputBitmap = null;
        var inputBitmap = frame.VideoMediaFrame?.SoftwareBitmap;
         if (inputBitmap != null)
         {
             _detectingMotion = true;
             //The XAML Image control can only display images in BRGA8 format with premultiplied or no alpha
             if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8
                 && inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied)
             {
                 aForgeInputBitmap = SoftwareBitmap.Copy(inputBitmap);
             }
             else
             {
                 aForgeInputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
             }
             await _aForgeHelper.MoveBackgrounds(aForgeInputBitmap);
             SoftwareBitmap aForgeOutputBitmap = await _aForgeHelper.DetectMotion();
             _frameRenderer.PresentSoftwareBitmap(aForgeOutputBitmap);
             _detectingMotion = false;
         }
    }
}

class AForgeHelper
{
    private Bitmap _background;
    private Bitmap _currentFrameBitmap;

    public async Task MoveBackgrounds(SoftwareBitmap currentFrame)
    {
        if (_background == null)
        {
            _background = TransformToGrayscale(await ConvertSoftwareBitmapToBitmap(currentFrame));
        }
        else
        {
            // modifying _background in compliance with algorithm - in this case irrelevant
        }
    }

    public async Task<SoftwareBitmap> DetectMotion()
    {
        // to check only this conversion
        return await ConvertBitmapToSoftwareBitmap(_background);
    }

    private static async Task<Bitmap> ConvertSoftwareBitmapToBitmap(SoftwareBitmap input)
    {
        Bitmap output = null;
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            WriteableBitmap tmpBitmap = new WriteableBitmap(input.PixelWidth, input.PixelHeight);
            input.CopyToBuffer(tmpBitmap.PixelBuffer);
            output = (Bitmap)tmpBitmap;
        });
        return output;
    }

    private static async Task<SoftwareBitmap> ConvertBitmapToSoftwareBitmap(Bitmap input)
    {
        SoftwareBitmap output = null;
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            WriteableBitmap tmpBitmap = (WriteableBitmap)input;
            output = new SoftwareBitmap(BitmapPixelFormat.Bgra8, tmpBitmap.PixelWidth, tmpBitmap.PixelHeight,
                                        BitmapAlphaMode.Premultiplied);
            output.CopyFromBuffer(tmpBitmap.PixelBuffer);
        });
        return output;
    }

    private static Bitmap TransformToGrayscale(Bitmap input)
    {
        Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
        Bitmap output = grayscaleFilter.Apply(input);
        return output;
    }
}


Certainly, I've tried to detect some errors using try-catch clauses. I've found nothing. But it seems to me that this problem is caused by casting WriteableBitmap to Bitmap and at this point I want ask if during this procedure it is applied special watermark in the shape of red cross. Thanks in advance for help.
Sarnapa
 
Posts: 4
Joined: Sun Apr 08, 2018 10:47 am

Re: Red cross on output image in UWP app

Postby andrew.kirillov » Mon Apr 09, 2018 1:32 am

Red cross means exception in some part of your code.

Casting WriteableBitmap to Bitmap will certainly generate you an exception. Check MSDN documentation - WriteableBitmap has no relation to Bitmp and can not be casted.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3280
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Red cross on output image in UWP app

Postby Sarnapa » Mon Apr 09, 2018 9:53 am

Okay, but it is possible to convert SoftwareBitmap to Bitmap (and vice versa) in some way? And first of all, thanks for the answer.
Sarnapa
 
Posts: 4
Joined: Sun Apr 08, 2018 10:47 am

Re: Red cross on output image in UWP app

Postby andrew.kirillov » Mon Apr 09, 2018 2:43 pm

Sarnapa wrote:Okay, but it is possible to convert SoftwareBitmap to Bitmap (and vice versa) in some way?

Yes, but you will need to write some code fo this. If both SoftwareBitmap (or whatever meant under this) and Bitmap classes provide access to pixel data, then you can always make some functions to convert one to another.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3280
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Red cross on output image in UWP app

Postby Sarnapa » Mon Apr 09, 2018 8:59 pm

Unfortunately, I have little problem that PCL version of AForge.NET Framework facilitates only cast operator that enables to create System.Drawing.Bitmap object. Another option is using Bitmap.FromStream() method but it returns System.Drawing.Image object and casting it to Bitmap ends up in the same way like my previous attempts, in other words with red cross on my output image. What is more, I've not found method from Bitmap class that provides me possibility to create Bitmap object using pixel data in the shape of bytes array.

Sorry for the trouble but I cannot solve this problem.
Sarnapa
 
Posts: 4
Joined: Sun Apr 08, 2018 10:47 am

Re: Red cross on output image in UWP app

Postby Sarnapa » Thu Apr 12, 2018 3:40 pm

Problem solved - topic may be closed.
Sarnapa
 
Posts: 4
Joined: Sun Apr 08, 2018 10:47 am




Return to AForge.NET Framework

cron