有没有人在iOS上有vImage处理的示例代码?

时间:2010-12-09 19:19:30

标签: iphone image ipad image-processing vimage

我已经阅读了我可以找到的所有文档,但是无法通过简单的vImage调用来工作。我只需要一个真实的例子。文档很少。

4 个答案:

答案 0 :(得分:4)

如果您正在实时处理视频,并且您的输入是在像素缓冲区中处理的,那么如何将该缓冲区复制到vImage中,此时您可以调用您喜欢的任何命令:

- (CVPixelBufferRef)copyRenderedPixelBuffer:(CVPixelBufferRef)pixelBuffer
{ 
    CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

    // vImage processing
    vImage_Error err;
    vImage_Buffer buffer;
    buffer.data = (unsigned char *)CVPixelBufferGetBaseAddress( pixelBuffer );
    buffer.rowBytes = CVPixelBufferGetBytesPerRow( pixelBuffer );
    buffer.width = CVPixelBufferGetWidth( pixelBuffer );
    buffer.height = CVPixelBufferGetHeight( pixelBuffer );
    vImageCVImageFormatRef vformat = vImageCVImageFormat_CreateWithCVPixelBuffer( pixelBuffer );
    vImage_CGImageFormat cgformat = {
        .bitsPerComponent = 8,
        .bitsPerPixel = 32,
        .bitmapInfo = kCGBitmapByteOrderDefault,
        .colorSpace = NULL,    //sRGB
    };
    const CGFloat bgColor[3] = {0.0, 0.0, 0.0};
    vImageBuffer_InitWithCVPixelBuffer(&buffer, &cgformat, pixelBuffer, vformat, bgColor, kvImageNoAllocate);

    vImage_Buffer outbuffer;
    void *tempBuffer;
    tempBuffer = malloc(CVPixelBufferGetBytesPerRow( pixelBuffer ) * CVPixelBufferGetHeight( pixelBuffer ));
    outbuffer.data = tempBuffer;
    outbuffer.rowBytes = CVPixelBufferGetBytesPerRow( pixelBuffer );
    outbuffer.width = CVPixelBufferGetWidth( pixelBuffer );
    outbuffer.height = CVPixelBufferGetHeight( pixelBuffer );

    err = vImageEqualization_ARGB8888(&buffer, &outbuffer, kvImageNoFlags);

    if(err != -1)
        err = vImageContrastStretch_ARGB8888(&buffer, &outbuffer, kvImageNoFlags);

    if(err != -1)
        err = vImageBuffer_CopyToCVPixelBuffer(&outbuffer, &cgformat, pixelBuffer, vformat, bgColor, kvImageNoFlags);

    if(err != -1)
        free(tempBuffer);

    CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

    return (CVPixelBufferRef)CFRetain( pixelBuffer );
}

答案 1 :(得分:1)

更新Ole Bermann的回答:在iOS 5.0中添加了Accelerate框架。

答案 2 :(得分:1)

一个更高级的例子,优化了均衡和拉伸功能(忽略已注释掉的代码,虽然你可以使用它 - 它也有效:

- (CVPixelBufferRef)copyRenderedPixelBuffer:(CVPixelBufferRef)pixelBuffer {

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

unsigned char *base = (unsigned char *)CVPixelBufferGetBaseAddress( pixelBuffer );
size_t width = CVPixelBufferGetWidth( pixelBuffer );
size_t height = CVPixelBufferGetHeight( pixelBuffer );
size_t stride = CVPixelBufferGetBytesPerRow( pixelBuffer );

vImage_Buffer _img = {
    .data = base,
    .height = height,
    .width = width,
    .rowBytes = stride
};

vImage_Error err;
vImage_Buffer _dstA, _dstR, _dstG, _dstB;

err = vImageBuffer_Init( &_dstA, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageBuffer_Init (alpha) error: %ld", err);

err = vImageBuffer_Init( &_dstR, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageBuffer_Init (red) error: %ld", err);

err = vImageBuffer_Init( &_dstG, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageBuffer_Init (green) error: %ld", err);

err = vImageBuffer_Init( &_dstB, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageBuffer_Init (blue) error: %ld", err);

err = vImageConvert_ARGB8888toPlanar8(&_img, &_dstA, &_dstR, &_dstG, &_dstB, kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageConvert_ARGB8888toPlanar8 error: %ld", err);

err = vImageEqualization_Planar8(&_dstR, &_dstR, kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageEqualization_Planar8 (red) error: %ld", err);

err = vImageEqualization_Planar8(&_dstG, &_dstG, kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageEqualization_Planar8 (green) error: %ld", err);

err = vImageEqualization_Planar8(&_dstB, &_dstB, kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageEqualization_Planar8 (blue) error: %ld", err);

err = vImageConvert_Planar8toARGB8888(&_dstA, &_dstR, &_dstG, &_dstB, &_img, kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImageConvert_Planar8toARGB8888 error: %ld", err);

err = vImageContrastStretch_ARGB8888( &_img, &_img, kvImageNoError );
if (err != kvImageNoError)
    NSLog(@"vImageContrastStretch_ARGB8888 error: %ld", err);

err = vImagePermuteChannels_ARGB8888(&_img, &_img, map, kvImageNoFlags);
if (err != kvImageNoError)
    NSLog(@"vImagePermuteChannels_ARGB8888 error: %ld", err);

free(_dstA.data);
free(_dstR.data);
free(_dstG.data);
free(_dstB.data);

CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

return (CVPixelBufferRef)CFRetain( pixelBuffer );

}

答案 3 :(得分:0)

文档不存在,因为iOS上没有vImage库。

vImage自iOS 5.0开始在iOS上可用。 The documentation is here