1

Topic: Adaptive smooth/sharpen filter

This algorithm will compute a map of all edges in the image and then sharpen the edges and smooth between the edges.
The idea is to enhance edges and reduce noise.

using System;
using MeeSoft.ImageProcessing;
using MeeSoft.ImageProcessing.Operations;


public class ScriptOp : MeeSoft.ImageProcessing.Operations.ScriptOperation.Base
{
    public const float SharpenFilterStrength = 4f;
    
    public const float SharpenFilterSize = 2f;
    
    public const float SmoothFilterSize = 2f;    
        
    BitmapPlane edgeMap;
    
    PlanarBitmap hpBitmap, lpBitmap;
    
    public override void Apply()
    {       
        // Construct edge map
        var vertDisplacementMap = new BitmapPlane();
        var horzDisplacementMap = new BitmapPlane();
        var vertFilter = new FIRFilterOperation();
        vertFilter.Kernel = TextureGenerator.SobelOperatorVert();
        var horzFilter = new FIRFilterOperation();
        horzFilter.Kernel = TextureGenerator.SobelOperatorHorz();                
        var grayPlane = SrcBitmap.ToGrayscalePlane();
        horzFilter.Apply(grayPlane, horzDisplacementMap);
        vertFilter.Apply(grayPlane, vertDisplacementMap);
        edgeMap = EdgeDetectFilter.CreateEdgeMap(horzDisplacementMap, vertDisplacementMap);
        // Smooth edge map
        edgeMap.IIRSmooth(SmoothFilterSize * 2);
        
        // Create sharpened version of the source
        hpBitmap = new PlanarBitmap(SrcBitmap);       
        hpBitmap.IIRSharpen(SharpenFilterStrength, SharpenFilterSize);
        
        // Create smoothed version of the source
        lpBitmap = new PlanarBitmap(SrcBitmap);       
        lpBitmap.IIRSmooth(SmoothFilterSize);        
        
        base.Apply();
    }
    
    protected override float GetPixel(int x, int y, int p, BitmapPlane source)
    {
        // Returned average of the sharpened and smoothed image weighed by the edge map
        return hpBitmap[p][x,y] * edgeMap[x,y] + lpBitmap[p][x,y] * (1-edgeMap[x,y]);
        //return edgeMap[x,y];
    }
}
Michael Vinther

> software developer <