rerun icon indicating copy to clipboard operation
rerun copied to clipboard

Support images in BGR and BGRA

Open abey79 opened this issue 2 years ago • 7 comments

Describe the annoyance

There is generally no consensus between RGB and BGR image formats. For example, Pillow's PIL.Image.open() yields RGB format (including when the image is converted to a Numpy array). OpenCV's cv2.imread() or VideoCapture instead yield BGR numpy arrays.

rr.log_image() expects RGB and yields false colours otherwise. ~~At the very least, this should be documented.~~ Ideally, some optional format argument should be added so alternative formats may be correctly ingested.

To Reproduce This code yields false color (e.g. blue skins):

import cv2
import rerun as rr

rr.init("demo")
rr.spawn()
image = cv2.imread("image.jpg")
rr.log_image("image", image)

This is fixed by adding explicit format conversion:

# ... 
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
rr.log_image("image", image)

Ideally, something like that would be possible:

rr.log_image("image", image, format=rr.BGR)

abey79 avatar Jun 08 '23 16:06 abey79

FWIW np.array(mediapipe.Image.create_from_file("image.jpg").numpy_view()) is RGB

abey79 avatar Jun 08 '23 19:06 abey79

This is related to adding more semantics to color tensors in general:

  • RGB, BGR, RGBA, …
  • Linear vs gamma
  • Data range (0-255, 0-65535, …) (https://github.com/rerun-io/rerun/issues/2341)
  • Color map

emilk avatar Jun 27 '23 09:06 emilk

The quick-fix is just to reorder the data into RGB on the SDK side.

The deeper fix is adding a "ColorSemantics" component or similar.

emilk avatar Jun 27 '23 09:06 emilk

rr.log_image is no more, but the same applies for the rr.Image archetype.

abey79 avatar Nov 06 '23 11:11 abey79

Note some places in the code indicate we should change our defaults to BGR.

I do NOT think we should do this. BGR is a bad, legacy choice from opencv. It should not be our default.

jleibs avatar Aug 01 '24 00:08 jleibs

The simple way to implement BGR and BGRA right now is to upload them as RGB(A) textures, and add a swap_red_blue: bool flag to the rectangle.wgsl shader

emilk avatar Aug 06 '24 11:08 emilk

Blocked on

  • https://github.com/rerun-io/rerun/issues/7100

emilk avatar Aug 08 '24 06:08 emilk

not blocked by it, we can have bgr ofc without it being editable

Wumpf avatar Aug 20 '24 09:08 Wumpf