IOS - 加速将CMSampleBufferRef转换为dlib图像

时间:2017-10-06 08:07:20

标签: c++ ios image-processing dlib opencv4ios

我使用下面的代码将iOS CMSampleBufferRef转换为dlib :: array2d:

- (void)inputImageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly);

    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    char *baseBuffer = (char *)CVPixelBufferGetBaseAddress(imageBuffer);

    if (inputImage.nr() != height) {
        inputImage.set_size(height, width);
    }

    inputImage.reset();
    long position = 0;
    while (inputImage.move_next()) {
        dlib::bgr_pixel& pixel = inputImage.element();

        long bufferLocation = position * 4;
        char r = baseBuffer[bufferLocation];
        char g = baseBuffer[bufferLocation + 1];
        char b = baseBuffer[bufferLocation + 2];

        dlib::bgr_pixel newpixel(b, g, r);
        pixel = newpixel;

        position++;
    }

    CVPixelBufferUnlockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly);
}

我为委托- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection的每一帧做了这个。这个任务的时间处理大约是10毫秒(在Iphone 5S上运行),这对我的需求来说太慢了。有办法加快这项任务吗?

0 个答案:

没有答案