Tips for using OpenCV and Cameras

Junhao Wang

Read CompressedImage type

When using Turtlebot in the lab, it only publishs the CompressedImage from '/raspicam_node/image/compressed'. Here's how to read CompressedImage and Raw if you need.

from cv_bridge import CvBridge
def __init__(self):
   self.bridge = CvBridge()
def image_callback(self, msg):
        # get raw image
        # image = self.bridge.imgmsg_to_cv2(msg)
        
        # get compressed image
        np_arr = np.frombuffer(msg.data, np.uint8)
        img_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
        image = cv2.cvtColor(img_np, cv2.COLOR_BGR2HSV)

Limit frame rate

When the computation resource and the bandwidtn of robot are restricted, you need limit the frame rate.

def __init__(self):
   self.counter = 1
def image_callback(self, msg):
        # set frame rate 1/3
        if self.counter % 3 != 0:
            self.counter += 1
            return
        else:
            self.counter = 1

Republish the image

Again, when you have multiple nodes need image from robot, you can republish it to save bandwidth.

Last updated

Was this helpful?