Extend-Robotics / camera_aravis

A ROS1 driver for GenICam based GigE and USB3 cameras.
Other
1 stars 1 forks source link

optimizing PhotoneoYCoCg420 pixel format conversion #13

Closed bmegli closed 1 year ago

bmegli commented 1 year ago

As identified in:

After enabling internal benchmark:

bmegli commented 1 year ago

The current code was taken from Photoneo example:

bmegli commented 1 year ago

In the example code we can read:

/**
 * Utility class providing conversion function for images.
 * Provides conversion
 *     BGR  <--> YCoCg 4:2:0,
 * where the chromatic channels Co, Cg are subsampled in half resolution.
 * The 2x2 plaquette of the YCoCg format consists of:
 *   (0, 0): Y (10 bits), 1st half of Co (6 bits)
 *   (1, 0): Y (10 bits), 2nd half of Co (6 bits)
 *   (0, 1): Y (10 bits), 1st half of Cg (6 bits)
 *   (1, 1): Y (10 bits), 2nd half of Cg (6 bits)
 *
 * Black pixels are treated specially in order to prevent artifacts in images containing valid pixels only in a subregion.
 *
 * NOTE: The YCoCg format used here differs from the reference by:
 *        - a factor 2 in the both chromatic channels Co, Cg,
 *        - an offset equal to the saturation value, which is added to Co and Cg to prevent negative values.
 *
 * @see https://en.wikipedia.org/wiki/YCoCg

    // Subsampling of the chromatic channels Co, Cg.
    // The subsampling is meant both in horizontal and vertical direction.
    static const int chromaSubsampling = 2;

    // Pixel depth of B, G, R, and Y channels. Chromatic channels Co and Cg have an additional bit.
    static const int pixelDepth = 10;

 */
bmegli commented 1 year ago

Examining the code:

It looks as reversible YCoCg-R

YCoCg-R was introduced in this Microsoft paper:

bmegli commented 1 year ago

YCoCg-R -> RGB

The 10 bit depth variant is also described in the VESA standard

"7.7 Color Space Conversion" has algorithm for converting back to RGB

image

bmegli commented 1 year ago

RGB -> YCoCg-R

Looking at Photoneo comments

/*
NOTE: The YCoCg format used here differs from the reference by:
 *        - a factor 2 in the both chromatic channels Co, Cg,
 *        - an offset equal to the saturation value, which is added to Co and Cg to prevent negative values.
 */

This also looks as YCoCg-R defined in VESA

"6.1 Color Space Conversion"

image

bmegli commented 1 year ago

Original

image

bmegli commented 1 year ago

Quick and dirty optimization trial

image

bmegli commented 1 year ago

Asked Photoneo

bmegli commented 1 year ago

Original vs Optimized

We need to fix the clock or check in performance mode to eliminate variability of test result.

Retesting in performance mode

Systems don't run on maxed clocks so the expected behavior in production is closer to previous tests

Original

image

Optimized

image

bmegli commented 1 year ago

That's good enough for now.

In case we need faster processing

bmegli commented 1 year ago

One more test

The second one maps directly to VAAPI bgr0 so avoids another color conversion later (not in the scope of this repository)

The time measured here

RGB8

image

BGRA8

image