图像处理------像素格效果

来源:互联网 发布:php隐藏源码 编辑:程序博客网 时间:2024/05/17 06:42

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

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

 

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

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

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

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

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

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

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

程序效果:



像素格滤镜源代码如下:

[java] view plaincopy
  1. package com.process.blur.study;  
  2. /** 
  3.  * @author gloomy fish 
  4.  * @date 2012-05-30 
  5.  *  
  6.  */  
  7. import java.awt.image.BufferedImage;  
  8.   
  9. public class PixellateFilter extends AbstractBufferedImageOp {  
  10.     private int size;  
  11.       
  12.     public PixellateFilter() {  
  13.         size = 10// default block size=10x10  
  14.     }  
  15.       
  16.     public PixellateFilter(int size) {  
  17.         this.size = size;  
  18.     }  
  19.   
  20.     @Override  
  21.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  22.         int width = src.getWidth();  
  23.         int height = src.getHeight();  
  24.   
  25.         if ( dest == null )  
  26.             dest = createCompatibleDestImage( src, null );  
  27.   
  28.         int[] inPixels = new int[width*height];  
  29.         int[] outPixels = new int[width*height];  
  30.         getRGB( src, 00, width, height, inPixels );  
  31.         int index = 0;  
  32.           
  33.         int offsetX = 0, offsetY = 0;  
  34.         int newX = 0, newY = 0;  
  35.         double total = size*size;  
  36.         double sumred = 0, sumgreen = 0, sumblue = 0;  
  37.         for(int row=0; row<height; row++) {  
  38.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  39.             for(int col=0; col<width; col++) {  
  40.                 newY = (row/size) * size;  
  41.                 newX = (col/size) * size;  
  42.                 offsetX = newX + size;  
  43.                 offsetY = newY + size;  
  44.                 for(int subRow =newY; subRow < offsetY; subRow++) {  
  45.                     for(int subCol =newX; subCol < offsetX; subCol++) {  
  46.                         if(subRow <0 || subRow >= height) {  
  47.                             continue;  
  48.                         }  
  49.                         if(subCol < 0 || subCol >=width) {  
  50.                             continue;  
  51.                         }  
  52.                         index = subRow * width + subCol;  
  53.                         ta = (inPixels[index] >> 24) & 0xff;  
  54.                         sumred += (inPixels[index] >> 16) & 0xff;  
  55.                         sumgreen += (inPixels[index] >> 8) & 0xff;  
  56.                         sumblue += inPixels[index] & 0xff;  
  57.                     }  
  58.                 }  
  59.                 index = row * width + col;  
  60.                 tr = (int)(sumred/total);  
  61.                 tg = (int)(sumgreen/total);  
  62.                 tb = (int)(sumblue/total);  
  63.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  64.   
  65.                 sumred = sumgreen = sumblue = 0// reset them...  
  66.             }  
  67.         }  
  68.   
  69.         setRGB( dest, 00, width, height, outPixels );  
  70.         return dest;  
  71.     }  
  72.   
  73. }  
0 0
原创粉丝点击