简体   繁体   English

快速链接具有GPUImage的滤镜

[英]Chaining filters with GPUImage in swift

(First post to Stack Overflow - So, Hi 'waves nervously') (Stack Overflow的第一篇文章-因此,嗨,“紧张地挥手”)

I'm using the GPUImage library with some success - and have managed to get a simple filter working on a static image using swift. 我正在成功使用GPUImage库-并设法使用swift获得了一个用于静态图像的简单滤镜。

However I'm having problems trying to chain multiple filters together. 但是我在尝试将多个过滤器链接在一起时遇到问题。 The included examples in the library don't seem to cover this. 库中包含的示例似乎并未涵盖这一点。 There are plenty of objective C examples but not swift. 有很多客观的C示例,但并不迅速。

Can anyone please give an example of how to apply: 任何人都可以举例说明如何申请:

2 blend filters plus a brightness, contrast and saturation filter to a single static image? 2个混合滤镜以及一个亮度,对比度和饱和度滤镜可用于单个静态图像?

I think this is sufficiently complex to cover most uses of the library in Swift. 我认为这足够复杂,无法涵盖Swift中该库的大多数用法。 Thanks. 谢谢。

Allocating and chaining filters in Swift is the same as it is in Objective-C, it's just syntactic conversion. Swift中的分配和链接过滤器与Objective-C中的相同,只是语法转换。 For example, the following is how you'd chain two still image inputs to a blend filter in Objective-C, then have the result of that blend be directed to a contrast filter, with a capture of your final image: 例如,以下是将两个静态图像输入链接到Objective-C中的混合滤镜,然后将该混合的结果定向到对比度滤镜并捕获最终图像的方式:

GPUImageOverlayBlendFilter *blendFilter = [[GPUImageOverlayBlendFilter alloc] init];
[stillImageSource1 addTarget:blendFilter];
[stillImageSource2 addTarget:blendFilter];

GPUImageContrastFilter *contrastFilter = [[GPUImageContrastFilter alloc] init];
[blendFilter addTarget:contrastFilter];

[contrastFilter useNextFrameForImageCapture];
[stillImageSource1 processImage];
[stillImageSource2 processImage];

UIImage *currentFilteredImage = [contrastFilter imageFromCurrentFramebuffer];

This is the equivalent in Swift: 这在Swift中是等效的:

let blendFilter = GPUImageOverlayBlendFilter()
stillImageSource1.addTarget(blendFilter)
stillImageSource2.addTarget(blendFilter)

let contrastFilter = GPUImageContrastFilter()
blendFilter.addTarget(contrastFilter)

contrastFilter.useNextFrameForImageCapture()
stillImageSource1.processImage()
stillImageSource2.processImage()

let currentFilteredImage = contrastFilter.imageFromCurrentFramebuffer()

As you can see, it's all syntax, nothing different in how you actually call things. 如您所见,这都是语法,实际调用方式没有什么不同。 You can use the Objective-C example code as a basis for what you want to do, and just rewrite that in your Swift equivalents. 您可以使用Objective-C示例代码作为您想要做的事情的基础,而只需用Swift的等效代码重写它即可。 The Swift examples that I ship with the framework are either really simple (the tiny application that uses a single filter on live video) or fairly complex (my test case application that executes every filter and operation in the framework). 我随框架附带的Swift示例非常简单(在实时视频上使用单个过滤器的微型应用程序)或相当复杂(在框架中执行每个过滤器和操作的测试用例应用程序)。

When you use the FilterOperations.swift class from the demo, then you could initialize filters like this: 当您使用演示中的FilterOperations.swift类时,可以像这样初始化过滤器:

// Quick reference to the used filter configurations
var filterExposure = filterOperations.firstMatch {item in return item.listName == "Exposure"}!
var filterHighlightShadow = filterOperations.firstMatch {item in return item.listName == "Highlights and shadows"}!
var filterSaturation = filterOperations.firstMatch {item in return item.listName == "Saturation"}!
var filterContrast  = filterOperations.firstMatch {item in return item.listName == "Contrast"}!
var filterAdaptiveThreshold  = filterOperations.firstMatch {item in return item.listName == "Adaptive threshold"}!

And then setup these filters like this: 然后像这样设置这些过滤器:

    (self.filterExposure.filter as GPUImageExposureFilter).exposure = 0.8 // -10 - 10
    (self.filterHighlightShadow.filter as GPUImageHighlightShadowFilter).highlights = 1.0 // 0 - 1
    (self.filterSaturation.filter as GPUImageSaturationFilter).saturation = 0.0 // 0 - 2
    (self.filterContrast.filter as GPUImageContrastFilter).contrast = 2.0  // 0 - 4
    (self.filterAdaptiveThreshold.filter as GPUImageAdaptiveThresholdFilter).blurRadiusInPixels = 8.0

And then you could chain the filters like this: 然后,您可以像这样链接过滤器:

    videoCamera.addTarget((self.filterExposure.filter as GPUImageInput))
    self.filterExposure.filter.addTarget((self.filterHighlightShadow.filter as GPUImageInput))
    self.filterHighlightShadow.filter.addTarget((self.filterSaturation.filter as GPUImageInput))
    self.filterSaturation.filter.addTarget((self.filterContrast.filter as GPUImageInput))
    self.filterContrast.filter.addTarget((self.filterAdaptiveThreshold.filter as GPUImageInput))
    self.filterAdaptiveThreshold.filter.addTarget(self.filterView)

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

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