图像处理之像素格效果

- 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;  
    }  
  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值