Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Image Processing

Ian Auty edited this page Jan 25, 2019 · 11 revisions

Contents

  1. Sharpen
  2. Edge Detection
  3. Gaussian Blur
  4. Strip Bayer metadata

Most of the image processing techniques available currently in MMALSharp are based on Matrix Convolutions which can be found on Wikipedia.

Sharpen

public async Task TakePictureManual()
{                        
    MMALCamera cam = MMALCamera.Instance;

    using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
    using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
    using (var nullSink = new MMALNullSinkComponent())
    {
        cam.ConfigureCameraSettings();
        
        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.RGB24, 90);

        // Create our component pipeline.         
        imgEncoder.ConfigureOutputPort(portConfig);
                
        cam.Camera.StillPort.ConnectTo(imgEncoder);                    
        cam.Camera.PreviewPort.ConnectTo(nullSink);
        
        imgCaptureHandler.Manipulate(context =>
        {
            context.Apply(new SharpenProcessor());
        }, new ImageContext(MMALCameraConfig.StillResolution));
        
        // Camera warm up time
        await Task.Delay(2000);        
        await cam.ProcessAsync(cam.Camera.StillPort);
    }

    // Only call when you no longer require the camera, i.e. on app shutdown.
    cam.Cleanup();
}

Edge Detection

There are 3 modes available which provide different strengths of Edge Detection: EDStrength.Low, EDStrength.Medium and EDStrength.High.

public async Task TakePictureManual()
{                        
    MMALCamera cam = MMALCamera.Instance;

    using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
    using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
    using (var nullSink = new MMALNullSinkComponent())
    {
        cam.ConfigureCameraSettings();
        
        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.RGB24, 90);

        // Create our component pipeline.         
        imgEncoder.ConfigureOutputPort(portConfig);
                
        cam.Camera.StillPort.ConnectTo(imgEncoder);                    
        cam.Camera.PreviewPort.ConnectTo(nullSink);
        
        imgCaptureHandler.Manipulate(context =>
        {
            context.Apply(new EdgeDetection(EDStrength.High));
        }, new ImageContext(MMALCameraConfig.StillResolution));
        
        // Camera warm up time
        await Task.Delay(2000);        
        await cam.ProcessAsync(cam.Camera.StillPort);
    }

    // Only call when you no longer require the camera, i.e. on app shutdown.
    cam.Cleanup();
}

Gaussian Blur

As per the Matrices available on the Wikipedia page, MMALSharp provides functionality for a 3x3 or 5x5 Gaussian Blur matrix convolution.

public async Task TakePictureManual()
{                        
    MMALCamera cam = MMALCamera.Instance;

    using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
    using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
    using (var nullSink = new MMALNullSinkComponent())
    {
        cam.ConfigureCameraSettings();
        
        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.RGB24, 90);

        // Create our component pipeline.         
        imgEncoder.ConfigureOutputPort(portConfig);
                
        cam.Camera.StillPort.ConnectTo(imgEncoder);                    
        cam.Camera.PreviewPort.ConnectTo(nullSink);
        
        imgCaptureHandler.Manipulate(context =>
        {
            context.Apply(new GaussianProcessor(GaussianMatrix.Matrix3x3));
        }, new ImageContext(MMALCameraConfig.StillResolution));
        
        // Camera warm up time
        await Task.Delay(2000);        
        await cam.ProcessAsync(cam.Camera.StillPort);
    }

    // Only call when you no longer require the camera, i.e. on app shutdown.
    cam.Cleanup();
}

Strip Bayer Metadata

Use the following example to get raw access to the Bayer metadata produced with a JPEG frame. Ensure you pass in the correct camera version to the BayerMetaProcessor constructor, either CameraVersion.OV5647 or CameraVersion.IMX219.

public async Task TakePictureManual()
{                        
    MMALCamera cam = MMALCamera.Instance;

    using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
    using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler, true))
    using (var nullSink = new MMALNullSinkComponent())
    {
        cam.ConfigureCameraSettings();
        
        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.RGB24, 90);

        // Create our component pipeline.         
        imgEncoder.ConfigureOutputPort(portConfig);
                
        cam.Camera.StillPort.ConnectTo(imgEncoder);                    
        cam.Camera.PreviewPort.ConnectTo(nullSink);
        
        imgCaptureHandler.Manipulate(context =>
        {
            context.Apply(new BayerMetaProcessor(CameraVersion.OV5647));
        }, new ImageContext(MMALCameraConfig.StillResolution));
        
        // Camera warm up time
        await Task.Delay(2000);        
        await cam.ProcessAsync(cam.Camera.StillPort);
    }

    // Only call when you no longer require the camera, i.e. on app shutdown.
    cam.Cleanup();
}