Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Red cross on output image in UWP app #25

Closed
Sarnapa opened this issue Apr 4, 2018 · 0 comments
Closed

Red cross on output image in UWP app #25

Sarnapa opened this issue Apr 4, 2018 · 0 comments

Comments

@Sarnapa
Copy link

Sarnapa commented Apr 4, 2018

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:

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 Sarnapa closed this as completed Apr 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant