Skip to content

Instantly share code, notes, and snippets.

@walkoncross
Last active March 20, 2023 16:49
Show Gist options
  • Save walkoncross/9e2074a0efc2710302be49ec668b5249 to your computer and use it in GitHub Desktop.
Save walkoncross/9e2074a0efc2710302be49ec668b5249 to your computer and use it in GitHub Desktop.
Numpy / OpenCV image BGR to RGB

(from https://www.scivision.co/numpy-image-bgr-to-rgb/)

Numpy / OpenCV image BGR to RGB

Conversion between any/all of BGR, RGB, and GBR may be necessary when working with Matplotlib expects M x N x 3 image, where last dimension is RGB.

OpenCV expects M x N x 3 image, where last dimension is BGR.

Scientific Cameras, some of which output an M X N x 3 image, where last dimension is GBR

Python BGR to RGB code

BGR to RGB

OpenCV image to Matplotlib

rgb = bgr[...,::-1]

RGB to BGR

Matplotlib image to OpenCV

bgr = rgb[...,::-1]

RGB to GBR

gbr = rgb[...,[2,0,1]]

Axis order for Python images

3-D: W x H x 3, where the last axis is color (e.g. RGB) 4-D: W x H x 3 x 1 is typically an alpha channel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment