1

tldr: does OpenCV adopt a different Bayer ordering than what's commonly used?

I'm trying to capture an image with a camera with a Bayer sensor, using the drivers provided by the camera company and a Python wrapper. The issue that I'm facing is that I expect a RGB image as output, but I get a BGR one instead.

I'm converting the image using cv2.cvtColor(image, cv2.COLOR_BAYER_BG2RGB), which seems the correct conversion, because both the drivers and the camera company GUI to interact with the camera show the Bayer mosaic as BayerBG.

Since the image comes out as bgr, either I'm using the wrong conversion code, or the camera company's drivers are at fault (which seems highly unlikely).

Here and here are two other questions that kinda touch on the problem, but don't solve my doubts.

My suspicion on the issue is that what chip manufacturers and OpenCV consider as Bayer ordering does not match. For instance, consider the following:

B G B G B G
G R G R G R
B G B G B G
G R G R G R

I would expect the above Bayer format to be referred as BayerBG (which is what I see when using the camera company's software and drivers), however I suspect from here (bottom of page) that OpenCV would consider that as BayerRG format, due to the following quote from that page:

The two letters C1 and C2 in the conversion constants CV_Bayer C1C2 2BGR and CV_Bayer C1C2 2RGB indicate the particular pattern type. These are components from the second row, second and third columns, respectively.

Am I correct in my interpretation that OpenCV adopts a different Bayer ordering than what's commonly used?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • OpenCV uses the BGR ordering everywhere. If you create an RGB image with this code, and then show it with OpenCV’s display or save it to file with OpenCV’s save function, it will have channels reversed. If you use OpenCV functionality to show and save the file, create the image in BGR order. Use `COLOR_BAYER_BG2BGR`. – Cris Luengo Jul 17 '23 at 13:52
  • I remember that the bayer defines used to be named in funny ways. I hope someone cleaned that up since then. you may have to read the source to be sure. – Christoph Rackwitz Jul 17 '23 at 22:59
  • please use documentation that isn't out of date: https://docs.opencv.org/4.x/de/d25/imgproc_color_conversions.html this lists proper naming besides the weird obsolete naming – Christoph Rackwitz Jul 17 '23 at 23:01
  • @ChristophRackwitz thank you for the tip, the most recent version of the documentation is actually clearer on the difference between classical and OpenCV Bayer naming. I was looking at the 3.2.0 documentation because, unfortunately, I'm stuck on that version of OpenCV (don't ask...). – user1627317 Jul 18 '23 at 07:30

0 Answers0