1

I am currently working on a HIL Simulation. I am using CARLA platform, There exist a RGB camera sensor, by which I am able to get a RGB data values (3D array (Height, Width, 3(RGB))) for the Image frame. I want to send the data over network layer, .....

The Question is I would like to convert the RGB format to a Bayer format array. I am not well aware of the details with the Bayer raw filter. I notice that there is a Opencv flag for Bayer to RGB conversions but not the other way round. I need this format to reuse the libraries on the image unpacking or the server side.

Any suggestion regarding conversion from RGB to bayer format would help me a lot moving this project further.

Ref (Pictorial ref - I want to do it from right to left)- https://theailearner.com/2018/10/28/bayer-filter/

  • you could DIY with some numpy slicing and assignment. if you know the bayer grid you need to generate, it's very little work. – Christoph Rackwitz Nov 15 '21 at 17:48
  • Does the camera sensor not have some means of obtaining the raw out, which is before the debayering? Single-chip color cameras use some sort of bayer-type filter and re-construction. *(a notable exception is the Apollo moon landing color camera which used a sequential (spinning) filter)*. – Myndex Aug 10 '23 at 00:43

2 Answers2

2

Here's a naive code to generate the first bayer pattern in that link you have (GRBG?):

import numpy as np # of course

im = cv.imread(...)
(height, width) = im.shape[:2]
(B,G,R) = cv.split(im)

bayer = np.empty((height, width), np.uint8)

# strided slicing for this pattern:
#   G R
#   B G
bayer[0::2, 0::2] = G[0::2, 0::2] # top left
bayer[0::2, 1::2] = R[0::2, 1::2] # top right
bayer[1::2, 0::2] = B[1::2, 0::2] # bottom left
bayer[1::2, 1::2] = G[1::2, 1::2] # bottom right

This emulates image formation. You might want to lowpass (GaussianBlur) the input slightly, or else you'll see funny aliasing artefacts.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
0

Thank you Christoph Rackwithz for your contribution! However, it appears that the code for sampling from the R and B channels has been swapped. Here is the modified code:

Before:

bayer[0::2, 0::2]= G[0::2, 0::2]# top left
bayer[0::2, 1::2]= R[0::2, 1::2]# top right
bayer[1::2, 0::2]= B[1::2, 0::2]# bottom left
bayer[1::2, 1::2]= G[1::2, 1::2]# bottom right

After:

bayer[0::2, 0::2]= G[0::2, 0::2] # top left
bayer[1::2, 0::2]= R[1::2, 0::2] # top right
bayer[0::2, 1::2]= B[0::2, 1::2] # bottom left
bayer[1::2, 1::2]= G[1::2, 1::2] # bottom right