CVPixelBufferRef到IplImage并返回

时间:2013-08-01 05:10:54

标签: iphone ios objective-c opencv image-processing

我正在尝试将CVPixelBufferRef (YUV 4:2:0)转换为灰度 IplImage然后返回。

我的计划是获取Y平面baseAddress并将其设置为imageData的{​​{1}},如:

IplImage

要转换回来,我只是打电话:

- (IplImage*) CreateIplImageFromCVPixelBuffer:(CVPixelBufferRef) cvImageBuffer and:(int) outChannels
{
    CVPixelBufferLockBaseAddress(cvImageBuffer, kCVPixelBufferLock_ReadOnly);

    void *baseAddress = CVPixelBufferGetBaseAddressOfPlane(cvImageBuffer, 0);
    size_t width = CVPixelBufferGetWidth(cvImageBuffer);
    size_t height = CVPixelBufferGetHeight(cvImageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(cvImageBuffer);

    IplImage *iplImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);

    //Fill it in.
    iplImage->nSize       = sizeof(IplImage);
    iplImage->ID          = 0;
    iplImage->nChannels   = 1;
    iplImage->depth       = IPL_DEPTH_8U;
    iplImage->dataOrder   = 0;
    iplImage->origin      = 0;
    iplImage->width       = width;
    iplImage->height      = height;
    iplImage->roi         = 0;
    iplImage->maskROI     = 0;
    iplImage->imageId     = 0;
    iplImage->tileInfo    = 0;
    iplImage->imageSize   = CVPixelBufferGetDataSize(cvImageBuffer);
    iplImage->imageData   = (char*)baseAddress;
    iplImage->widthStep   = bytesPerRow;
    iplImage->imageDataOrigin = (char*)baseAddress;

    CVPixelBufferUnlockBaseAddress(cvImageBuffer, kCVPixelBufferLock_ReadOnly);

    return iplImage;
}

我预计这会非常直接,但我无法让它发挥作用。我的结果图像是空的 我可能做错了什么想法?

0 个答案:

没有答案