ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

cv_bridge and IplImage -> Mat when going from OpenCV 2.1 to OpenCV 2.3?

asked 2012-02-13 05:10:02 -0600

Pi Robot gravatar image

updated 2012-02-13 05:31:29 -0600


I am trying to use some of the newer Python sample code included with OpenCV 2.3 while also using cv_bridge in ROS Electric. It appears that most of the sample code (such as the demo) assumes images are in Mat format rather than IplImage format. However, cv_bridge converts ROS Image format to IplImage format so that I am constantly running into format incompatibilities. Is there some way I can have cv_bridge convert ROS Image messages to Mat format instead of IplImage? Alternatively, can I convert the IplImage returned by cv_bridge to Mat? I am programming in Python so I am hoping for a solution in that language.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-02-13 05:35:25 -0600

updated 2012-02-13 14:07:02 -0600

Pi Robot gravatar image

Since OpenCV seems to be moving away from IplImages, it would make sense for cv_bridge to do the same. Perhaps that should be ticketed.

edit flag offensive delete link more


Many thanks @diaz! Do you happen to know what type of Mat is returned by GetMat? For example, if I use 'frame = cv.GetMat(self.bridge.imgmsg_to_cv(ros_image, "bgr8"))', what Mat type is frame? e.g. 8UC3, 32FC3? etc. I ask because I'm still having size and/or type compatibility problems when trying to copy or perform other operations with the 'frame' matrix and other image matrices in my code.

Pi Robot gravatar image Pi Robot  ( 2012-02-13 06:21:30 -0600 )edit

I'm not sure what the correspondence is between IplImage encodings and the regular Mat encodings, but it should be semantically equivalent. bgr8 would likely give you 8UC3 or something similar. When creating new Mats of the same type, you can be encoding-agnostic by using mat.type and mat.channels.

Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2012-02-13 06:44:49 -0600 )edit

Thanks again @diaz. Yes, it looks like its type 8UC3 or 16 in integer format.

Pi Robot gravatar image Pi Robot  ( 2012-02-13 07:29:43 -0600 )edit

Could you post code that requires converting to a numpy array? Most OpenCV functions work on array-like objects, and Mats certainly are. I just tried cv.CvtColor using a Mat as the source and destination, and it works fine.

Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2012-02-13 10:09:11 -0600 )edit

OK, I jumped the gun. It looks like it is only the functions in cv2 that require a numpy array. In the demo the convert function used is cv2.cvtColor and this requires a numpy array whereas the old cv.CvtColor does not. Similarly, the new cv2.goodFeaturesToTrack requires numpy arrays.

Pi Robot gravatar image Pi Robot  ( 2012-02-13 14:09:49 -0600 )edit

Question Tools

1 follower


Asked: 2012-02-13 05:10:02 -0600

Seen: 2,490 times

Last updated: Feb 13 '12