如何转换YUV至iOS版CIImage [英] How to convert from YUV to CIImage for iOS

查看:543
本文介绍了如何转换YUV至iOS版CIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将YUV图像转换为CIIMage并最终转换为UIImage。我是相当新手,试图找出一个简单的方法来做到这一点。从我所学到的,从iOS6 YUV可以直接用于创建CIImage,但是当我试图创建它时,CIImage只保留一个零值。我的代码是这样的 - >

I am trying to convert a YUV image to CIIMage and ultimately UIImage. I am fairly novice at these and trying to figure out an easy way to do it. From what I have learnt, from iOS6 YUV can be directly used to create CIImage but as I am trying to create it the CIImage is only holding a nil value. My code is like this ->

NSLog(@"Started DrawVideoFrame\n");

CVPixelBufferRef pixelBuffer = NULL;

CVReturn ret = CVPixelBufferCreateWithBytes(
                                            kCFAllocatorDefault, iWidth, iHeight, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
                                            lpData, bytesPerRow, 0, 0, 0, &pixelBuffer
                                            );

if(ret != kCVReturnSuccess)
{
    NSLog(@"CVPixelBufferRelease Failed");
    CVPixelBufferRelease(pixelBuffer);
}

NSDictionary *opt =  @{ (id)kCVPixelBufferPixelFormatTypeKey :
                      @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) };

CIImage *cimage = [CIImage imageWithCVPixelBuffer:pixelBuffer options:opt];
NSLog(@"CURRENT CIImage -> %p\n", cimage);

UIImage *image = [UIImage imageWithCIImage:cimage scale:1.0 orientation:UIImageOrientationUp];
NSLog(@"CURRENT UIImage -> %p\n", image);

这里的lpData是YUV数据,它是一个无符号字符数组。

Here the lpData is the YUV data which is an array of unsigned character.

这看起来也很有趣: vImageMatrixMultiply ,找不到任何关于此的例子。任何人都可以帮我吗?

This also looks interesting : vImageMatrixMultiply, can't find any example on this. Can anyone help me with this?

推荐答案

我也面临过类似的问题。我试图将YUV(NV12)格式的数据显示到屏幕上。此解决方案适用于我的项目......

I have also faced with this similar problem. I was trying to Display YUV(NV12) formatted data to the screen. This solution is working in my project...

//YUV(NV12)-->CIImage--->UIImage Conversion
NSDictionary *pixelAttributes = @{kCVPixelBufferIOSurfacePropertiesKey : @{}};
CVPixelBufferRef pixelBuffer = NULL;

CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault,
                                      640,
                                      480,
                                      kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
                                      (__bridge CFDictionaryRef)(pixelAttributes),
                                      &pixelBuffer);

CVPixelBufferLockBaseAddress(pixelBuffer,0);
unsigned char *yDestPlane = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

// Here y_ch0 is Y-Plane of YUV(NV12) data.
memcpy(yDestPlane, y_ch0, 640 * 480); 
unsigned char *uvDestPlane = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);

// Here y_ch1 is UV-Plane of YUV(NV12) data. 
memcpy(uvDestPlane, y_ch1, 640*480/2);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

if (result != kCVReturnSuccess) {
    NSLog(@"Unable to create cvpixelbuffer %d", result);
}

// CIImage Conversion    
CIImage *coreImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

CIContext *MytemporaryContext = [CIContext contextWithOptions:nil];
CGImageRef MyvideoImage = [MytemporaryContext createCGImage:coreImage
                                                    fromRect:CGRectMake(0, 0, 640, 480)];

// UIImage Conversion
UIImage *Mynnnimage = [[UIImage alloc] initWithCGImage:MyvideoImage 
                                                 scale:1.0 
                                           orientation:UIImageOrientationRight];

CVPixelBufferRelease(pixelBuffer);
CGImageRelease(MyvideoImage);

这里我展示了YUV(NV12)数据的数据结构以及我们如何获得Y-Plane (y_ch0)和UV-Plane(y_ch1),用于创建CVPixelBufferRef。让我们看一下YUV(NV12)数据结构。
如果我们查看图片,我们可以获得有关YUV(NV12)的信息,

Here I am showing data structure of YUV(NV12) data and how we can get the Y-Plane(y_ch0) and UV-Plane(y_ch1) which is used to create CVPixelBufferRef. Let's look at the YUV(NV12) data structure.. If we look at the picture we can get following information about YUV(NV12),


  • 总框架尺寸=宽度*高度* 3/2,

  • Y平面尺寸=框架尺寸* 2 / 3,

  • 紫外线平面尺寸=框架尺寸* 1/3,

  • 存储在Y平面中的数据 - > {Y1,Y2, Y3,Y4,Y5 .....}。

  • U-Plane - >(U1,V1,U2,V2,U3,V3,......}。

  • Total Frame Size = Width * Height * 3/2,
  • Y-Plane Size = Frame Size * 2/3,
  • UV-Plane size = Frame Size * 1/3,
  • Data stored in Y-Plane -->{Y1, Y2, Y3, Y4, Y5.....}.
  • U-Plane-->(U1, V1, U2, V2, U3, V3,......}.

我希望它对所有人都有帮助。:)享受IOS开发的乐趣:D

I hope it will be helpful to all. :) Have fun with IOS Development :D

这篇关于如何转换YUV至iOS版CIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆