static { g = new float[3][3]; sum = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int x = i - 1; int y = j - 1; g[i][j] = (float) Math.exp(-(x * x + y * y) / (2.0 * 0.8)); sum += g[i][j]; } } sum1 = g[0][0] + g[0][1] + g[0][2] + g[1][0] + g[1][1] + g[1][2]; }
/** * Make a Gaussian blur kernel. * * @param radius the blur radius * @return the kernel */ public static Kernel makeKernel(float radius) { int r = (int) Math.ceil(radius); int rows = r * 2 + 1; float[] matrix = new float[rows]; float sigma = radius / 3; float sigma22 = 2 * sigma * sigma; float sigmaPi2 = 2 * ImageMath.PI * sigma; float sqrtSigmaPi2 = (float) Math.sqrt(sigmaPi2); float radius2 = radius * radius; float total = 0; int index = 0; for (int row = -r; row <= r; row++) { float distance = row * row; if (distance > radius2) matrix[index] = 0; else matrix[index] = (float) Math.exp(-(distance) / sigma22) / sqrtSigmaPi2; total += matrix[index]; index++; } for (int i = 0; i < rows; i++) matrix[i] /= total; return new Kernel(rows, 1, matrix); }
public static WritableRaster imgblur(WritableRaster img, int rad, double var) { int h = img.getHeight(), w = img.getWidth(); double[] gk = new double[(rad * 2) + 1]; for (int i = 0; i <= rad; i++) gk[rad + i] = gk[rad - i] = Math.exp(-0.5 * Math.pow(i / var, 2.0)); double s = 0; for (double cw : gk) s += cw; s = 1.0 / s; for (int i = 0; i <= rad * 2; i++) gk[i] *= s; int[] buf = new int[w * h]; for (int band = 0; band < img.getNumBands(); band++) { int o; o = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double v = 0; int l = Math.max(0, x - rad), r = Math.min(w - 1, x + rad); for (int x2 = l, ks = l - (x - rad); x2 <= r; x2++, ks++) v += img.getSample(x2, y, band) * gk[ks]; buf[o++] = (int) v; } } img.setSamples(0, 0, w, h, band, buf); o = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double v = 0; int u = Math.max(0, y - rad), b = Math.min(h - 1, y + rad); for (int y2 = u, ks = u - (y - rad); y2 <= b; y2++, ks++) v += img.getSample(x, y2, band) * gk[ks]; buf[o++] = (int) v; } } img.setSamples(0, 0, w, h, band, buf); } return (img); }
/** * Applies a gaussian blur filter to the given image. Apart from the filter radius, you can also * specify an alpha factor which will be multiplied with the filter's result. Also, you can * specify whether the blurred image should be rendered into a newly created BufferedImage * instance or into the original image. If you request a new image instance, the result will be * larger than the original one as a (2*filterradius) pixel wide padding will be applied. * * @param image the image to be blurred. * @param filterRadius the radius of the gaussian filter to apply. The corresponding kernel will * be sized 2 * filterRadius + 1; * @param alphaFactor a factor which will be multiplied with the filtered image. You can use this * parameter to weaken or strengthen the colors in the blurred image. * @param useOriginalImageAsDestination Determines whether the blur result should be rendered into * the original image or into a new image instance. If you choose to create a new image * instance, the result will be larger than the original image to provide the required padding * for the blur effect. * @return An image instance containing a blurred version of the given image. */ public static BufferedImage applyGaussianBlur( final BufferedImage image, final int filterRadius, final float alphaFactor, final boolean useOriginalImageAsDestination) { if (filterRadius < 1) { throw new IllegalArgumentException( "Illegal filter radius: expected to be >= 1, was " + filterRadius); } float[] kernel = new float[2 * filterRadius + 1]; final float sigma = filterRadius / 3f; final float alpha = 2f * sigma * sigma; final float rootAlphaPI = (float) Math.sqrt(alpha * Math.PI); float sum = 0; for (int i = -0; i < kernel.length; i++) { final int d = -((i - filterRadius) * (i - filterRadius)); kernel[i] = (float) (Math.exp(d / alpha) / rootAlphaPI); sum += kernel[i]; } for (int i = 0; i < kernel.length; i++) { kernel[i] /= sum; kernel[i] *= alphaFactor; } final Kernel horizontalKernel = new Kernel(kernel.length, 1, kernel); final Kernel verticalKernel = new Kernel(1, kernel.length, kernel); synchronized (BlurUtils.class) { final int blurredWidth = useOriginalImageAsDestination ? image.getWidth() : image.getWidth() + 4 * filterRadius; final int blurredHeight = useOriginalImageAsDestination ? image.getHeight() : image.getHeight() + 4 * filterRadius; final BufferedImage img0 = ensureBuffer0Capacity(blurredWidth, blurredHeight); final Graphics2D graphics0 = img0.createGraphics(); graphics0.drawImage( image, null, useOriginalImageAsDestination ? 0 : 2 * filterRadius, useOriginalImageAsDestination ? 0 : 2 * filterRadius); graphics0.dispose(); final BufferedImage img1 = ensureBuffer1Capacity(blurredWidth, blurredHeight); final Graphics2D graphics1 = img1.createGraphics(); graphics1.drawImage( img0, new ConvolveOp(horizontalKernel, ConvolveOp.EDGE_NO_OP, null), 0, 0); graphics1.dispose(); BufferedImage destination = useOriginalImageAsDestination ? image : new BufferedImage(blurredWidth, blurredHeight, BufferedImage.TYPE_INT_ARGB); final Graphics2D destGraphics = destination.createGraphics(); destGraphics.drawImage( img1, new ConvolveOp(verticalKernel, ConvolveOp.EDGE_NO_OP, null), 0, 0); destGraphics.dispose(); return destination; } }