UIImage颜色校正

时间:2013-06-05 14:15:55

标签: ios performance video image-processing avfoundation

我正在使用AVFoundation通过CGImage回调创建AVFoundation来播放视频,从UIImage创建CGImage,然后显示UIImage中的UIImageView

我希望在我在屏幕上显示之前对图像应用一些颜色校正。将我正在拍摄的图像着色的最佳方法是什么?

我尝试使用CIFilters,但这要求我首先从CIImage创建AVFoundation,然后对其着色,然后创建CGImage,然后创建{ {1}},如果可能的话,我宁愿避免额外的步骤来创建UIImage

此外,CIImage的性能似乎不够快 - 至少在创建额外的CIFilters时也是如此。有什么建议可以更快地实现这一目标吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

似乎使用EAGLContext代替标准CIContext就是答案。这样可以在为我的需求创建彩色图像时提供足够快的性能。

这里的简单代码示例:

On Init:

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];