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();
        }
    }
  • 相关阅读:
    PHP发红包程序限制红包的大小
    PHP函数
    HTTP Keep-Alive详解[转]
    PHP中PSR
    一级域名、二级域名和二级目录有什么区别?
    mysqli_query(“set nams utf8”)为FALSE
    Mysql中结果集(mysql_result)与Traversable
    Hbase系列(一)---- Hbase究竟是一个怎样的数据库(基础原理)
    java-快速失败与安全失败
    java-hashmap 工作原理
  • 原文地址:https://www.cnblogs.com/libaoting/p/gaosimohu.html
Copyright © 2011-2022 走看看