3

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

我的计划是获取 Y 平面baseAddress并将其设置为imageData类似的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;
}

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

- (void) writeIplImage:(IplImage*) iplImage toPixelBufferRef:(CVPixelBufferRef) output
{
    CVPixelBufferCreateWithBytes(NULL,
                                 iplImage->width,
                                 iplImage->height,
                                 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
                                 iplImage->imageData,
                                 iplImage->widthStep,
                                 releaseImage,
                                 iplImage,
                                 NULL,
                                 &output);

}

我希望这很简单,但我就是无法让它发挥作用。我生成的图像是空的。
有什么想法我可能做错了吗?

4

0 回答 0