您的位置:首页 > 编程语言 > Java开发

JAVA实现水彩画滤镜

2016-06-05 19:17 369 查看
先上图。



左边原图,右边效果图。

实现思路:

水彩画滤镜算法如下:

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就行了。 一共就两个类,我就不多解释了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: