java 图像特效之老照片

阅读本系列,请先看前言!谢谢

让图像微微泛黄~是不是充满回忆呢~

本文先给出一种实现方法(参考:图像处理之老照片特效

算法步骤如下:

1. 首先对图像重新计算RGB值,计算公式如下:

  1. int fr= (int)(((double)tr * 0.393) + ((double)tg * 0.769) + ((double)tb * 0.189));  
  2. int fg = (int)(((double)tr * 0.349) + ((double)tg * 0.686) + ((double)tb * 0.168));   
  3. int fb= (int)(((double)tr * 0.272) + ((double)tg * 0.534) + ((double)tb * 0.131));  
2. 对图像计算出来的新的RGB值根据随机权重与旧值混合。

计算随机权重的代码如下:

  1. private double noise() {  
  2.     return Math.random()*0.5 + 0.5;  
  3. }  
混合颜色的代码如下:
  1. private double colorBlend(double scale, double dest, double src) {  
  2.     return (scale * dest + (1.0 - scale) * src);  


实现代码如下:

public Image filter() {
		
		if(this.img.gray)
			return this.img;  // Grayscale images can not be processed
		
		for (int y = 0; y < this.img.h; y++) {
            for (int x = 0; x < this.img.w; x++) {
            	
                int c = this.img.data[x + y * this.img.w];
                int ta = (c >> 24) & 0xff; 
                int tr = (c >> 16) & 0xFF;
                int tg = (c >> 8) & 0xFF;
                int tb = (c >> 0) & 0xFF;
                
                int fr = (int)colorBlend(noise(), (tr * 0.393) + (tg * 0.769) + (tb * 0.189), tr);  
                int fg = (int)colorBlend(noise(), (tr * 0.349) + (tg * 0.686) + (tb * 0.168), tg);  
                int fb = (int)colorBlend(noise(), (tr * 0.272) + (tg * 0.534) + (tb * 0.131), tb);  
                  
                this.img.data[x + y * this.img.w] = (ta << 24) | (clamp(fr) << 16) | (clamp(fg) << 8) | clamp(fb);              
            }
        }
		
		return this.img;
	}

其中

 private  int clamp(int c)  
    {  
        return c > 255 ? 255 :( (c < 0) ? 0: c);  
    } 

运行效果如下:

其实可以看到,这个操作是对每个像素进行的,不涉及其它的像素,故可以通过颜色矩阵来实现,对于颜色矩阵,我会在后面单独开一篇文章来说明

先放出有颜色矩阵实现的老照片的特效:

可以看出比上面的明亮。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值