This document is archived and information here might be outdated.  Recommended version.


How to create a custom convolution filter (ArcObjects .NET 10.5 SDK)

How to create a custom convolution filter


Creating a custom convolution filter

To create a custom convolution filter by specifying a kernel, see the following code example:
[C#]
public static void CustomConvolutionFilter(IRaster raster)
{
    //The following code sample sets up a horizontal line detection filter and applies it to a raster
    //to create a convolution filter using a custom kernel.
    IRasterConvolutionFilter newFilter=new RasterConvolutionFilterClass();
    newFilter.SetSize(3, 3);
    double[] kernel=new double[9]
    {
         - 1,  - 1,  - 1, 2, 2, 2,  - 1,  - 1,  - 1
    };
    newFilter.PutCoefficients(ref kernel);

    //Apply the filter to the raster.
    IPixelOperation pixelOperation=(IPixelOperation)raster;
    pixelOperation.PixelFilter=(IPixelFilter)newFilter;

    //Save the result.
    ISaveAs saveas=(ISaveAs)raster;
    saveas.SaveAs(@"c:\temp\convolution_output.img", null, "IMAGINE Image");
}
[VB.NET]
Public Shared Sub CustomConvolutionFilter(ByVal raster As IRaster)
'The following code sample sets up a horizontal line detection filter and applies it to a raster
'to create a convolution filter using a custom kernel.

'Define the kernal
Dim NewFilter As IRasterConvolutionFilter=New RasterConvolutionFilterClass()
NewFilter.SetSize(3, 3);
Dim kernel() As Double=New Double(9) {-1, -1, -1, 2, 2, 2, -1, -1, -1}
NewFilter.PutCoefficients( kernel)

'Apply the filter to the raster.
Dim pixelOperation As IPixelOperation=CType(raster, IPixelOperation)
pixelOperation.PixelFilter=CType(NewFilter, IPixelFilter)

'Save the result.
Dim saveas As ISaveAs=CType(raster, ISaveAs)
saveas.SaveAs("c:\temp\convolution_output.img", Nothing, "IMAGINE Image")
End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Basic ArcGIS Desktop Basic
Engine Developer Kit Engine