Hey everyone,
I’m trying to get a better more in-depth understanding of the demosaicing algorithm for converting BayerBG images to RGB in OpenCV.
I understand the basic concepts that I’ve seen around the internet such as summing up neighboring pixels of the target color and averaging them but I’m not sure if OpenCV is even doing that.
Here’s a Python example to demonstrate what I’m talking about:
We create a simple image with 3 rows and 4 columns in Bayer BG pattern:
src = np.uint8([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
So this should follow a structure like this:
R G R G => 1 2 3 4
G B G B => 5 6 7 8
R G R G => 9 10 11 12
And then when I run this in the console:
>>> src = np.uint8([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> cv.demosaicing(src, cv.COLOR_BayerBG2RGB)
array([[[6, 6, 6],
[6, 6, 6],
[7, 7, 7],
[7, 7, 7]],
[[6, 6, 6],
[6, 6, 6],
[7, 7, 7],
[7, 7, 7]],
[[6, 6, 6],
[6, 6, 6],
[7, 7, 7],
[7, 7, 7]]], dtype=uint8)
This is output I wouldn’t actually expect!
Namely, let’s look at pixel [0][0]
.
We get a pixel value of array([6, 6, 6], dtype=uint8)
.
From what I understand, we shouldn’t be doing anything to the red channel here because that part of our Bayer pattern is supposed to already have the red component in it!
OpenCV claims this algorithm is supposed to follow a simple bilinear interpolation but it seems at odds with the results I’m getting.
Does anyone know what’s going on here?