Skip to content

Converting frames between pixel formats

Adwyzz (OLED Edition) edited this page Jan 22, 2022 · 2 revisions

You got a camera feed. Awesome! Only issue is that the pixel format might not be what you need.

How do we make sure the camera uses the correct pixel format and convert the frame to a new one if it isn't?

The FrameConverter class will take care of that.

The FrameConverter class has multiple constructors, but the easiest way to convert a frame is:

FrameConverter converter = new FrameConverter(frame, PixelFormat.Rgba); // Convert to a new pixel format without changing the image.
// Do NOT create a new FrameConverter for every frame
Frame converted = converter.Convert(frame);

It will convert the frame to the new pixel format, even if it is already correct, meaning you do have to dispose the source frame.

Remember that a FrameConverter is tied to a specific pixel format and size, so if the size or pixel format changes, you'll have to dispose the current instance and make a new one.

Additionally, the FrameConverter class can scale frames too.

FrameConverter converter = new FrameConverter(frame, 1920, 1080, PixelFormat.Rgba); // Convert to a new pixel format and scale the image to 1920x1080.
// Do NOT create a new FrameConverter for every frame
Frame converted = converter.Convert(frame);

As always, remember to dispose the FrameConverter when not needed anymore by calling Dispose()