Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fail to use function in cv_bridge #207

Open
wyixiang opened this issue Apr 4, 2018 · 12 comments
Open

fail to use function in cv_bridge #207

wyixiang opened this issue Apr 4, 2018 · 12 comments
Labels

Comments

@wyixiang
Copy link

wyixiang commented Apr 4, 2018

When I use some function in cv_bridge, I receive an importerror:

Traceback (most recent call last):
File "/home/yixiangw/gym-gazebo/gym_gazebo/envs/turtlebot/gazebo_circuit2c_turtlebot_camera_nn.py", line 217, in _reset
cv_image = CvBridge().imgmsg_to_cv2(image_data, "bgr8")
File "/home/yixiangw/gym-gazebo/gym_gazebo/envs/installation/catkin_ws/src/vision_opencv/cv_bridge/python/cv_bridge/core.py", line 163, in imgmsg_to_cv2
dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
File "/home/yixiangw/gym-gazebo/gym_gazebo/envs/installation/catkin_ws/src/vision_opencv/cv_bridge/python/cv_bridge/core.py", line 99, in encoding_to_dtype_with_channels
return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding))
File "/home/yixiangw/gym-gazebo/gym_gazebo/envs/installation/catkin_ws/src/vision_opencv/cv_bridge/python/cv_bridge/core.py", line 91, in encoding_to_cvtype2
from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)

so how can I solve it ?

@walid19
Copy link

walid19 commented Aug 31, 2018

i am having the same problem did you find the solution?

@wyixiang
Copy link
Author

@walid19 In my try, cv_bridge is available for python2 and cannot be used in py3. You can try running in python2.

@Akashbaskaran
Copy link

Was anyone able to use python3 and solve this issue?

@ghost
Copy link

ghost commented Jul 30, 2019

Found a way around. This helped http://www.programmersought.com/article/4908420840/

@qgallouedec
Copy link

You could use numpy directly, instead of cv_bridge

import numpy as np
im = np.frombuffer(image_data.data, dtype=np.uint8).reshape(image_data.height, image_data.width, -1)

@felixvd
Copy link

felixvd commented Mar 8, 2021

↑↑↑ A real MVP, just like in the legends

If you're missing the desired_encoding parameter, use cv2.cvtColor with these codes.

import cv2
im_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

@nerdneilsfield
Copy link

Is there anyway to implement the same thing as CvBridge().cv2_to_imgmsg rather than using cv_bridge?

@andrewjong
Copy link

andrewjong commented Jul 26, 2021

A different solution worked for me. I'm also trying to use Python 3 with CVBridge. I'm using conda as my environment manager. For me the solution was to configure catkin like so:

catkin config \
-DPYTHON_EXECUTABLE=/home/andrew/.miniconda3/envs/myenv/bin/python \
-DPYTHON_INCLUDE_DIR=/home/andrew/.miniconda3/envs/myenv/include/python3.7m/ \
-DPYTHON_LIBRARY=/home/andrew/.miniconda3/envs/myenv/lib/libpython3.7m.so

After adding these CMake Args, I no longer get the error. Note if you're using a different version of conda / conda-python, you may have to adjust your -DPYTHON_INCLUDE_DIR and -DPYTHON_LIBRARY to point to the correct files as they're slightly different in some versions. I'm using ROS melodic.

@kilichzf
Copy link

I was having the same error message but not with this repository.
I am writing this because it may help somebody.
In my case I forgot to source cv2_bridge package for python 3 and trying to use python 2 one.

@tamerlan-b
Copy link

tamerlan-b commented Sep 30, 2021

Beautiful solution is to use ros_numpy. It is similar to the function of cv_bridge, but without the dependency on cv2:

import ros_numpy
np_img = ros_numpy.numpify(ros_img_msg)

@arthurBricq
Copy link

arthurBricq commented Jun 20, 2022

If you are trying to not use cv_bridge when publishing an image to ROS (the other direction as the one of this issue): ndarray (or cv2) --> ROS, here is a snippet of code that does the job using the methods mentionned just above.

Assuming that img is a numpy array containing your image:

ros_image = Image(encoding="mono8")
# Create the header
ros_image.header.stamp = rospy.Time.from_sec(t)
ros_image.header.frame_id = id
# Fill the image data 
ros_image.height, ros_image.width = img.shape
ros_image.data = img.ravel().tobytes() # or .tostring()
ros_image.step=ros_image.width

@DanielTakeshi
Copy link

DanielTakeshi commented Jan 23, 2023

@qgallouedec where are you getting image_data in your example code? I have some "msg" which I am using with bridge.imgmsg_to_cv2(msg, 'bgr8') where bridge = CvBridge().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests