Pixel order for demosaicing

asked 2020-10-15 02:43:46 -0500

maro gravatar image


If I create a bayer pattern (e.g. ndarray:(128,128)) with the pattern RGGB

RG RG ...
GB GB ...

255 0   255 0
0   0     0 0

I would expect that this will be converted to a pure red image (e.g. ndarray:(128,128, 3)) using opencv demosaicing (COLOR_BAYER_RG2BGR). Unfortunately the result is a blue image (BGR ordering), so output[:,:,0] == 255 which would be right if the output would have RGB ordering but I explicitly used COLOR_BAYER_RG2BGR!

Steps to reproduce

import numpy as np
import cv2

rggb = np.zeros((128, 128), np.uint8)
rggb[::2, ::2] = 255
bgr_debayered = cv2.demosaicing(rggb, cv2.COLOR_BAYER_RG2BGR)
cv2.imwrite("debayered_should_be_red_but_is_blue.png", bgr_debayered)

Any help would be appreciated to understand the change in channel ordering here.

edit retag flag offensive close merge delete


if you come to the conclusion that this is either a bug or a deficiency of the documentation, consider opening an issue on the github repo with such a self-contained reproducing example. as for the defines and their effects, you could inspect to the source code of OpenCV. that doesn't help the next guy but tells you what's going on (and you can judge if that's correct or not)

crackwitz gravatar imagecrackwitz ( 2020-10-15 06:19:10 -0500 )edit