Summary:
I'm using new vision of opencv, opencv 4.2 with ubuntu 16.04 and python 2.7
After built cv_bridge
, the output is:
import rospkg
ImportError: No module named rospkg
Here's the more detailed version: here
I tried to build cv_bridge in order to run OpenCV 4.2, Ubuntu 16.04 (ROS Kinetic) and python 2.7.
I will explain what I do for that.
I built it before with OpenCV 3.3.1 in order to use GPU and it was very fine and it is still working now. To best my knowledge, there is ros-kinetic-opencv3
, it is working only on CPU. I removed it using sudo apt remove ros-kinetic-opencv3
After removing, I just downloaded your here and put in my ~/catkin_ws/src/
, then making ~/catkin_ws/catkin_make
on my PC without any modifications. I think that I was lucky.
Now, I'd like use some advantage of OpenCV 4.2 that is not with older versions like OpenCV 3.3.1. Meanwhile, I can't update robot's system, it is Ubuntu 16.04.
So, I tried to build cv_bridge
, all modifications as following:
I copied and pasted module_opencv3.cpp
, then change its name to module_opencv4.cpp
so that became three models 4, 3 and 2.cpp instead of two 3 and 2.cpp.
Next , Sreevan mentioned here some changes, I did like Sreevan:
In cv_bridge/src/module_opencv3.cpp
change signature of two functions
3.1) UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, int flags, UMatUsageFlags usageFlags) const
to
UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, AccessFlag flags, UMatUsageFlags usageFlags) const
3.2) bool allocate(UMatData* u, int accessFlags, UMatUsageFlags usageFlags) const
to
bool allocate(UMatData* u, AccessFlag accessFlags, UMatUsageFlags usageFlags) const
Next, I went to this file ~/catkin_ws/src/vision_opencv/cv_bridge$ CMakeLists.txt
and change it as:
from:
find_package(OpenCV 3 REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
PATHS /usr/local
NO_DEFAULT_PATH
CONFIG
)
to:
if ( NOT (OpenCV_VERSION_MAJOR VERSION_LESS 4) )
find_package(OpenCV REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
#PATHS /usr/local/include
PATHS /usr/local
NO_DEFAULT_PATH
CONFIG
)
else()
find_package(OpenCV 3 REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
PATHS /usr/local
NO_DEFAULT_PATH
CONFIG
)
endif()
Next, I went to this file ~/catkin_ws/src/vision_opencv/cv_bridge/src$ CMakeLists.txt
and change it as:
from:
if (OpenCV_VERSION_MAJOR VERSION_EQUAL 3)
add_library(${PROJECT_NAME}_boost module.cpp module_opencv3.cpp)
else()
add_library(${PROJECT_NAME}_boost module.cpp module_opencv2.cpp)
endif()
to
if (OpenCV_VERSION_MAJOR VERSION_EQUAL 4)
add_library(${PROJECT_NAME}_boost module.cpp module_opencv4.cpp)
elseif(OpenCV_VERSION_MAJOR VERSION_EQUAL 3)
add_library(${PROJECT_NAME}_boost module.cpp module_opencv3.cpp)
else()
add_library(${PROJECT_NAME}_boost module.cpp module_opencv2.cpp)
endif()
Next, I think this step is not important, I went to this file ~/catkin_ws/src/vision_opencv/cv_bridge$ package.xml
and change any opencv3
to opencv4
Finally, ~/catkin_ws$ catkin_make
everything was ok but when I run the code:
import rospy
import cv2
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
print(cv2.__version__)
class CVControl:
def __init__(self):
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.img_callback)
def img_callback(self, data):
try:
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as e:
print e
frame = cv_image
cv2.imshow("Image window", frame)
cv2.waitKey(3)
def main():
ctrl = CVControl()
rospy.init_node('image_converter')
try:
rospy.spin()
except KeyboardInterrupt:
print "Shutting down"
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
It runs by OpenCV 3.3.1
If I need to run it by OpenCV 4.2 I added those lines in first the code:
import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if ros_path2 and ros_path3 in sys.path:
sys.path.remove(ros_path2)
sys.path.remove(ros_path3)
The output is:
import rospkg
ImportError: No module named rospkg
What is wrong?