图像处理之仿油画效果

基于像素权重实现图像的像素模糊从而达到近似油画效果模糊

其实卷积的变种,不是基于Stroke维诺图算法与采样moment算法

模拟实现。不想跟多的废话了,先看效果:


说实话,货是干货,但是不想再多写罗嗦话,自己看代码吧!滤镜代码:

package com.gloomyfish.nature.filter.study;

import java.awt.image.BufferedImage;

import com.gloomyfish.filter.study.AbstractBufferedImageOp;

public class OilPaintFilter extends AbstractBufferedImageOp {

	private int radius = 5; // default value
	private int intensity = 20; // default value

	public OilPaintFilter(int radius, int graylevel) {
		this.radius = radius;
		this.intensity = graylevel;
	}

	public OilPaintFilter() {
		this(5, 20);
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}

	public int getIntensity() {
		return intensity;
	}

	public void setIntensity(int intensity) {
		this.intensity = intensity;
	}

	@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 subradius = this.radius / 2;
        int[] intensityCount = new int[intensity+1];
        int[] ravg = new int[intensity+1];
        int[] gavg = new int[intensity+1];
        int[] bavg = new int[intensity+1];
        for(int i=0; i<=intensity; i++)
        {
        	intensityCount[i] = 0;
        	ravg[i] = 0;
        	gavg[i] = 0;
        	bavg[i] = 0;
        }
        for(int row=0; row<height; row++) {
        	int ta = 0, tr = 0, tg = 0, tb = 0;
        	for(int col=0; col<width; col++) {
        		
        		for(int subRow = -subradius; subRow <= subradius; subRow++)
        		{
        			for(int subCol = -subradius; subCol <= subradius; subCol++)
        			{
        				int nrow = row + subRow;
        				int ncol = col + subCol;
        				if(nrow >=height || nrow < 0)
        				{
        					nrow = 0;
        				}
        				if(ncol >= width || ncol < 0)
        				{
        					ncol = 0;
        				}
        				index = nrow * width + ncol;
	                    tr = (inPixels[index] >> 16) & 0xff;
	                    tg = (inPixels[index] >> 8) & 0xff;
	                    tb = inPixels[index] & 0xff;
	                    int curIntensity = (int)(((double)((tr+tg+tb)/3)*intensity)/255.0f);
	            		intensityCount[curIntensity]++;
	            		ravg[curIntensity] += tr;
	            		gavg[curIntensity] += tg;
	            		bavg[curIntensity] += tb;
        			}
        		}
        		
        		// find the max number of same gray level pixel
        		int maxCount = 0, maxIndex = 0;
        		for(int m=0; m<intensityCount.length; m++)
        		{
        			if(intensityCount[m] > maxCount)
        			{
        				maxCount = intensityCount[m];
        				maxIndex = m;
        			}
        		}
        		
        		// get average value of the pixel
        		int nr = ravg[maxIndex] / maxCount;
        		int ng = gavg[maxIndex] / maxCount;
        		int nb = bavg[maxIndex] / maxCount;
        		index = row * width + col;
        		outPixels[index] = (ta << 24) | (nr << 16) | (ng << 8) | nb;
        		
        		// post clear values for next pixel
                for(int i=0; i<=intensity; i++)
                {
                	intensityCount[i] = 0;
                	ravg[i] = 0;
                	gavg[i] = 0;
                	bavg[i] = 0;
                }
                
        	}
        }
        setRGB( dest, 0, 0, width, height, outPixels );
        return dest;
	}

}
最后:

祝各位新春快乐,阖家欢乐!谢谢过去一年你们对本博客的关注与支持!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gloomyfish

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值