1

Topic: Close holes marked by selection

Close selected "holes" by averaging edge pixels

// Close holes marked by selection

using MeeSoft.ImageProcessing;
using MeeSoft.Mathematics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public class ScriptOp : MeeSoft.ImageProcessing.Operations.ScriptOperation.Base
{
    const double Pow = 2;

    BitmapPlane _mask;

    public override void Apply()
    {
        _mask = GetMask("*");

        DstWidth = SrcBitmap.Width;
        DstHeight = SrcBitmap.Height;

        // Call base class Apply to run ApplyToPlane on all planes
        base.Apply();
    }

    struct Point
    {
        public int X, Y;
    }

    protected override void ApplyToPlane(BitmapPlane srcPlane, BitmapPlane dstPlane, int planeIndex)
    {
        // Trace edges
        var edge = new List<Point>();
        for (int y=0; y<DstHeight; y++)
            for (int x=0; x<DstWidth; x++)
                if (_mask[x,y] == 0 &&
                    (_mask.GetPixelSafe(x,y-1) != 0 ||
                     _mask.GetPixelSafe(x-1,y) != 0 ||
                     _mask.GetPixelSafe(x+1,y) != 0 ||
                     _mask.GetPixelSafe(x,y+1) != 0))
                    edge.Add(new Point() { X = x, Y = y });

        // Synthesize pixels
        Parallel.For(0, DstHeight, y=>
        {
            for (int x=0; x<DstWidth; x++)
                if (_mask[x,y] != 0)
                {
                    float sum=0, wSum = 0;
                    for (int i=0; i<edge.Count; i++)
                    {
                        float w = (float)(1 / RealMath.Pow(IntMath.Sqr(x - edge[i].X) + 
                                                           IntMath.Sqr(y - edge[i].Y), Pow));
                        sum += srcPlane[edge[i].X,edge[i].Y] * w;
                        wSum += w;
                    }
                    dstPlane[x,y] = sum/wSum;
                }
                else
                    dstPlane[x,y] = srcPlane[x,y];
         });
    }
}
Michael Vinther

> software developer <