簡體   English   中英

使用 iOS 上的 GPU 將一個圖像覆蓋在另一個圖像上(視頻幀)

[英]Using the GPU on iOS for Overlaying one image on another Image (Video Frame)

我正在我的應用程序中進行一些圖像處理。 拍攝實時視頻並在其上添加圖像以將其用作疊加層。 不幸的是,這需要大量的 CPU 來完成,這會導致程序的其他部分變慢並且無法按預期工作。 基本上我想讓下面的代碼使用 GPU 而不是 CPU。

- (UIImage *)processUsingCoreImage:(CVPixelBufferRef)input {

    CIImage *inputCIImage = [CIImage imageWithCVPixelBuffer:input];


// Use Core Graphics for this
UIImage * ghostImage = [self createPaddedGhostImageWithSize:CGSizeMake(1280, 720)];//[UIImage imageNamed:@"myImage"];
CIImage * ghostCIImage = [[CIImage alloc] initWithImage:ghostImage];

CIFilter * blendFilter = [CIFilter filterWithName:@"CISourceAtopCompositing"];
[blendFilter setValue:ghostCIImage forKeyPath:@"inputImage"];
[blendFilter setValue:inputCIImage forKeyPath:@"inputBackgroundImage"];

CIImage * blendOutput = [blendFilter outputImage];

EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
NSDictionary *contextOptions = @{ kCIContextWorkingColorSpace : [NSNull null] ,[NSNumber numberWithBool:NO]:kCIContextUseSoftwareRenderer};
CIContext *context = [CIContext contextWithEAGLContext:myEAGLContext options:contextOptions];

CGImageRef outputCGImage = [context createCGImage:blendOutput fromRect:[blendOutput extent]];
UIImage * outputImage = [UIImage imageWithCGImage:outputCGImage];
CGImageRelease(outputCGImage);

return outputImage;}

建議按順序:

  1. 你真的需要合成這兩個圖像嗎? 頂部帶有UIImageViewAVCaptureVideoPreviewLayer是否不足? 然后,您只需將當前的幻影變換應用於圖像視圖(或其層),並讓合成器將兩者粘合在一起,它將使用 GPU。
  2. 如果不是,那么第一個調用端口應該是 CoreImage——它將 GPU 圖像操作包裝到一個相對簡單的 Swift/Objective-C 包中。 有一個簡單的合成過濾器,所以你需要做的就是-imageByApplyingTransform:兩個東西變成CIImage並使用-imageByApplyingTransform:來調整重影。
  3. 兩者都失敗了,那么您正在尋找 OpenGL 解決方案。 您特別希望使用CVOpenGLESTextureCache將核心視頻幀推送到 GPU,而幽靈將只是永久存在於那里。 GLCameraRipple 示例開始了解這些內容,然后查看GLKBaseEffect以免自己需要了解 GLSL(如果您還沒有)。 您需要做的就是打包一些頂點並進行繪圖調用。

最大的性能問題是你創建的每一幀EAGLContextCIContext 這只需在 processUsingCoreImage 方法之外完成一次。

此外,如果您想避免 CPU-GPU 往返,而不是創建核心圖形圖像(createCGImage),因此您可以像這樣直接在 EaglLayer 中渲染 Cpu 處理:

[context drawImage:blendOutput inRect: fromRect: ];
[myEaglContext presentRenderBuffer:G:_RENDERBUFFER];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM