Android-高斯模糊效果-OpenGL [英] Android - Gaussian blur like effect - OpenGL

查看:181
本文介绍了Android-高斯模糊效果-OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未指定此问题的地方只是在 grafika 项目的CameraCaptureActivity之上在github上找到.

Where not specified this question is just building on top of the CameraCaptureActivity within the grafika project found on github.

它具有使用3x3内核的内置模糊效果

It has a built in blur effect that utilises a 3x3 kernel

kernel = new float[] {
    1f/16f, 2f/16f, 1f/16f,
    2f/16f, 4f/16f, 2f/16f,
    1f/16f, 2f/16f, 1f/16f };

但是这种模糊效果不够强,我正在寻找类似高斯效果的东西,该效果可以在iOS上使用UIVisualEffectView来实现,它看起来像这样:

However this blur effect is not strong enough, im looking for something like what the gaussian effect can do on iOS with UIVisualEffectView, it looks something like this:

一个很好的平滑重模糊效果,但是到目前为止,我所能控制的最佳效果是:

A nice smooth heavy blur effect but so far the best ive managed is this:

如您所见,它不那么平滑,而且有点方形.

As you can see it is not nearly as smooth and also a bit squarish.

我通过转换为使用此便捷工具生成的5x5内核来实现这一目标.的sigma为30,内核大小为5.它会产生以下结果:

I achieved this by converting to a 5x5 kernel generated using this handy tool with a sigma of 30 and kernel size of 5. It produces the following:

kernel = new float[] {
    0.039911f,  0.039978f,  0.04f,      0.039978f,  0.039911f,
    0.039978f,  0.040044f,  0.040067f,  0.040044f,  0.039978f,
    0.04f,      0.040067f,  0.040089f,  0.040067f,  0.04f,
    0.039978f,  0.040044f,  0.040067f,  0.040044f,  0.039978f,
    0.039911f,  0.039978f,  0.04f,      0.039978f,  0.039911f
};

为了在Grafika项目中工作,我不得不在Texture2dProgram类中修改KERNEL_SIZEmTexOffset

In order to get the to work within the Grafika project i had to modify KERNEL_SIZE and mTexOffset within the Texture2dProgram class

KERNEL_SIZE现在是25,而mTextOffset现在是这样计算的:

KERNEL_SIZE is now 25 and mTextOffset is now calculated like so:

public void setTexSize(int width, int height) {

    float rw = 50.0f / width;
    float rh = 50.0f / height;

    float rw50 = rw * 0.5f;
    float rh50 = rh * 0.5f;

    mTexOffset = new float[] {
            -rw, -rh,   -rw50, -rh,     0f, -rh,    rw50, -rh,      rw, -rh,
            -rw, -rh50, -rw50, -rh50,   0f, -rh50,  rw50, -rh50,    rw, -rh50,
            -rw, 0f,    -rw50, 0f,      0f, 0f,     rw50, -0f,      rw, 0f,
            -rw, rh50,  -rw50, rh50,    0f, rh50,   rw50, rh50,     rw, rh50,
            -rw, rh,    -rw50, rh,      0f, rh,     rw50, rh,       rw, rh
    };
};

有人对我可以进行哪些修改以实现像模糊这样的iOS提出建议(我认为iOS也在减轻像素)?我认为我真正出错的地方是setTextSize()计算,特别是50.0f值,我刚刚从空中摘下来并观察了它的效果

Does anyone have an suggestions on what i could modify to achieve an iOS like blur (i think iOS is also lightening pixels as well)? I think where i am really going wrong is the setTextSize() calculation, specifically the 50.0f value, i have just plucked this from thin air and observed the effect it has

推荐答案

即使在GPU上,使用实际模糊内核进行卷积也是一项计算量很大的任务.有一些技巧可以使其更好地工作:

Convolution with an actual blur kernel is a computationally intensive task, even on a GPU. There are a few techniques that make it work better:

  • 高斯核可以分解为X和Y分量,分别进行计算.图像处理程序使用此技术是因为它相对较快且准确.

  • The Gaussian kernel can be decomposed into X and Y components, which are computed separately. This technique is used by image manipulation programs because it is relatively fast and accurate.

您可以使用Poisson光盘采样来使图像模糊,而不是使用高斯内核.

Instead of using the Gaussian kernel, you can use Poisson disc sampling to blur the image.

您可以使用多通Kawase滤波器作为高斯滤波器的近似值.

You can use a multi-pass Kawase filter as an approximation to a Gaussian filter.

在给定的主观模糊质量下,从较低分辨率的mip贴图采样将获得更好的性能.

Sampling from lower resolution mip maps will result in better performance for a given subjective blur quality.

您可以在纹理像素之间进行采样,以劫持纹理插值并获得更多的水龙头.

You can sample from between texels in order to hijack texture interpolation and get more taps than you would otherwise get.

一旦开始进行这些权衡,通常需要进行一些微调才能使其看起来正确".

It will generally take some fine-tuning to get it to "look right" once you start making these tradeoffs. A good summary is available at An investigation of fast real-time GPU-based image blur algorithms (Filip S., 2014).

这篇关于Android-高斯模糊效果-OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆