swiftcoremlcvpixelbuffer

How to change CVPixelBuffer data format


I convert UIImage to CVPixelBuffer for the CoreML, but I want change the RGB pixel, like R/1.5, G/2, B/2.5.

I can read the pixel data by using assumingMemoryBound(to : UInt8.self),but how can I modify the pixel data in CVPixelBuffer?


Solution

  • Why do you want to do this in the CVPixelBuffer? Core ML can automatically do this for you as part of the model.

    When you convert your model to Core ML you can specify an image_scale preprocessing option.

    In your case it looks like you want different scales for each color channel, which you can do by adding a scaling layer to the model. See the following Jupyter notebook for an example: https://github.com/apple/coremltools/blob/master/examples/Image_preprocessing_per_channel_scale.ipynb