2

如何使用 vImage 从图像中“提取”一个通道?我现在的做法是:

vImage_Buffer redBuffer;
redBuffer.data = (void*)redImageData.bytes;
redBuffer.width = size.width;
redBuffer.height = size.height;
redBuffer.rowBytes = [redImageData length]/size.height;
vImage_Buffer redBuffer2;
redBuffer2.width = size.width;
redBuffer2.height = size.height;
redBuffer2.rowBytes = size.width;
vImageConvert_RGB888toPlanar8(&redBuffer, &redBuffer2, nil, nil, kvImageNoFlags);

redBuffer 是一个工作图像,没有错误,但 vImageConvert 给了我一个 EXC_BAD_ACCESS,我尝试了很多选项,有些导致 EXC_BAD_ACCESS 和一些导致输出图像损坏。我究竟做错了什么?

因此,多亏了 Rob Keniger,我设法提取了通道,但是如果我尝试将它们与其他通道重新组合在一起,图像会被水平拉伸并带有 RGB 条纹。为什么他们没有正确地重新组合在一起?这是一个提取通道然后将它们重新组合在一起的示例:

    vImage_Buffer blueBuffer;
    blueBuffer.data = (void*)blueImageData.bytes;
    blueBuffer.width = size.width;
    blueBuffer.height = size.height;
    blueBuffer.rowBytes = [blueImageData length]/size.height;

    vImage_Buffer rBuffer;
    rBuffer.width = size.width;
    rBuffer.height = size.height;
    rBuffer.rowBytes = size.width;
    void *rPixelBuffer = malloc(size.width * size.height);
    if(rPixelBuffer == NULL)
    {
        NSLog(@"No pixelbuffer");
    }
    rBuffer.data = rPixelBuffer;

    vImage_Buffer gBuffer;
    gBuffer.width = size.width;
    gBuffer.height = size.height;
    gBuffer.rowBytes = size.width;
    void *gPixelBuffer = malloc(size.width * size.height);
    if(gPixelBuffer == NULL)
    {
        NSLog(@"No pixelbuffer");
    }
    gBuffer.data = gPixelBuffer;

    vImage_Buffer bBuffer;
    bBuffer.width = size.width;
    bBuffer.height = size.height;
    bBuffer.rowBytes = size.width;
    void *bPixelBuffer = malloc(size.width * size.height);
    if(bPixelBuffer == NULL)
    {
        NSLog(@"No pixelbuffer");
    }
    bBuffer.data = bPixelBuffer;

    vImageConvert_RGB888toPlanar8(&blueBuffer, &rBuffer, &gBuffer, &bBuffer, kvImageNoFlags);

    size_t destinationImageBytesLength = size.width*size.height*3;
    const void* destinationImageBytes = valloc(destinationImageBytesLength);
    NSData* destinationImageData = [[NSData alloc] initWithBytes:destinationImageBytes length:destinationImageBytesLength];
    vImage_Buffer destinationBuffer;
    destinationBuffer.data = (void*)destinationImageData.bytes;
    destinationBuffer.width = size.width;
    destinationBuffer.height = size.height;
    destinationBuffer.rowBytes = [destinationImageData length]/size.height;

    vImage_Error result = vImageConvert_Planar8toRGB888(&rBuffer, &gBuffer, &bBuffer, &destinationBuffer, 0);
    NSImage* image = nil;
    if(result == kvImageNoError)
    {
        //TODO: If you need color matching, use an appropriate colorspace here
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)(destinationImageData));
        CGImageRef finalImageRef = CGImageCreate(size.width, size.height, 8, 24, destinationBuffer.rowBytes, colorSpace, kCGBitmapByteOrder32Big|kCGImageAlphaNone, dataProvider, NULL, NO, kCGRenderingIntentDefault);
        CGColorSpaceRelease(colorSpace);
        CGDataProviderRelease(dataProvider);
        image = [[NSImage alloc] initWithCGImage:finalImageRef size:NSMakeSize(size.width, size.height)];
        CGImageRelease(finalImageRef);
    }
    free((void*)destinationImageBytes);
    return image;
4

3 回答 3

3

一个解决方案(或者,至少是一个解决方案的开始):

err = vImageExtractChannel_ARGB8888(&_src, &_blu, 2, kvImageNoError);
...
uint8_t copyMask = 1;
vImageOverwriteChannels_ARGB8888(&_blu, &_src, &_dst, copyMask, kvImageNoFlags);

这对我有用,除了我正在开发的 iOS 相机应用程序的 OpenCV 版本;预览窗口在一侧显示一条白色条纹,每次 OpenCV 不喜欢我做某事的方式时都会出现这种情况。我不认为这将是您的问题,并且由于您的多行代码只有三行代码,因此值得尝试。

于 2015-05-11T23:58:48.093 回答
2

您没有初始化redBuffer2输出缓冲区,因此您没有任何地方可以存储函数的输出。

//allocate a buffer for the output image and check it exists
void *pixelBuffer = malloc(size.width * size.height);
if(pixelBuffer == NULL)
{
    NSLog(@"No pixelbuffer"); 
    //handle this somehow
}

//assign the allocated buffer to the vImage buffer struct
redBuffer2.data = pixelBuffer;
于 2013-07-17T06:03:31.213 回答
2

vImageConvert_RGB888toPlanar8 不接受 vImage_Buffer 结构的 NULL 指针。该函数用 VIMAGE_NON_NULL(1,2,3,4) 标记,这应该会导致您的编译器抱怨这种用法。

在 OS X.10 或 iOS 8 上添加了 vImageExtractChannel_ARGB8888,但没有等效的 RGB888。如果您需要,您应该提交功能请求。http://bugreporter.apple.com 同时,您可以制作三个plane8 输出缓冲区并丢弃两个。

于 2014-08-02T18:05:19.220 回答