How to add multiple filters to UIImage GPUImage?

本文介绍如何在iOS应用中使用GPUImage库为UIImage添加多个滤镜效果,并提供了具体的代码示例。此外,还讨论了如何正确地切换不同的滤镜效果。

from: http://www.scriptscoop.net/t/174138f67b0f/ios-how-to-add-multiple-filters-to-uiimage-gpuimage.html

I noticed a lot of people asking questions about linking filters with GPUImage. I can't quite figure out how to do it succinctly. Finally got it working tonight. Just wanted to share my code so people can link to the solution.

1 answer

By chrisallickBest answer

UIImage *faceImage = [UIImage imageNamed:@"469453586_640.jpg"];
UIImageView *face = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, faceImage.size.width/2.0, faceImage.size.height/2.0)];
[face setImage:faceImage];
[self.view addSubview:face];

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:faceImage];

GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
[brightnessFilter setBrightness:.15];
GPUImageGrayscaleFilter *grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];

GPUImagePosterizeFilter *posterizeFilter = [[GPUImagePosterizeFilter alloc] init];
[posterizeFilter setColorLevels:1];


[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:grayscaleFilter];
[grayscaleFilter addTarget:posterizeFilter];

// these need to be changed if you change the order of your filters
//    [brightnessFilter useNextFrameForImageCapture];
//    [grayscaleFilter useNextFrameForImageCapture];
[posterizeFilter useNextFrameForImageCapture];

[stillImageSource processImage];

[face setImage: [posterizeFilter imageFromCurrentFramebuffer]];

2 similar answers  β 

By ophychius

I have an app that sort of does the same, and what I do is that I keep my baseImage at hand, and once I am done applying the filter and showing the image with filter I reset everything. The moment a user selects a different filter I use the baseImage again to create the new filtered image and show it.

So in this example you could for example move the [sourcePicture removeAllTargets]; to right after this [self.view addSubview:filterView];

That saves you work when you want to apply a new filter. Also, there is a quicker (dirtier way to filter a picture when you are working with an image that already exists. I got this from the documentation that came with GPUImage and it works like a charm.

GPUImageSketchFilter *stillImageFilter2 = [[GPUImageSketchFilter alloc] init];
tmp = [stillImageFilter2 imageByFilteringImage:[self baseImage]];
[self.imageView setImage: tmp];
tmp = nil;

Hope it helps you on your way.

By user4211235

You can sometimes get hints/code for your apps via the open source repo for the Rally App Catalog. For your example, there is available source code for the Release Planning App. Reviewing the source code, you can see that the Filter Picker is defined by the following requirement defined in the source:

Rally.ui.gridboard.plugin.GridBoardCustomFilterControl

And this is incorporated into the board by adding its plugin to the board configuration.

It's tempting to add this to a Simple Grid example, exactly as the Release planning board does, which I tried doing as follows:

<!DOCTYPE html>
<html>
<head>
    <title>Rally Example: Simple Board</title>

    <script type="text/javascript" src="/apps/2.0rc3/sdk.js"></script>

    <script type="text/javascript">
        Rally.onReady(function () {
                Ext.define('Rally.example.SimpleBoard', {
                    extend: 'Rally.app.App',
                    requires: [
                        'Rally.ui.gridboard.plugin.GridBoardCustomFilterControl'
                    ],

                    launch: function() {
                        this.add({
                            xtype: 'rallycardboard',
                            types: ['User Story'],
                            attribute: 'ScheduleState',
                            context: this.getContext(),
                            readOnly: true,
                            cardConfig: {
                                showIconsAndHighlightBorder: false,
                                editable: false
                            },
                            plugins: [
                                {
                                    ptype: 'rallygridboardcustomfiltercontrol',
                                    filterChildren: false,
                                    filterControlConfig: {
                                        margin: '3 9 3 30',
                                        blackListFields: ['PortfolioItemType', 'Release'],
                                        whiteListFields: [this._milestonesAreEnabled() ? 'Milestones' : ''],
                                        modelNames: ['HierarchicalRequirement']                                           
                                    }
                                }
                            ]
                        });
                    }
                });

            Rally.launchApp('Rally.example.SimpleBoard', {
                name:"Rally Example: Simple Board",
                parentRepos:""
            });

        });
    </script>


    <style type="text/css">
        .app {
  /* Add app styles here */
}

    </style>
</head>
<body>
</body>
</html>

However, if you try to load the app in this way, you'll get a 404 when it looks for the Rally.ui.gridboard.plugin.GridBoardCustomFilterControlclass.

Looking at the AppSDK2.0rc3 docs, this plugin does not appear to be available under the Rally.ui.cardboard.plugins.* tree that's bundled into the SDK. See screenshot here:

AppSDK2.0rc3 screenshot excerpt:

AppSDK2.0rc3 screenshot excerpt

Nor does it appear that the Rally.ui.gridboard.plugin.* tree is bundled into the AppSDK. It is likely that the class is however, available to the Rally UI via a different javascript bundle (non-public) that the Rally developers use.

Perhaps it would be feasible for Rally Engineering to bundle this plugin into the AppSDK so that customer developers could use it - perhaps file a Feature Request on Rally Ideas or something like that to see if this is achievable.

Similar Posts
how to add multiple filters to mapbox leaflet maps
 nrutasVotes: 0 Relevancy: 90%
I have a working mapbox/leaflet map and I can filter based on dropdowns but only one of them will work, not sure of the syntax (or if it's possible) to combine filters? I basically have a real estate map populated with json data which includes property types and neighborhoods. need to combine the possible filters, so selecting a different property type won't erase the neighborhood filter.
ng-repeat: how to add multiple filters?
 SpearfisherVotes: 1 Relevancy: 87%
I'd like to use multiple filters on the same item for an ng-repeat. The idea is that for each job below I have different properties like location and salary and I'd like to be able to filter the results with both criteria. So far, I've tried this. But it's obviously not working. Does anyone have a clue how to fix this? Thanks
UISearchDisplayController: Add multiple filters to an existing filter
 AlexRVotes: 0 Relevancy: 84%
I am using a UISearchDisplayController the default way to filter the cells of a UITableViewController by its text values (e.g. to show only the cells which start with the letters 'ab'). The values in the cells of the table view are filled by a NSFetchedResultsController. In addition to this text based filtering, I would like to add additional filtering capabilities based on the numeric range of the items (e.g.
How can I get a UIImage from GPUImage camera?
 Aaron BratcherVotes: 2 Relevancy: 81%
I know I can use the still camera's capturePhotoAsImageProcessedUpToFilter method, but it gives a shutter click sound and I still have some processing to do so I don't want it to sound yet. I tried using a filter's imageFromCurrentFramebuffer method, but that is always turning nil.
Proper way to rotate UIImage with GPUImage
 kevVotes: 3 Relevancy: 78%
I want to rotate a large UIImage using GPUImage because it's so much faster than core graphics, at least on iOS devices. Here's what I'm doing. The only problem is that I get a strip of white pixels on the end (the black strips are not part of the image). How do I get rid of that white strip? Note. I am using other filters on top of the rotation filter. Maybe there's an issue in conjunction with other filters.
How do I apply apply multiple filters that require multiple images using GPUImage
 user379468Votes: 0 Relevancy: 75%
I'm using GPUImage 0.1.2. Currently I have a GPUImageChromaKeyBlendFilter that is bing applied to GPUImage picture (picture 1) and that picture is in turn added to another picture (picture 2) So in essence picture one is composited over picture 2. Now in addition I also want to apply a mask filter to picture 1, but I cant seem to figure out how to chain the filters correctly, I've looked into using filter groups, but it hasn't gotten me any closer.
Multiple Filters using GPUImage Library
 Helium3Votes: 0 Relevancy: 72%
I am trying to apply 3 filters to an image. One rgbFilter which is has its values constant, a brightness filter and a saturation filter, both of which should be able to be modified and the image should update. I have followed the advice here. I have setup a UIView using IB and set its class to GPUImageView. For some reason the image doesnt show. My steps are as follows. and then I call this which sets the constant values on the rgb filter I setup my filters before this using.
Generating UIImage from GPUImage video frame
 John Michael ZorkoVotes: 0 Relevancy: 69%
I'm trying to generate a UIImage from a video frame captured by GPUImage. I've done a lot of AVFoundation video work, but i'm new to using GPUImage. I've subclassed GPUImageVideoCamera and added this method, but the UIImage is always nil. If anyone can tell me where i've gone so horribly wrong, i'd be very appreciative! { [super processVideoSampleBuffer. sampleBuffer]; // to let GPUImage do it's processing first
Convolution UIImage with GPUImage framework
 TonyVotes: 0 Relevancy: 66%
I'm trying to use GPUImage3x3ConvolutionFilter of GPUImage framework but is not working. This is my code, I only get a white image. I'm also try changing. for.
iOS Invert Colors on Application
 Emrah AyanogluVotes: 1 Relevancy: 63%
I'd like to invert all colors in my application like Settings > Accessibility > Invert Colors. Is there a way to make this stuff in a programmatically way in iOS such using CALayer or Filters like GPUImage or CoreImage?
xcode5: how can I save and load an image?
 user3550084Votes: -1 Relevancy: 60%
I'm developing an app, where I can apply filters on an image. I want to create an undo button which resets the original image in the imageView.The solution is that I just save the original image in it's own UIImage object before I apply any filters to it. This way I can just go back to that in my undo method. Does somebody know how I can do this?
How to apply HSB color filters to UIImage
 saerosVotes: 2 Relevancy: 57%
I've been struggling for a few days for a project on UIImage colorization.The idea is that the app will embark a set of images that I will have to colorize with values retrieved from a webservice. Some sort of themes if you wish. The designer I work with gave me a background image on all of his Photoshop values. The first problem is that Photoshop uses HSL and iOS uses HSB. So the first challenge was to translate the values from Photoshop. Photoshop HSL.
Add multiple UIImage copies using UIStepper
 user3156776Votes: 0 Relevancy: 54%
I want to add/delete images in a view using a UIStepper. So every time you press the '+' the same image is copied within the same viewController, still being able to move/rotate every copy. I have something like this. but this does not work because it just replaces the previous image. Is there a proper way to copy the same image multiple times using the UIStepper?
JQuery DataTables multiple filters
 aiguoferVotes: 0 Relevancy: 51%
I have a table to display the raw data from a Rickshaw graph and I want to update it whenever I use the Rickshaw controls. Currently, here's the relevant code. The problem with this is that each time it's going to redraw, in the worst case it has to run through the whole filter. Is there a way that I could add each filter separately (one for the slider and one for the legend)?
How to add multiple route filters in Laravel 4.2 while using role:permission pattern?
 Ali GajaniVotes: 0 Relevancy: 48%
I am having issues with using multiple route filters in Laravel 4.2 while using the pattern role. permission. I've attached my code below. This doesn't work at all. When I change roles, it always give one 403 unauthorized. I want both moderator and administrator to access this route. Perhaps there's a way to tell Laravel, "if the logged in user is either an administrator OR a moderator, then let them access this route". This is my role filter.
Adding multiple filters to jquery find
 nexuscreatorVotes: 0 Relevancy: 45%
I am extending this question.I have this following code. How can I add filter to remove disabled input fields and having tabindex greater than 0? So far I have tried combining with .not('. disabled') and .not('input[tabindex>"0"]'), but those are not working.
Objectify multiple filters doesn´t work with cron job
 WeiniVotes: 0 Relevancy: 42%
I´m working with objectify on appengine, I tried to add a cron job to delete all temp entities which are older than an hour. but i always get an Exception when executing the cron job on the appengine server. does anybody know why this happens? The job works if I remove.
Angularfire and jQM: Multiple filters not working
 ivyVotes: 0 Relevancy: 39%
I'm using Angularfire with jQuery Mobile (mostly for the CSS). I'm working on a website where you can keep track of your books, which are stored in a Firebase. I have a page (using jQM's page navigation) where all my books are listed in a listview, and the search/filter works perfectly fine. The problem starts when I add another page with a different listview and search bar to search through the books I've read. The search bar on the second page doesn't work, even when the first one does.
Play Framework Multiple filters in Global.java
 max1221Votes: 2 Relevancy: 36%
I'm using Play Framework 2.3.2 (Java version) I was wondering how I would go about adding multiple filters to the filters() override in Global.java? I have this to enable the CSRF Filter. and I'd like to now also add the Gzip filter. What's the correct syntax to use to have both the CSRF filter and GZIP compression? It's described here. http. //www.playframework.com/documentation/2.3.x/GzipEncoding but it doesn't say how to add that as a filter when one already exists. Thanks in advance!


【电力系统】单机无穷大电力系统短路故障暂态稳定Simulink仿真(带说明文档)内容概要:本文档围绕“单机无穷大电力系统短路故障暂态稳定Simulink仿真”展开,提供了完整的仿真模型与说明文档,重点研究电力系统在发生短路故障后的暂态稳定性问题。通过Simulink搭建单机无穷大系统模型,模拟不同类型的短路故障(如三相短路),分析系统在故障期间及切除后的动态响应,包括发电机转子角度、转速、电压和功率等关键参数的变化,进而评估系统的暂态稳定能力。该仿真有助于理解电力系统稳定性机理,掌握暂态过程分析方法。; 适合人群:电气工程及相关专业的本科生、研究生,以及从事电力系统分析、运行与控制工作的科研人员和工程师。; 使用场景及目标:①学习电力系统暂态稳定的基本概念与分析方法;②掌握利用Simulink进行电力系统建模与仿真的技能;③研究短路故障对系统稳定性的影响及提高稳定性的措施(如故障清除时间优化);④辅助课程设计、毕业设计或科研项目中的系统仿真验证。; 阅读建议:建议结合电力系统稳定性理论知识进行学习,先理解仿真模型各模块的功能与参数设置,再运行仿真并仔细分析输出结果,尝试改变故障类型或系统参数以观察其对稳定性的影响,从而深化对暂态稳定问题的理解。
本研究聚焦于运用MATLAB平台,将支持向量机(SVM)应用于数据预测任务,并引入粒子群优化(PSO)算法对模型的关键参数进行自动调优。该研究属于机器学习领域的典型实践,其核心在于利用SVM构建分类模型,同时借助PSO的全局搜索能力,高效确定SVM的最优超参数配置,从而显著增强模型的整体预测效能。 支持向量机作为一种经典的监督学习方法,其基本原理是通过在高维特征空间中构造一个具有最大间隔的决策边界,以实现对样本数据的分类或回归分析。该算法擅长处理小规模样本集、非线性关系以及高维度特征识别问题,其有效性源于通过核函数将原始数据映射至更高维的空间,使得原本复杂的分类问题变得线性可分。 粒子群优化算法是一种模拟鸟群社会行为的群体智能优化技术。在该算法框架下,每个潜在解被视作一个“粒子”,粒子群在解空间中协同搜索,通过不断迭代更新自身速度与位置,并参考个体历史最优解和群体全局最优解的信息,逐步逼近问题的最优解。在本应用中,PSO被专门用于搜寻SVM中影响模型性能的两个关键参数——正则化参数C与核函数参数γ的最优组合。 项目所提供的实现代码涵盖了从数据加载、预处理(如标准化处理)、基础SVM模型构建到PSO优化流程的完整步骤。优化过程会针对不同的核函数(例如线性核、多项式核及径向基函数核等)进行参数寻优,并系统评估优化前后模型性能的差异。性能对比通常基于准确率、精确率、召回率及F1分数等多项分类指标展开,从而定量验证PSO算法在提升SVM模型分类能力方面的实际效果。 本研究通过一个具体的MATLAB实现案例,旨在演示如何将全局优化算法与机器学习模型相结合,以解决模型参数选择这一关键问题。通过此实践,研究者不仅能够深入理解SVM的工作原理,还能掌握利用智能优化技术提升模型泛化性能的有效方法,这对于机器学习在实际问题中的应用具有重要的参考价值。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值