Applying Median and Wiener Filters

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.PSD for Java 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.PSD provides MedianFilterOptions class to apply filter on a RasterImage. The code snippet provided below demonstrates how to apply median filter to a raster image.

String dataDir = Utils.getDataDir(ApplyMedianAndWienerFilters.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "median_test_denoise_out.gif";
try (Image image = Image.load(sourceFile);
// Cast the image into RasterImage
RasterImage rasterImage = (RasterImage) image) {
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.getBounds(), options);
image.save(destName, new GifOptions());
}

Applying Gauss Wiener Filter

Aspose.PSD 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.

String dataDir = Utils.getDataDir(ApplyGausWienerFilters.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "gauss_wiener_out.gif";
try (Image image = Image.load(sourceFile);
RasterImage rasterImage = (RasterImage) image) {
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.setGrayscale(true);
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image
rasterImage.filter(image.getBounds(), options);
image.save(destName, new GifOptions());
}

Applying Gauss Wiener Filter For Colored image

Aspose.PSD provides GaussWienerFilterOptions for colored images as well.. The code snippet provided below demonstrates how to apply Gauss wiener filter to a color image.

String dataDir = Utils.getDataDir(ApplyGausWienerFiltersForColorImage.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "gauss_wiener_color_out.gif";
try (Image image = Image.load(sourceFile);
// Cast the image into RasterImage
RasterImage rasterImage = (RasterImage) image) {
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.setBrightness(1);
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image
rasterImage.filter(image.getBounds(), options);
image.save(destName, new GifOptions());
}

Applying Motion Wiener Filter

Aspose.PSD 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.

String dataDir = Utils.getDataDir(ApplyMotionWienerFilters.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "motion_filter_out.gif";
try (Image image = Image.load(sourceFile);
// Cast the image into RasterImage
RasterImage rasterImage = (RasterImage) image) {
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.setGrayscale(true);
// Apply MedianFilterOptions filter to RasterImage object and Save the resultant image
rasterImage.filter(image.getBounds(), options);
image.save(destName, new GifOptions());
}

Apply Correction Filter On An Image

This article demonstrates the usage of Aspose.PSD for Java to perform correction filters on an image. Aspose.PSD APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.PSD for Java 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:

  1. Load an image using the factory method Load exposed by Image class.
  2. Convert the image into RasterImage.
  3. Create an instances of BilateralSmoothingFilterOptions and SharpenFilterOptions classes.
  4. Call the RasterImage.Filter method while specifying rectangle as image bounds and BilateralSmoothingFilterOptions class instance.
  5. Call the RasterImage.Filter method while specifying rectangle as image bounds and SharpenFilterOptions class instance.
  6. Adjust the contrast
  7. Set brightness
  8. Save the results.

The following code snippet shows you how to apply correction filter.

try (Image image = Image.load(dataDir + "sample.psd");
// Convert the image into RasterImage.
RasterImage rasterImage = (RasterImage) image) {
if (rasterImage == null) {
return;
}
// Get Bounds[rectangle] of image.
Rectangle rect = image.getBounds();
// Create an instance of BilateralSmoothingFilterOptions class with size
// parameter.
BilateralSmoothingFilterOptions bilateralOptions = new BilateralSmoothingFilterOptions(3);
// Create an instance of SharpenFilterOptions class.
SharpenFilterOptions sharpenOptions = new SharpenFilterOptions();
// Supply the filters to raster image.
rasterImage.filter(rect, bilateralOptions);
rasterImage.filter(rect, sharpenOptions);
// Adjust the contrast accordingly.
rasterImage.adjustContrast(-10);
// Set brightness using Binarize Bradley
rasterImage.binarizeBradley(80);
// Save the results to output path.
rasterImage.save(dataDir + "a1_out.jpg");
}

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.PSD 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.

String dataDir = Utils.getDataDir(Bradleythreshold.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "binarized_out.png";
// Load an image
try (PsdImage image = (PsdImage) Image.load(sourceFile)) {
// Define threshold value, Call BinarizeBradley method and pass the threshold value as parameter and Save the output image
double threshold = 0.15;
image.binarizeBradley(threshold);
image.save(destName, new PngOptions());
}