JAVA实现水彩画滤镜
2016-06-05 19:17
369 查看
先上图。
![](http://img.blog.csdn.net/20160605191218647?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
左边原图,右边效果图。
实现思路:
水彩画滤镜算法如下:
1,假设原始图像为F(x,y),灰度化得到G(x,y);
2,构建一个半径为Radius的正方形模板M,边长为2*Radius+1;
3,将M在F上依次遍历每个像素,对于当前像素P(x,y):
设置一个油漆桶数N,由于图像灰度值范围为0-255,因此我们油漆桶的数量N要小于255,这个油漆桶是用来盛放不同类别的像素。
3.1首先按照油漆桶数N将0-255的范围划分为等距的N个油漆桶,对于模板中对应的像素,我们按照其灰度值,依次将其放入相应的油漆桶中;
3.2统计N个油漆桶中的像素数目,计算像素数最多的那个油漆桶内,像素的均值Mean,这个均值RGB就是模板中心像素P(x,y)的值。
完结- -下面是代码,复制粘贴运行即可。
package suicai;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SuiCaiDemo {
public static BufferedImage readImage(String imageName) {
File imageFile = new File(imageName);
BufferedImage bufferedImage=null;
try {
bufferedImage = ImageIO.read(imageFile);
} catch (IOException e) {
e.printStackTrace();
}
return bufferedImage;
}
public static void writeImage(BufferedImage bi, String imageName) {
File skinImageOut = new File(imageName);
try {
ImageIO.write(bi, "png", skinImageOut);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int rgb2gray(int rgb) {
int r = (rgb & 0xff0000) >> 16;
int g = (rgb & 0xff00) >> 8;
int b = (rgb & 0xff);
int gray = (int)(r * 0.3 + g * 0.59 + b * 0.11);
return gray;
}
public static void main(String[] args) {
//参数设置
int radius =4;//半径
int tank=10;//油漆桶
String savePath="I:\\suicai";
String openPath="I:\\pic3.jpg";
BufferedImage colorImage=readImage(openPath);//原图
int width=colorImage.getWidth();
int height=colorImage.getHeight();
//输出图
BufferedImage outRes = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 1; y < height - 1; ++y){
for (int x = 1; x < width - 1; ++x){
ColorTank ctank=new ColorTank(tank);
for (int tx = x-radius; tx < x+radius; tx++) {
for (int ty = y-radius; ty < y+radius; ty++) {
if (tx>0 && tx<width && ty>0 && ty<height) {
ctank.addToTank(colorImage.getRGB(tx, ty));
}
}
}//内矩阵循环
outRes.setRGB(x, y,ctank.getColor());
// System.out.println("x:"+x+"y:"+y);
}
}
writeImage(outRes, savePath+System.currentTimeMillis()+".jpg");
System.out.println("****success****");
}
}
运行SuiCaiDemo就行了。 一共就两个类,我就不多解释了。
左边原图,右边效果图。
实现思路:
水彩画滤镜算法如下:
1,假设原始图像为F(x,y),灰度化得到G(x,y);
2,构建一个半径为Radius的正方形模板M,边长为2*Radius+1;
3,将M在F上依次遍历每个像素,对于当前像素P(x,y):
设置一个油漆桶数N,由于图像灰度值范围为0-255,因此我们油漆桶的数量N要小于255,这个油漆桶是用来盛放不同类别的像素。
3.1首先按照油漆桶数N将0-255的范围划分为等距的N个油漆桶,对于模板中对应的像素,我们按照其灰度值,依次将其放入相应的油漆桶中;
3.2统计N个油漆桶中的像素数目,计算像素数最多的那个油漆桶内,像素的均值Mean,这个均值RGB就是模板中心像素P(x,y)的值。
完结- -下面是代码,复制粘贴运行即可。
package suicai;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SuiCaiDemo {
public static BufferedImage readImage(String imageName) {
File imageFile = new File(imageName);
BufferedImage bufferedImage=null;
try {
bufferedImage = ImageIO.read(imageFile);
} catch (IOException e) {
e.printStackTrace();
}
return bufferedImage;
}
public static void writeImage(BufferedImage bi, String imageName) {
File skinImageOut = new File(imageName);
try {
ImageIO.write(bi, "png", skinImageOut);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int rgb2gray(int rgb) {
int r = (rgb & 0xff0000) >> 16;
int g = (rgb & 0xff00) >> 8;
int b = (rgb & 0xff);
int gray = (int)(r * 0.3 + g * 0.59 + b * 0.11);
return gray;
}
public static void main(String[] args) {
//参数设置
int radius =4;//半径
int tank=10;//油漆桶
String savePath="I:\\suicai";
String openPath="I:\\pic3.jpg";
BufferedImage colorImage=readImage(openPath);//原图
int width=colorImage.getWidth();
int height=colorImage.getHeight();
//输出图
BufferedImage outRes = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 1; y < height - 1; ++y){
for (int x = 1; x < width - 1; ++x){
ColorTank ctank=new ColorTank(tank);
for (int tx = x-radius; tx < x+radius; tx++) {
for (int ty = y-radius; ty < y+radius; ty++) {
if (tx>0 && tx<width && ty>0 && ty<height) {
ctank.addToTank(colorImage.getRGB(tx, ty));
}
}
}//内矩阵循环
outRes.setRGB(x, y,ctank.getColor());
// System.out.println("x:"+x+"y:"+y);
}
}
writeImage(outRes, savePath+System.currentTimeMillis()+".jpg");
System.out.println("****success****");
}
}
package suicai; import java.awt.Color; import java.util.ArrayList; import java.util.List; public class ColorTank { private int count=0; private List<List<Integer>> colorList=new ArrayList<List<Integer>>(); ColorTank(int count){ this.count=count; for(int i=0;i<=this.count;i++){ colorList.add(new ArrayList<Integer>()); } } void addToTank(int rgb){ float gray=SuiCaiDemo.rgb2gray(rgb); int index=(int) ((gray/255)*count); colorList.get(index).add(rgb); } int getColor(){ int maxIndex=0; int maxSize = 0; for (int i = 0; i < colorList.size(); i++) { List<Integer> temp=colorList.get(i); if (temp.size()>maxSize) { maxSize=temp.size(); maxIndex=i; } } List<Integer> maxRGBList=colorList.get(maxIndex); int allColorR=0; int allColorG=0; int allColorB=0; for (Integer integer : maxRGBList) { Color c=new Color(integer); allColorR+=c.getRed(); allColorG+=c.getGreen(); allColorB+=c.getBlue(); } allColorR=allColorR/maxRGBList.size(); allColorG=allColorG/maxRGBList.size(); allColorB=allColorB/maxRGBList.size(); return new Color(allColorR, allColorG, allColorB).getRGB(); } }
运行SuiCaiDemo就行了。 一共就两个类,我就不多解释了。
相关文章推荐
- 导入eclipse代码格式化模板
- spring setter注入案例
- SSH三大框架整合的流程是怎样走的,spring在三大框架中式怎么的一个流程,hibernate是怎么的一个流程,struts2是怎么的一个流程的,
- JDK的方法是最优的吗?
- spring mvc实现图片上传
- Thrift 小试牛刀
- 【Web开发原理】web发展历史
- gPRC 小试牛刀
- Java环境配置
- JAVA中的数组对象
- Java泛型让声明方法返回子类型
- SSH框架介绍以及如何搭建(myEclipse下)
- 贪吃蛇的java代码分析(二)
- java之基础(01)
- JAVA WEB 之 XML(1)
- Java设计模式之适配器模式Adapter(结构型)
- Java EL表达式
- springMVC
- java基础之继承
- Spring之IOC