zoukankan      html  css  js  c++  java
  • 简单的java高斯模糊算法

    import java.awt.Color;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;   
    
    public class Test
    {
     /**
         * 简单高斯模糊算法
         * 
         * @param args
         * @throws IOException [参数说明]
         * 
         * @return void [返回类型说明]
         * @exception throws [违例类型] [违例说明]
         * @see [类、类#方法、类#成员]
         */
        public static void main(String[] args)
            throws IOException
        {
            BufferedImage img = ImageIO.read(new File("d:\My Documents\psb.jpg"));
            System.out.println(img);
            int height = img.getHeight();
            int width = img.getWidth();
            int[][] matrix = new int[3][3];
            int[] values = new int[9];
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    readPixel(img, i, j, values);
                    fillMatrix(matrix, values);
                    img.setRGB(i, j, avgMatrix(matrix));
                }
            }
            ImageIO.write(img, "jpeg", new File("d:/test.jpg"));//保存在d盘为test.jpeg文件
        }
        
        private static void readPixel(BufferedImage img, int x, int y, int[] pixels)
        {
            int xStart = x - 1;
            int yStart = y - 1;
            int current = 0;
            for (int i = xStart; i < 3 + xStart; i++)
            {
                for (int j = yStart; j < 3 + yStart; j++)
                {
                    int tx = i;
                    if (tx < 0)
                    {
                        tx = -tx;
                    }
                    else if (tx >= img.getWidth())
                    {
                        tx = x;
                    }
                    
                    int ty = j;
                    if (ty < 0)
                    {
                        ty = -ty;
                    }
                    else if (ty >= img.getHeight())
                    {
                        ty = y;
                    }
                    pixels[current++] = img.getRGB(tx, ty);
                }
            }
        }
        
        private static void fillMatrix(int[][] matrix, int... values)
        {
            int filled = 0;
            for (int i = 0; i < matrix.length; i++)
            {
                int[] x = matrix[i];
                for (int j = 0; j < x.length; j++)
                {
                    x[j] = values[filled++];
                }
            }
        }
        
        private static int avgMatrix(int[][] matrix)
        {
            int r = 0;
            int g = 0;
            int b = 0;
            for (int i = 0; i < matrix.length; i++)
            {
                int[] x = matrix[i];
                for (int j = 0; j < x.length; j++)
                {
                    if (j == 1)
                    {
                        continue;
                    }
                    Color c = new Color(x[j]);
                    r += c.getRed();
                    g += c.getGreen();
                    b += c.getBlue();
                }
            }
            return new Color(r / 8, g / 8, b / 8).getRGB();
        }
    }
  • 相关阅读:
    Java的三个基础排序算法(其余将在以后补充)
    Review PHP设计模式之——单例模式
    Mysql忘记密码,重新设置
    PHP加解密相关函数
    http返回状态代码及含义
    符合web标准的网页下拉菜单
    解决MySQL查询不区分大小写
    MYSQL Error 2006HY000:MySQL server has gone away的解决方案
    MySQL之count(*)与count(id)效率比较(转)
    如何让sudo命令不需要输入密码就可执行
  • 原文地址:https://www.cnblogs.com/libaoting/p/gaosimohu.html
Copyright © 2011-2022 走看看