【问题标题】:Chaining filters with GPUImage in swift在 swift 中使用 GPUImage 链接过滤器
【发布时间】:2014-12-18 11:52:47
【问题描述】:

(第一次在 Stack Overflow 上发帖 - 所以,嗨,“紧张地挥手”)

我使用 GPUImage 库取得了一些成功 - 并且已经设法使用 swift 获得一个简单的过滤器来处理静态图像。

但是,我在尝试将多个过滤器链接在一起时遇到了问题。库中包含的示例似乎没有涵盖这一点。有很多客观的 C 示例,但并不迅速。

谁能举例说明如何申请:

2 个混合滤镜加上一个亮度、对比度和饱和度滤镜到单个静态图像?

我认为这已经足够复杂,足以涵盖 Swift 中库的大部分用途。谢谢。

【问题讨论】:

  • 你试过用 Swift 重写这些 Objective-C 示例吗?你遇到了什么问题?
  • 公平地说,这些示例似乎并未涵盖我能找到的任何特定深度的链接。 (我不太了解Objective c语法,所以如果我遗漏了什么,请原谅)。 (“简单”的例子在这里:github.com/BradLarson/GPUImage/tree/master/examples/iOS)我已经尝试在谷歌上搜索很多快速的例子,但没有太多运气。我想我理解链接理论(我用 coreimage 管理),只是语法从 obj-c 到 swift 的翻译让我对这个库感到困惑。我已经尝试了好几天,因此寻求帮助。 Tnx
  • 我认为我为 obj-c 找到的最佳示例是在 Stack Overflow 上 stackoverflow.com/questions/18265292/… 但是我认为这已经过时了,因为它已经有一年多了。
  • 我不认为它已经过时了。自去年以来,Objective-C 并没有太大变化,我不知道 GPUImage 是否有,但我对此表示怀疑。由于您是 Stack Overflow 上的新手,您可能不知道“为我编写代码”的问题在这里经常不受欢迎。尝试自己做一些事情,如果遇到困难,请向我们展示您到目前为止取得了什么成就,以及您无法解决的具体问题是什么。这样你会学到一些东西,同时你也更有可能得到一个好的答案。

标签: ios swift gpuimage


【解决方案1】:

在 Swift 中分配和链接过滤器与在 Objective-C 中相同,只是语法转换。例如,以下是如何将两个静止图像输入链接到 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];

这是 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()

如您所见,这都是语法,与您实际调用事物的方式没有什么不同。您可以使用 Objective-C 示例代码作为您想要做的事情的基础,然后在您的 Swift 等效程序中重写它。我随框架提供的 Swift 示例要么非常简单(在实时视频上使用单个过滤器的微型应用程序),要么相当复杂(我的测试用例应用程序执行框架中的每个过滤器和操作)。

【讨论】:

  • 非常感谢。你是对的,就像你说的那样,这只是一个语法问题。 Objective C 对我来说不是最清晰的语言(目前),它有点像一个说英语的人试图使用德语教科书学习法语:)
【解决方案2】:

当您使用演示中的 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"}!

然后像这样设置这些过滤器:

    (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

然后你可以像这样链接过滤器:

    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)

【讨论】:

  • 我强烈建议不要使用我在 FilterShowcaseSwift 示例应用程序中创建的数据类型。这些只是为那个应用程序创建的,作为组织和展示我单独为那个项目的测试用例的一种方式。正如我在回答中所描述的那样,直接使用这些类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多