AForge.NET

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

Write filtered image to frame captured by NewFrame event

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

Write filtered image to frame captured by NewFrame event

Postby lbrg95 » Tue Jan 16, 2018 3:11 pm

Hello, I'm new to AForge and I'm currently trying to capture images from an IP Camera, filter them and display the result in the same camera output:
- I'm using a wrapped VideoSourcePlayer control in a WPF application.
- I'm trying to apply grayscale, threshold euclidean difference, fill holes and remove blobs filters in order to remove background from captured image (using a previously captured background image).

Code: Select all
private Bitmap RemoveBackground(BitmapData frame, Bitmap background, int threshold)
        {
            var grayFilter = Grayscale.CommonAlgorithms.BT709;
            var grayFrame = grayFilter.Apply(frame);

            var diffFilter = new ThresholdedEuclideanDifference(threshold)
            {
                OverlayImage = background
            };

            var mask = diffFilter.Apply(grayFrame);
            var maskFilter = new ApplyMask(mask);

            holeFilter.ApplyInPlace(mask);
            blobsFilter.ApplyInPlace(mask);
            var output = maskFilter.Apply(frame);
            return output;

        }


- What I'm doing in order to display the result in this moment is sending the filtered frame to a WPF Image control in the NewFrame event.
Code: Select all
private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            lastFrame = (Bitmap)image.Clone();
            var newFrame = lastFrame;
            if (RemvBack)
            {
                BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);
                newFrame = RemoveBackground(bitmapData, background, Threshold);
                image.UnlockBits(bitmapData);
            }

            Img.Dispatcher.Invoke(new Action(() =>
            {
                Img.Source = BitmapToImageSource(newFrame);
            })
            );
        }


Img corresponds to the Image WPF control. I know this is not the best desing and I'm having rendering speed issues. What I want to do is displaying the filtered image in the same VideoSourcePlayer control.

The Bitmap to BitmapImage conversion method is the following:

Code: Select all
BitmapImage BitmapToImageSource(Bitmap bitmap)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Bmp);
                memory.Position = 0;
                BitmapImage bitmapimage = new BitmapImage();
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();

                return bitmapimage;
            }
        }


Any advice is welcomed. Thank you in advance.
lbrg95
 
Posts: 2
Joined: Tue Jan 16, 2018 2:59 pm

Re: Write filtered image to frame captured by NewFrame event

Postby andrew.kirillov » Wed Jan 17, 2018 8:00 am

Hello,

Notice that image parameter is passed to NewFrame using the "ref" specifier.
Code: Select all
rivate void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)


So all you need is to perform any image processing you want and then set "image" variable back, so that processed video frame is displayed by Video Source Player.
With best regards,
Andrew


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

Re: Write filtered image to frame captured by NewFrame event

Postby lbrg95 » Wed Jan 17, 2018 7:18 pm

Hi Andrew, thank you very much for your reply. It worked but it seems to be a bit more delayed as before. Do you know how can I speed up the image displaying? I'd tried using UnmanagedImages instead of Bitmap objects when aplying filters and doing the filtering inside a lock block but it hasn't improve very much. Thank you for your time.
lbrg95
 
Posts: 2
Joined: Tue Jan 16, 2018 2:59 pm

Re: Write filtered image to frame captured by NewFrame event

Postby andrew.kirillov » Wed Jan 17, 2018 9:43 pm

Hello,

To understand where the delay comes from, I would recommend doing some profiling. Just measure the time taken by the image processing you do and see if it is significant.
With best regards,
Andrew


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




Return to AForge.NET Framework