图像处理------像素格效果
2015-07-24 10:13
459 查看
图像中的像素格效果是最常见的图像特效,可以隐藏或者模糊一些不想被显示出来的图像细
节,是常用的图像处理手段。
像素格效果的算法其实非常的简单,只是对图像进行块扫描,求出每个像素块的平均RGB
值,然后赋值到块中的每个像素点,最后输出处理以后的图像,而像素块的扫描有点类似
卷积的处理。具体算法步骤如下:
1. 按照从左到右,自上而下的顺序,扫描每个像素点。
2. 对扫描到的像素,计算出它属于的像素块,并且计算像素块的平均RGB值
3. 将RGB赋值给扫描到的像素点。
4. 循环上面2,3步骤,直到所有像素点都完成。
程序效果:
![](http://img.my.csdn.net/uploads/201205/31/1338416421_3154.png)
像素格滤镜源代码如下:
[java] view plaincopypackage 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;
}
}
节,是常用的图像处理手段。
像素格效果的算法其实非常的简单,只是对图像进行块扫描,求出每个像素块的平均RGB
值,然后赋值到块中的每个像素点,最后输出处理以后的图像,而像素块的扫描有点类似
卷积的处理。具体算法步骤如下:
1. 按照从左到右,自上而下的顺序,扫描每个像素点。
2. 对扫描到的像素,计算出它属于的像素块,并且计算像素块的平均RGB值
3. 将RGB赋值给扫描到的像素点。
4. 循环上面2,3步骤,直到所有像素点都完成。
程序效果:
![](http://img.my.csdn.net/uploads/201205/31/1338416421_3154.png)
像素格滤镜源代码如下:
[java] view plaincopypackage 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;
}
}
相关文章推荐
- 使用target打开的iframe 获取src的问题
- 图像处理------像素格效果
- U盘安装CentOS 6.2(超级详细图解教程)
- Linux中的隐藏文件夹
- 变化的阅读程序猿自学习
- MYSQL---解决命令行汉字乱码
- 图像处理------像素格效果 分类: 视频图像处理 2015-07-24 10:13 47人阅读 评论(0) 收藏
- POI读取Excel时对cell中数据的判断
- 图像处理------简单综合实例(大米计数)
- 图像处理------简单综合实例(大米计数)
- 母函数小总结
- HDU 1290 献给杭电五十周年校庆的礼物(递推)
- realloc 函数的用法 以及 误区
- 图像处理------简单综合实例(大米计数)
- win7下的telnet
- 开始Orchard
- android资源:菜单和assets
- HTTP 协议
- CCFlow中通过excel向Dtl明细表(从表)导入数据的操作说明
- Linux 下 Oracle 内核参数优化