简体   繁体   中英

how to blur the image?

I need to implement image blur

to do this, I have to use a double array in the form of a matrix, and implement the following:

  1. each coefficient of the convolution matrix must be multiplied by the color value of the corresponding neighbor of the current (changeable) pixel
  2. I need to handle going beyond 0 to 255

I tried to create and fill in the matrix with the 1/9 numbers that are in the task, but then I don't understand what and by what should I multiply?

has anyone solved this?

public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("image.jpg"));
    WritableRaster raster = image.getRaster();

    int width = raster.getWidth();
    int height = raster.getHeight();

    final int colorsCountInRgb = 3;
    final int colorMaximum = 255;

    int[] pixel = new int[colorsCountInRgb];

    double[][] matrix = new double[3][3];
    double matrixMultiplier = 1 / 9d;

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            matrix[i][j] = matrixMultiplier;
        }
    }

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            raster.getPixel(x, y, pixel);

            raster.setPixel(x, y, pixel);
        }
    }

    ImageIO.write(image, "png", new File("out.png"));
}

You can blur a image by using smoothing. The java program is given below:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class GFG {

    
    public static void main(String[] args)
        throws IOException, InterruptedException
    {

        Color color[];

        
        File fin = new File("File Location"); // your file location here

        
        BufferedImage input = ImageIO.read(fin);

        
        BufferedImage output = new BufferedImage(
            input.getWidth(), input.getHeight(),
            BufferedImage.TYPE_INT_RGB);

        
        int i = 0;
        int max = 400, rad = 10;
        int a1 = 0, r1 = 0, g1 = 0, b1 = 0;
        color = new Color[max];


        int x = 1, y = 1, x1, y1, ex = 5, d = 0;

        
        for (x = rad; x < input.getHeight() - rad; x++) {
            for (y = rad; y < input.getWidth() - rad; y++) {
                for (x1 = x - rad; x1 < x + rad; x1++) {
                    for (y1 = y - rad; y1 < y + rad; y1++) {
                        color[i++] = new Color(
                            input.getRGB(y1, x1));
                    }
                }

                
                i = 0;
                for (d = 0; d < max; d++) {
                    a1 = a1 + color[d].getAlpha();
                }

                a1 = a1 / (max);
                for (d = 0; d < max; d++) {
                    r1 = r1 + color[d].getRed();
                }

                r1 = r1 / (max);
                for (d = 0; d < max; d++) {
                    g1 = g1 + color[d].getGreen();
                }

                g1 = g1 / (max);
                for (d = 0; d < max; d++) {
                    b1 = b1 + color[d].getBlue();
                }

                b1 = b1 / (max);
                int sum1 = (a1 << 24) + (r1 << 16)
                        + (g1 << 8) + b1;
                output.setRGB(y, x, (int)(sum1));
            }
        }

        
        ImageIO.write(
            output, "jpeg", // file type here
            new File("File location")); // your file location here

        
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM