How to: receive and use the 2D RGB image

Receiving RGB data with ifm3d is done similarly as 3D data: the core objects have to be instantiated, and a frame has to be retrieved (see full code below). The important part is how to access the RGB image and how to decode it for further use.

Access the data

The RGB image is stored in JPEG format and can be retrieved as follows:

jpeg = frame.get_buffer(buffer_id.JPEG_IMAGE)

Decode the data

Once accessed, the RGB image has to be decoded. We use OpenCV in this example:

rgb_decode = cv2.imdecode(jpeg, cv2.IMREAD_UNCHANGED)

Display (optional)

The decoded image can then be displayed, for instance with OpenCV.

Note that in c++, the image first has to be converted to a cv::Mat. Follow the full example for the conversion to cv::Mat with or without copy.

cv2.startWindowThread()
cv2.namedWindow("2D image", cv2.WINDOW_NORMAL)
# get frame
# ...
... 
cv2.imshow('RGB image', rgb_decode)
cv2.waitKey(0)

The full example

import time
import cv2
from ifm3dpy.device import O3R
from ifm3dpy.framegrabber import FrameGrabber, buffer_id


def callback(self):
    rgb = cv2.imdecode(self.get_buffer(buffer_id.JPEG_IMAGE), cv2.IMREAD_UNCHANGED)
    cv2.imshow("2D image", rgb)
    cv2.waitKey(1)


# Initialize the objects
o3r = O3R()
port = "port0"
fg = FrameGrabber(o3r, pcic_port=50010)

# Change port to RUN state
config = o3r.get()
config["ports"][port]["state"] = "RUN"
o3r.set(config)

# Register a callback and start streaming frames
fg.on_new_frame(callback)
fg.start([buffer_id.JPEG_IMAGE])

time.sleep(10)
# Stop the streaming
fg.stop()