简体   繁体   English

Android-高斯模糊效果-OpenGL

[英]Android - Gaussian blur like effect - OpenGL

Where not specified this question is just building on top of the CameraCaptureActivity within the grafika project found on github. 凡没有指定这个问题只是建立在顶部CameraCaptureActivity的内grafika在github上找到项目。

It has a built in blur effect that utilises a 3x3 kernel 它具有利用3x3内核的内置模糊效果

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

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: 但是,这种模糊效果不够强,我正在寻找类似高斯效果的UIVisualEffectView在iOS上可以做的UIVisualEffectView ,它看起来像这样:

A nice smooth heavy blur effect but so far the best ive managed is this: 一个不错的平滑重模糊效果,但到目前为止,管理得最好的ive是:

As you can see it is not nearly as smooth and also a bit squarish. 如您所见,它不那么平滑,而且有点方形。

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: 我通过转换为使用此便捷工具生成的5x5内核来实现这一目标,它的sigma为30,内核大小为5。它产生以下内容:

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
};

In order to get the to work within the Grafika project i had to modify KERNEL_SIZE and mTexOffset within the Texture2dProgram class 为了在Grafika项目中工作,我必须在Texture2dProgram类中修改KERNEL_SIZEmTexOffset

KERNEL_SIZE is now 25 and mTextOffset is now calculated like so: KERNEL_SIZE现在为25 ,现在按以下方式计算mTextOffset

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
    };
};

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)? 有没有人对我可以进行哪些修改以实现像模糊这样的iOS提出建议(我认为iOS也在减轻像素)? 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 我认为我真正出了问题的地方是setTextSize()计算,特别是50.0f值,我刚刚从稀薄的空气中拔出了它,并观察了它的效果

Convolution with an actual blur kernel is a computationally intensive task, even on a GPU. 即使在GPU上,与实际的模糊内核进行卷积也是一项计算量很大的任务。 There are a few techniques that make it work better: 有一些技巧可以使其更好地工作:

  • The Gaussian kernel can be decomposed into X and Y components, which are computed separately. 高斯核可以分解为X和Y分量,分别进行计算。 This technique is used by image manipulation programs because it is relatively fast and accurate. 图像处理程序使用此技术,因为它相对较快且准确。

  • Instead of using the Gaussian kernel, you can use Poisson disc sampling to blur the image. 您可以使用泊松圆盘采样来模糊图像,而不是使用高斯内核。

  • You can use a multi-pass Kawase filter as an approximation to a Gaussian filter. 您可以使用多通Kawase滤波器作为高斯滤波器的近似值。

  • Sampling from lower resolution mip maps will result in better performance for a given subjective blur quality. 对于给定的主观模糊质量,从较低分辨率的Mip贴图采样将获得更好的性能。

  • 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). 基于GPU的快速实时实时图像模糊算法研究》 (Filip S.,2014)中提供了很好的总结。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM