JAVA实现水彩画滤镜

先上图。


左边原图,右边效果图。


实现思路:

水彩画滤镜算法如下:

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就行了。 一共就两个类,我就不多解释了。


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值