[英]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;}
建議按順序:
UIImageView
的AVCaptureVideoPreviewLayer
是否不足? 然后,您只需將當前的幻影變換應用於圖像視圖(或其層),並讓合成器將兩者粘合在一起,它將使用 GPU。-imageByApplyingTransform:
兩個東西變成CIImage
並使用-imageByApplyingTransform:
來調整重影。CVOpenGLESTextureCache
將核心視頻幀推送到 GPU,而幽靈將只是永久存在於那里。 從GLCameraRipple 示例開始了解這些內容,然后查看GLKBaseEffect
以免自己需要了解 GLSL(如果您還沒有)。 您需要做的就是打包一些頂點並進行繪圖調用。最大的性能問題是你創建的每一幀EAGLContext和CIContext 。 這只需在 processUsingCoreImage 方法之外完成一次。
此外,如果您想避免 CPU-GPU 往返,而不是創建核心圖形圖像(createCGImage),因此您可以像這樣直接在 EaglLayer 中渲染 Cpu 處理:
[context drawImage:blendOutput inRect: fromRect: ];
[myEaglContext presentRenderBuffer:G:_RENDERBUFFER];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.