cv-bridge

raw JSON →
1.13.0.post0 verified Fri May 01 auth: no python

CvBridge converts between ROS Image messages and OpenCV images. Current version is 1.13.0.post0. Release cadence follows ROS distributions.

pip install cv-bridge
error ImportError: No module named cv_bridge
cause cv-bridge package not installed.
fix
Run pip install cv-bridge or install ROS environment with cv-bridge.
error AttributeError: module 'cv_bridge' has no attribute 'CvBridge'
cause Trying to import CvBridge from wrong location, e.g., `import cv_bridge` then `cv_bridge.CvBridge()`.
fix
Use correct import: from cv_bridge import CvBridge then instantiate with CvBridge().
error RuntimeError: cv_bridge exception: ...
cause Invalid encoding string or incompatible image formats.
fix
Ensure encoding matches the actual image type. Common: 'bgr8', 'rgb8', 'mono8'.
gotcha Forgetting to specify `desired_encoding` results in default encoding which may not match expected format. Always specify encoding explicitly.
fix Use `desired_encoding='bgr8'` (or appropriate encoding) in `imgmsg_to_cv2`.
gotcha OpenCV uses BGR channel order, but most other libraries use RGB. Converting to/from ROS Image without specifying encoding can swap channels.
fix Always set encoding to 'bgr8' for OpenCV compatibility.
deprecated The `cv_bridge.boost.cv_bridge_boost` module is deprecated and may be removed in future versions.
fix Use `from cv_bridge import CvBridge` instead of importing boost submodules.

Basic usage of CvBridge to convert between ROS Image messages and OpenCV images.

import cv2
from cv_bridge import CvBridge
from sensor_msgs.msg import Image

bridge = CvBridge()
# Convert a ROS Image message to OpenCV image
cv_image = bridge.imgmsg_to_cv2(image_message, desired_encoding='bgr8')
# Convert back
image_message = bridge.cv2_to_imgmsg(cv_image, encoding='bgr8')