Search code examples
.net-coreemgucv

Read a locally stored image as the background image for subtraction


I'm using Emgu.CV and planning to use background subtraction. I want to do something fairly simple and read two background images from my local disk, and use one of them as the background image and the other one as the overlay to compare with / the mask.

I haven't got far though, because the signature of the method is very different than just accepting a file. I'm guessing I'm missing some conversion from a File.Read to IInputArray


IBackgroundSubtractor backgroundSubtractor = new BackgroundSubtractorMOG2();

IInputArray inputImage; // how do I create an instance of an InputArray from a local file?
IOutputArray mask;

backgroundSubtractor.Apply(imputImage, mask);

How do I go from a file in C:\<somepath>\someimage1.png to formats IInputArray, IOutputArray below?


Solution

  • EmguCv offers different methods to load images from file (see V1 and V2 below). For the mask you just need to define a new Mat object and it will be allocated and filled automatically when you call backgroundSubtractor.Apply(input1, mask);

        //V1 load image
        var input1 = new Mat(@"C:\<somepath>\someimage1.png");
        
        //V2 load image
        Mat input2 = CvInvoke.Imread(@"C:\<somepath>\someimage1.png", ImreadModes.AnyColor);
        
        var mask = new Mat();
        IBackgroundSubtractor backgroundSubtractor = new BackgroundSubtractorMOG2();
        backgroundSubtractor.Apply(input1, mask);
    
    

    The Mat class implements IInputtArray as well as IOutputArray.