java 滤镜实现

一句话,滤镜的实现就是对像素点(RGBA)进行再运算,输出新的像素点。    F(r,g,b,a)=G(r,g,b,a);

 这个公式包含四个变换,即RGB颜色空间中RGB三个分量的变换以及透明度Alhpa的变换,这里我们简写为A的变换。

    举个灰度变换的例子,它对应的F——G变换如下:

    F(r) = b * 0.114 + g * 0.587 + r * 0.299;

    F(g) = b * 0.114 + g * 0.587 + r * 0.299;

    F(b) = b * 0.114 + g * 0.587 + r * 0.299;

    F(a) = a;

这个灰度化也就是一个基本变换。有了这个基本变换,图像也就达到了一定的效果,但是,一些复杂的滤镜,并非简单的基本变换,而是一些复杂的效果叠加,也就说

这个G函数是多个函数的联合体。

还有一种方法,对于一些滤镜产品,我们直观看到的滤镜效果是已经生成的新像素点组成,也就是说如果我们想破解这种滤镜,

我们可以通过颜色映射构建一个映射表,然后通过查表来快速实现该变换效果,

这种万能滤镜破解的方法,请参考 http://blog.csdn.net/trent1985/article/details/42212459

本人看了以上博主的很多滤镜相关的博文,得到很多启发(感谢!)

以下是Java实现Lomo效果的代码,可以直接用。

public static void Lomo(String fromPath, String toPath) throws IOException {

BufferedImage fromImage = ImageIO.read(new File(fromPath));

int width = fromImage.getWidth();
int height = fromImage.getHeight();
BufferedImage toImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

int a, r, g, b, grayValue = 0;

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int rgb = fromImage.getRGB(i, j);
// 过滤
a = rgb & 0xff000000;
r = (rgb & 0xff0000) >> 16;
g = (rgb & 0xff00) >> 8;
b = (rgb & 0xff);

b = ModeSmoothLight(b, b);
g = ModeSmoothLight(g, g);
r = ModeSmoothLight(r, r);
b = ModeExclude(b, 80);
g = ModeExclude(g, 15);
r = ModeExclude(r, 5);

grayValue = a | (r << 16) | (g << 8) | b;
toImage.setRGB(i, j, grayValue);
}
}
ImageIO.write(toImage, "jpg", new File(toPath));
}

private static int ModeSmoothLight(int basePixel, int mixPixel) {
int res = 0;
res = mixPixel > 128
? ((int) ((float) basePixel + ((float) mixPixel + (float) mixPixel - 255.0f)
* ((Math.sqrt((float) basePixel / 255.0f)) * 255.0f - (float) basePixel) / 255.0f))
: ((int) ((float) basePixel + ((float) mixPixel + (float) mixPixel - 255.0f)
* ((float) basePixel - (float) basePixel * (float) basePixel / 255.0f) / 255.0f));
return Math.min(255, Math.max(0, res));
}

private static int ModeExclude(int basePixel, int mixPixel) {
int res = 0;
res = (mixPixel + basePixel) - mixPixel * basePixel / 128;
return Math.min(255, Math.max(0, res));
}

public static void main(String[] args) throws Exception {

Lomo("C:\\2.jpg", "C:\\test2.jpg");

}

 

转载于:https://www.cnblogs.com/marszhw/p/5908901.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值