Zastosowanie filtrów medianowych i Wienera
Zastosowanie filtrów medianowych i Wienera
Filtr medianowy to nieliniowa technika filtrowania cyfrowego, często używana do usuwania szumów. Takie zmniejszenie szumów jest typowym krokiem wstępnym w celu poprawy wyników późniejszej obróbki. Filtr Wienera jest optymalnym filtrem MSE (błędu średniokwadratowego) do filtracji liniowej obrazów zdegradowanych przez szum dodawany i zamazanie. Korzystając z interfejsu API Aspose.PSD dla Javy, programiści mogą zastosować filtr medianowy w celu usunięcia szumów z obrazu oraz zastosować filtr Gaussa Wienera na obrazach. Ten artykuł demonstrowe, jak zastosować filtr medianowy i filtr Gaussa Wienera do obrazów.
Zastosowanie Filtra Medianowego
Aspose.PSD udostępnia klasę MedianFilterOptions do zastosowania filtru na RasterImage. Poniższy fragment kodu demonstruje, jak zastosować filtr medianowy do obrazu rastrowego.
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()); | |
} |
Zastosowanie Filtra Gaussa Wienera
Aspose.PSD udostępnia klasę GaussWienerFilterOptions do zastosowania filtru na RasterImage. Poniższy fragment kodu demonstruje, jak zastosować filtr Gaussa Wienera do obrazu rastrowego.
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()); | |
} |
Zastosowanie Filtra Gaussa Wienera do Obrazu Kolorowego
Aspose.PSD udostępnia klasę GaussWienerFilterOptions także do obrazów kolorowych. Poniższy fragment kodu demonstruje, jak zastosować filtr Gaussa Wienera do obrazu kolorowego.
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()); | |
} |
Zastosowanie Filtra Wienera Ruchu
Aspose.PSD udostępnia klasę MotionWienerFilterOptions do zastosowania filtru na RasterImage. Poniższy fragment kodu demonstruje, jak zastosować filtr Wienera ruchu do obrazu rastrowego.
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()); | |
} |
Zastosowanie Filtra Korekcyjnego Na Obrazie
Ten artykuł demonstruje użycie Aspose.PSD dla Javy do stosowania filtrów korekcyjnych na obrazie. API Aspose.PSD udostępnia wydajne i łatwe w użyciu metody do osiągnięcia tego celu. Aspose.PSD dla Javy udostępnia klasy BilateralSmoothingFilterOptions i SharpenFilterOptions do filtrowania. Klasa BilateralSmoothingFilterOptions wymaga podania liczby całkowitej jako rozmiaru. Kroki do wykonania zmiany rozmiaru są proste, jak poniżej:
- Załaduj obraz za pomocą metody fabrycznej Load udostępnionej przez klasę Image.
- Konwertuj obraz na RasterImage.
- Utwórz instancje klas BilateralSmoothingFilterOptions i SharpenFilterOptions.
- Wywołaj metodę Filter klasy RasterImage, podając prostokąt jako granice obrazu i instancję klasy BilateralSmoothingFilterOptions.
- Wywołaj metodę Filter klasy RasterImage, podając prostokąt jako granice obrazu i instancję klasy SharpenFilterOptions.
- Dostosuj kontrast.
- Ustaw jasność.
- Zapisz wyniki.
Poniższy fragment kodu pokazuje, jak zastosować filtr korekcyjny.
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"); | |
} |
Użyj algorytmu progowania Bradley’a
Progowanie obrazu jest używane w aplikacjach graficznych. Celem progowania obrazu jest sklasyfikowanie pikseli jako „ciemne” lub „jasne”. Interfejs API Aspose.PSD pozwala na użycie progowania Bradley’a podczas konwertowania obrazów. Poniższy fragment kodu pokazuje, jak zdefiniować wartość progu, a następnie wywołać algorytm progowania Bradley’a.
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()); | |
} |