Applying Median and Wiener Filters - Denoise Image in C#
Applying Median and Wiener Filters
The median filter is a nonlinear digital filtering technique, often used to remove noise. Such noise reduction is a typical pre-processing step to improve the results of later processing. The Wiener filter is the MSE(mean squared error) optimal stationary linearfilter for images degraded by additive noise and blurring. Using Aspose.Imaging for .NET API developers can apply median filter to denoise the image and can apply gauss wiener filter on images. This article demonstrates how median filter and gauss wiener filter can be applied to images.
Applying Median Filter
Aspose.Imaging provides Medianfilteroptions class to apply filter on a RasterImage. The code snippet provided below demonstrates how to apply median filter to a raster image.
using Aspose.Imaging; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load the noisy image | |
using (Image image = Image.Load(dataDir + "template.gif")) | |
{ | |
// Caste the image into RasterImage | |
RasterImage rasterImage = image as RasterImage; | |
if (rasterImage == null) | |
{ | |
return; | |
} | |
// Create an instance of MedianFilterOptions class and set the size, Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
MedianFilterOptions options = new MedianFilterOptions(4); | |
rasterImage.Filter(image.Bounds, options); | |
image.Save(dataDir + "result.gif"); | |
} | |
File.Delete(dataDir + "result.gif"); |
Applying Gauss Wiener Filter
Aspose.Imaging provides GaussWienerFilterOptions class to apply filter on a RasterImage. The code snippet provided below demonstrates how to apply gauss wiener filter to a raster image.
using Aspose.Imaging; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load the image | |
using (Image image = Image.Load(dataDir + "template.jpg")) | |
{ | |
// Caste the image into RasterImage | |
RasterImage rasterImage = image as RasterImage; | |
if (rasterImage == null) | |
{ | |
return; | |
} | |
// Create an instance of GaussWienerFilterOptions class and set the radius size and smooth value. | |
GaussWienerFilterOptions options = new GaussWienerFilterOptions(12, 3); | |
options.Grayscale = true; | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.Filter(image.Bounds, options); | |
image.Save(dataDir + "result.jpg"); | |
} | |
File.Delete(dataDir + "result.jpg"); |
Applying Gauss Wiener Filter For Colored image
using Aspose.Imaging; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load the image | |
using (Image image = Image.Load(dataDir + "template.gif")) | |
{ | |
// Caste the image into RasterImage | |
RasterImage rasterImage = image as RasterImage; | |
if (rasterImage == null) | |
{ | |
return; | |
} | |
// Create an instance of GaussWienerFilterOptions class and set the radius size and smooth value. | |
GaussWienerFilterOptions options = new GaussWienerFilterOptions(5, 1.5); | |
options.Brightness = 1; | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.Filter(image.Bounds, options); | |
image.Save(dataDir + "result.gif"); | |
} | |
File.Delete(dataDir + "result.gif"); |
Applying Motion Wiener Filter
Aspose.Imaging provides MotionWienerFilterOptions class to apply filter on a RasterImage. The code snippet provided below demonstrates how to apply motion wiener filter to a raster image.
using Aspose.Imaging; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load the image | |
using (Image image = Image.Load(dataDir + "template.gif")) | |
{ | |
// Caste the image into RasterImage | |
RasterImage rasterImage = image as RasterImage; | |
if (rasterImage == null) | |
{ | |
return; | |
} | |
// Create an instance of MotionWienerFilterOptions class and set the length, smooth value and angle. | |
MotionWienerFilterOptions options = new MotionWienerFilterOptions(50, 9, 90); | |
options.Grayscale = true; | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.Filter(image.Bounds, options); | |
image.Save(dataDir + "result.gif"); | |
} | |
File.Delete(dataDir + "result.gif"); |
Aligning Horizontal And Vetical Resolutions
This article demonstrates the usage of Aspose.Imaging for .NET to make sure that the horizontal & vetical resolutions of an image are equal while manipulating its properties. Aspose.Imaging API have exposed efficient & easy to use methods to achieve this goal. Aspose.Imaging for .NET has exposed the AlignResolutions method of the Image class for making horizontal & vertical alignment equal. The following code snippet shows you how to use AlignResolutions method.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image and convert the image instance to TiffImage | |
using (TiffImage image = (TiffImage)Image.Load(dataDir + "template.tiff")) | |
{ | |
// Call the align resolution method and Save the results to output path. | |
image.AlignResolutions(); | |
image.Save(dataDir + "result.tiff"); | |
int framesCount = image.Frames.Length; | |
for (int i = 0; i < framesCount; i++) | |
{ | |
TiffFrame frame = image.Frames[i]; | |
// All resolutions after aligment must be equal | |
Console.WriteLine("Horizontal and vertical resolutions are equal:" + ((int)frame.VerticalResolution == (int)frame.HorizontalResolution)); | |
} | |
} | |
File.Delete(dataDir + "result.tiff"); |
Apply Correction Filter On An Image
This article demonstrates the usage of Aspose.Imaging for .NET to perform correction filters on an image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.Imaging for .NET has exposed the BilateralSmoothingFilterOptions and SharpenFilterOptions classes for filtration. BilateralSmoothingFilterOptions class needs an integer as size. The steps to perform Resize are as simple as below:
- Load an image using the factory method Load exposed by Image class.
- Convert the image into RasterImage.
- Create an instances of BilateralSmoothingFilterOptions and SharpenFilterOptions classes.
- Call the RasterImage.Filter method while specifying rectangle as image bounds and BilateralSmoothingFilterOptions class instance.
- Call the RasterImage.Filter method while specifying rectangle as image bounds and SharpenFilterOptions class instance.
- Adjust the contrast
- Set brightness
- Save the results.
The following code snippet shows you how to apply correction filter.
using Aspose.Imaging; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load the image | |
using (Image image = Image.Load(dataDir + "template.gif")) | |
{ | |
// Caste the image into RasterImage | |
RasterImage rasterImage = image as RasterImage; | |
if (rasterImage == null) | |
{ | |
return; | |
} | |
BilateralSmoothingFilterOptions options = new BilateralSmoothingFilterOptions(10); | |
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image | |
rasterImage.Filter(image.Bounds, options); | |
image.Save(dataDir + "result.gif"); | |
} |
Use Bradley threshold algorithm
Image thresholding is used in graphics applications. The goal of thresholding an image is to classify pixels as either “dark” or “light”. Aspose.Imaging API allows you to use Bradley thresholding while converting images. The following code snippet shows you how to define the threshold value and then invoke the Bradley’s threshold algorithm.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an existing image. | |
using (var objimage = (BmpImage)Image.Load(dataDir + "template.bmp")) | |
{ | |
// Define threshold value, Call BinarizeBradley method and pass the threshold value as parameter and Save the output image | |
double threshold = 0.15; | |
objimage.BinarizeBradley(threshold); | |
objimage.Save(dataDir + "result.png"); | |
} | |
File.Delete(dataDir + "result.png"); |