如何对图像执行快速像素化过滤器? [英] How do I perform a fast pixellation filter on an image?

查看:21
本文介绍了如何对图像执行快速像素化过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的像素化图像处理算法有点问题.

I have a little problem with my pixellation image processing algorithm.

我从头开始将图像加载到 unsigned char* 类型的数组中之后,在需要时,我修改此数据并必须更新图像.这次更新时间太长了.这就是我的做法:

I load the image from the beginning into an array of type unsigned char* After that, when needed, I modify this data and have to update the image. This updating takes too long. This is how I am doing it:

CGDataProviderRef dataProvider = CGProviderCrateWithData(.....);
CGImageRef cgImage = CGImageCreate(....);
[imageView setImage:[UIImage imageWithCGImage:cgImage]]];

一切正常,但处理大图像非常慢.我尝试在后台线程上运行它,但这没有帮助.

Everything is working but it's very slow to process a large image. I tried running this on a background thread, but that didn't help.

所以基本上,这需要太长时间.有没有人知道如何改进它?

So basically, this takes too long. Does anyone have any idea how to improve it?

推荐答案

正如其他人所建议的,您需要将这项工作从 CPU 转移到 GPU,以便在这些移动设备上获得任何一种不错的处理性能.

As others have suggested, you'll want to offload this work from the CPU to the GPU in order to have any kind of decent processing performance on these mobile devices.

为此,我创建了一个名为 GPUImage 的 iOS 开源框架,使其相对做这种加速图像处理很简单.它确实需要 OpenGL ES 2.0 支持,但过去几年销售的每台 iOS 设备都支持这一点(统计数据显示,该领域 97% 的 iOS 设备都支持).

To that end, I've created an open source framework for iOS called GPUImage that makes it relatively simple to do this kind of accelerated image processing. It does require OpenGL ES 2.0 support, but every iOS device sold for the last couple of years has this (stats show something like 97% of all iOS devices in the field do).

作为该框架的一部分,我捆绑的初始过滤器之一是像素化过滤器.SimpleVideoFilter 示例应用程序展示了如何使用它,带有控制处理图像中像素宽度的滑块:

As part of that framework, one of the initial filters I've bundled is a pixellation one. The SimpleVideoFilter sample application shows how to use this, with a slider that controls the pixel width in the processed image:

此过滤器是具有以下 GLSL 代码的片段着色器的结果:

This filter is the result of a fragment shader with the following GLSL code:

 varying highp vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform highp fractionalWidthOfPixel;

 void main()
 {
    highp vec2 sampleDivisor = vec2(fractionalWidthOfPixel);

    highp vec2 samplePos = textureCoordinate - mod(textureCoordinate, sampleDivisor);
    gl_FragColor = texture2D(inputImageTexture, samplePos );
 }

在我的基准测试中,像这样的基于 GPU 的过滤器的执行速度比 iOS 上的图像和视频的等效 CPU 密集型处理例程快 6-24 倍.上面链接的框架应该相当容易集成到应用程序中,并且源代码可免费供您自定义,但您认为合适.

In my benchmarks, GPU-based filters like this perform 6-24X faster than equivalent CPU-bound processing routines for images and video on iOS. The above-linked framework should be reasonably easy to incorporate in an application, and the source code is freely available for you to customize however you see fit.

这篇关于如何对图像执行快速像素化过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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