How to convert BGGR 2 RGGB with opencv

This is several years late, but I just stumbled onto this and I don’t think it was made clear that there is no real benefit to this transform. If you require it, it’s likely a hack.

You could demosaic and then resample to a Bayer encoded image, but both transforms will be destructive.

Alternatively, as the difference between RGGB and BGGR is a diagonal translation of the CFA, you can crop (or pad) your buffer by one pixel vertically and horizontally. That will shift the principle point and create edge artifacts where you’ve added pixels, but I think that is probably preferable to the RGGB → BGR → BGGR transform