vendredi 3 juillet 2020

How to convert CMSampleBufferRef/CIImage/UIImage into pixels e.g. uint8_t[]

I have input from captured camera frame as CMSampleBufferRef and I need to get the raw pixels preferably in C type uint8_t[]. I also need to find the color scheme of the input image.

I know how to convert CMSampleBufferRef to UIImage and then to NSData with png format but I dont know how to get the raw pixels from there. Perhaps I could get it already from CMSampleBufferRef/CIImage`?

This code shows the need and the missing bits. Any thoughts where to start?

int convertCMSampleBufferToPixelArray (CMSampleBufferRef sampleBuffer)
{
    // inputs
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
    CIContext *imgContext = [CIContext new];
    CGImageRef cgImage = [imgContext createCGImage:ciImage fromRect:ciImage.extent];
    UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
    NSData *nsData = UIImagePNGRepresentation(uiImage);
    

    // Need to fill this gap
    uint8_t* data = XXXXXXXXXXXXXXXX;
    ImageFormat format = XXXXXXXXXXXXXXXX; // one of: GRAY8, RGB_888, YV12, BGRA_8888, ARGB_8888


    // sample showing expected data values
    // this routine converts the image data to gray
    //
    int width = uiImage.size.width;
    int height = uiImage.size.height;
    const int size = width * height;
    std::unique_ptr<uint8_t[]> new_data(new uint8_t[size]);
    for (int i = 0; i < size; ++i) {
        new_data[i] = uint8_t(data[i * 3] * 0.299f + data[i * 3 + 1] * 0.587f +
                          data[i * 3 + 2] * 0.114f + 0.5f);
    }

    return 1;
}

Aucun commentaire:

Enregistrer un commentaire