【问题标题】:UIImage color correctionUIImage色彩校正
【发布时间】:2013-06-01 07:05:06
【问题描述】:

我正在使用AVFoundation 来播放视频,方法是从AVFoundation 回调创建一个CGImage,从CGImage 创建一个UIImage,然后在UIImageView 中显示UIImage

我想在将图像显示在屏幕上之前对它们进行一些颜色校正。为我得到的图像着色的最佳方法是什么?

我尝试使用CIFilters,但这需要我首先从AVFoundation 创建一个CIImage,然后对其进行着色,然后创建一个CGImage,然后创建一个UIImage,我宁愿如果可能,请避免创建CIImage 的额外步骤。

此外,CIFilters 的性能似乎不够快 - 至少在还必须创建额外的 CIImage 时不会。有什么更快的方法来做这件事的建议吗?

提前致谢。

【问题讨论】:

  • 你看过GPUImage框架吗?它不会让你远离使用中间格式,但在大多数情况下,过滤器的运行速度比 CIFilters 快得多。 github.com/BradLarson/GPUImage
  • 总体上看起来是一个很酷的库,但我认为它不能满足我的需要,因为我需要 AVFoundation 人脸检测和颜色校正。

标签: ios performance video image-processing avfoundation


【解决方案1】:

似乎使用EAGLContext 而不是标准的CIContext 是答案。这为根据我的需要创建彩色图像提供了足够快的性能。

这里是简单的代码示例:

在初始化时:

NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject: [NSNull null] forKey: kCIContextWorkingColorSpace];
m_EAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
m_CIContext = [CIContext contextWithEAGLContext:m_EAGLContext options:options];

设置颜色:

-(void)setColorCorrection:(UIColor*)color
{  
  CGFloat r,g,b,a;
  [color getRed:&r green:&g blue:&b alpha:&a];

  CIVector *redVector = [CIVector vectorWithX:r Y:0 Z:0];
  CIVector *greenVector = [CIVector vectorWithX:0 Y:g Z:0];
  CIVector *blueVector = [CIVector vectorWithX:0 Y:0 Z:b];

  m_ColorFilter = [CIFilter filterWithName:@"CIColorMatrix"];
  [m_ColorFilter setDefaults];
  [m_ColorFilter setValue:redVector forKey:@"inputRVector"];
  [m_ColorFilter setValue:greenVector forKey:@"inputGVector"];
  [m_ColorFilter setValue:blueVector forKey:@"inputBVector"];
}

在每个视频帧上:

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CGImageRef cgImage = nil;

CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

[m_ColorFilter setValue:ciImage forKey:kCIInputImageKey];
CIImage *adjustedImage = [m_ColorFilter valueForKey:kCIOutputImageKey];

cgImage = [m_CIContext createCGImage:adjustedImage fromRect:ciImage.extent];

【讨论】: