|
| 1 | +package com.aspose.imaging.examples.ModifyingImages; |
| 2 | + |
| 3 | +import com.aspose.imaging.Image; |
| 4 | +import com.aspose.imaging.RasterImage; |
| 5 | +import com.aspose.imaging.VectorImage; |
| 6 | +import com.aspose.imaging.examples.Logger; |
| 7 | +import com.aspose.imaging.examples.Utils; |
| 8 | +import com.aspose.imaging.imagefilters.complexutils.Complex; |
| 9 | +import com.aspose.imaging.imagefilters.convolution.ConvolutionFilter; |
| 10 | +import com.aspose.imaging.imagefilters.filteroptions.*; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Random; |
| 16 | + |
| 17 | +public class KernelFilters |
| 18 | +{ |
| 19 | + public static void main(String[] args) |
| 20 | + { |
| 21 | + Logger.startExample(); |
| 22 | + |
| 23 | + final int Size = 5; |
| 24 | + final double Sigma = 1.5, Angle = 45; |
| 25 | + |
| 26 | + double[][] customKernel = getRandomKernel(Size, 7, new Random()); |
| 27 | + Complex[][] customComplex = ConvolutionFilter.toComplex(customKernel); |
| 28 | + FilterOptionsBase[] kernelFilters = new FilterOptionsBase[] |
| 29 | + { |
| 30 | + // convolution filters |
| 31 | + new ConvolutionFilterOptions(ConvolutionFilter.getEmboss3x3()), |
| 32 | + new ConvolutionFilterOptions(ConvolutionFilter.getEmboss5x5()), |
| 33 | + new ConvolutionFilterOptions(ConvolutionFilter.getSharpen3x3()), |
| 34 | + new ConvolutionFilterOptions(ConvolutionFilter.getSharpen5x5()), |
| 35 | + new ConvolutionFilterOptions(ConvolutionFilter.getBlurBox(Size)), |
| 36 | + new ConvolutionFilterOptions(ConvolutionFilter.getBlurMotion(Size, Angle)), |
| 37 | + new ConvolutionFilterOptions(ConvolutionFilter.getGaussian(Size, Sigma)), |
| 38 | + new ConvolutionFilterOptions(customKernel), |
| 39 | + new GaussianBlurFilterOptions(Size, Sigma), |
| 40 | + new SharpenFilterOptions(Size, Sigma), |
| 41 | + new MedianFilterOptions(Size), |
| 42 | + // deconvolution filters |
| 43 | + new DeconvolutionFilterOptions(ConvolutionFilter.getGaussian(Size, Sigma)), |
| 44 | + new DeconvolutionFilterOptions(customKernel), |
| 45 | + new DeconvolutionFilterOptions(customComplex), |
| 46 | + new GaussWienerFilterOptions(Size, Sigma), |
| 47 | + new MotionWienerFilterOptions(Size, Sigma, Angle), |
| 48 | + }; |
| 49 | + |
| 50 | + // The path to the documents' directory. |
| 51 | + String dataDir = Utils.getSharedDataDir() + "Png/"; |
| 52 | + String outDir = Utils.getOutDir("Png/"); |
| 53 | + String[] inputPaths = |
| 54 | + { |
| 55 | + "template.png" |
| 56 | + }; |
| 57 | + |
| 58 | + List<String> outputs = new ArrayList<>(); |
| 59 | + for (String inputPath : inputPaths) |
| 60 | + { |
| 61 | + for (int i = 0; i < kernelFilters.length; i++) |
| 62 | + { |
| 63 | + FilterOptionsBase options = kernelFilters[i]; |
| 64 | + try (Image image = Image.load(dataDir + inputPath)) |
| 65 | + { |
| 66 | + String outputPath = String.format("%s%c%s-%d.png", outDir, File.separatorChar, inputPath, i); |
| 67 | + |
| 68 | + if (image instanceof RasterImage) |
| 69 | + { |
| 70 | + filter((RasterImage) image, options, outputPath); |
| 71 | + } |
| 72 | + else if (image instanceof VectorImage) |
| 73 | + { |
| 74 | + String vectorAsPng = inputPath + ".png"; |
| 75 | + if (!new File(vectorAsPng).exists()) |
| 76 | + { |
| 77 | + image.save(vectorAsPng); |
| 78 | + outputs.add(vectorAsPng); |
| 79 | + } |
| 80 | + |
| 81 | + try (Image png = Image.load(vectorAsPng)) |
| 82 | + { |
| 83 | + filter((RasterImage) png, options, outputPath); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + outputs.forEach(p -> new File(p).delete()); |
| 91 | + |
| 92 | + Logger.endExample(); |
| 93 | + } |
| 94 | + |
| 95 | + static void filter(RasterImage raster, FilterOptionsBase options, String outputPath) |
| 96 | + { |
| 97 | + raster.filter(raster.getBounds(), options); |
| 98 | + raster.save(outputPath); |
| 99 | + } |
| 100 | + |
| 101 | + static double[][] getRandomKernel(int cols, int rows, Random random) |
| 102 | + { |
| 103 | + double[][] customKernel = new double[cols][rows]; |
| 104 | + for (int y = 0; y < customKernel.length; y++) |
| 105 | + { |
| 106 | + for (int x = 0; x < customKernel[0].length; x++) |
| 107 | + { |
| 108 | + customKernel[y][x] = random.nextDouble(); |
| 109 | + } |
| 110 | + } |
| 111 | + return customKernel; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments