java 图算法_java – 照片/图像到草图算法

这篇博客介绍了如何使用Java实现将照片或图像转化为草图的算法。通过将图像转换为灰度、颜色反转、高斯模糊,然后应用颜色 dodge 混合模式的公式,最终得到草图效果。博主分享了详细的Java代码实现,并邀请读者提供改进意见。
摘要由CSDN通过智能技术生成

好的,所以我用马克告诉我的不同技术找到了自己的答案.

我使用以下伪代码:

*s = Read-File-Into-Image("/path/to/image")

*g = Convert-To-Gray-Scale(s)

*i = Invert-Colors(g)

*b = Apply-Gaussian-Blur(i)

*result = Color-Dodge-Blend-Merge(b,g)

前四种方法很容易在互联网上找到,但是在最后一种方法中我找不到很多信息,甚至没有找到源代码.所以我搜索了PS是如何做到的,并在c中找到了以下公式:

((uint8)((B == 255) ? B:min(255, ((A << 8 ) / (255 - B)))))

然后我使用以下代码将其转换为Java:

private int colordodge(int in1, int in2) {

float image = (float)in2;

float mask = (float)in1;

return ((int) ((image == 255) ? image:Math.min(255, (((long)mask << 8 ) / (255 - image)))));

}

/**

* Blends 2 bitmaps to one and adds the color dodge blend mode to it.

*/

public Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) {

Bitmap base = source.copy(Config.ARGB_8888, true);

Bitmap blend = layer.copy(Config.ARGB_8888, false);

IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());

base.copyPixelsToBuffer(buffBase);

buffBase.rewind();

IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());

blend.copyPixelsToBuffer(buffBlend);

buffBlend.rewind();

IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());

buffOut.rewind();

while (buffOut.position() < buffOut.limit()) {

int filterInt = buffBlend.get();

int srcInt = buffBase.get();

int redValueFilter = Color.red(filterInt);

int greenValueFilter = Color.green(filterInt);

int blueValueFilter = Color.blue(filterInt);

int redValueSrc = Color.red(srcInt);

int greenValueSrc = Color.green(srcInt);

int blueValueSrc = Color.blue(srcInt);

int redValueFinal = colordodge(redValueFilter, redValueSrc);

int greenValueFinal = colordodge(greenValueFilter, greenValueSrc);

int blueValueFinal = colordodge(blueValueFilter, blueValueSrc);

int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal);

buffOut.put(pixel);

}

buffOut.rewind();

base.copyPixelsFromBuffer(buffOut);

blend.recycle();

return base;

}

如果代码可以改进,请在下面发布新的答案或评论.谢谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值