图像处理之像素格效果

来源:互联网 发布:python 打包 发布网站 编辑:程序博客网 时间:2024/06/06 21:30

- created by gloomyfish


图像中的像素格效果是最常见的图像特效,可以隐藏或者模糊一些不想被显示出来的图像细

节,是常用的图像处理手段。

 

像素格效果的算法其实非常的简单,只是对图像进行块扫描,求出每个像素块的平均RGB

值,然后赋值到块中的每个像素点,最后输出处理以后的图像,而像素块的扫描有点类似

卷积的处理。具体算法步骤如下:

1.      按照从左到右,自上而下的顺序,扫描每个像素点。

2.      对扫描到的像素,计算出它属于的像素块,并且计算像素块的平均RGB值

3.      将RGB赋值给扫描到的像素点。

4.      循环上面2,3步骤,直到所有像素点都完成。

程序效果:


http://my.csdn.net/my/album/detail/1166674

package com.process.blur.study;  /**  * @author gloomy fish  * @date 2012-05-30  *   */  import java.awt.image.BufferedImage;    public class PixellateFilter extends AbstractBufferedImageOp {      private int size;            public PixellateFilter() {          size = 10; // default block size=10x10      }            public PixellateFilter(int size) {          this.size = size;      }        @Override      public BufferedImage filter(BufferedImage src, BufferedImage dest) {          int width = src.getWidth();          int height = src.getHeight();            if ( dest == null )              dest = createCompatibleDestImage( src, null );            int[] inPixels = new int[width*height];          int[] outPixels = new int[width*height];          getRGB( src, 0, 0, width, height, inPixels );          int index = 0;                    int offsetX = 0, offsetY = 0;          int newX = 0, newY = 0;          double total = size*size;          double sumred = 0, sumgreen = 0, sumblue = 0;          for(int row=0; row<height; row++) {              int ta = 0, tr = 0, tg = 0, tb = 0;              for(int col=0; col<width; col++) {                  newY = (row/size) * size;                  newX = (col/size) * size;                  offsetX = newX + size;                  offsetY = newY + size;                  for(int subRow =newY; subRow < offsetY; subRow++) {                      for(int subCol =newX; subCol < offsetX; subCol++) {                          if(subRow <0 || subRow >= height) {                              continue;                          }                          if(subCol < 0 || subCol >=width) {                              continue;                          }                          index = subRow * width + subCol;                          ta = (inPixels[index] >> 24) & 0xff;                          sumred += (inPixels[index] >> 16) & 0xff;                          sumgreen += (inPixels[index] >> 8) & 0xff;                          sumblue += inPixels[index] & 0xff;                      }                  }                  index = row * width + col;                  tr = (int)(sumred/total);                  tg = (int)(sumgreen/total);                  tb = (int)(sumblue/total);                  outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;                    sumred = sumgreen = sumblue = 0; // reset them...              }          }            setRGB( dest, 0, 0, width, height, outPixels );          return dest;      }    }  


原创粉丝点击