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

OpenCV4.2 and Ubuntu16.04 with python2.7? #338

Closed
Algabri opened this issue Jun 2, 2020 · 1 comment
Closed

OpenCV4.2 and Ubuntu16.04 with python2.7? #338

Algabri opened this issue Jun 2, 2020 · 1 comment

Comments

@Algabri
Copy link

Algabri commented Jun 2, 2020

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?

It is possible to run it by OpenCV or not?? If yes, it is easy or difficult??
I read some answer, someone said " it is impossible"

Please your opinions or any suggestions?

@Algabri Algabri changed the title Could I build a cv_bridge with OpenCV4.2, Ubuntu16.04 and python2.7? OpenCV4.2, Ubuntu16.04 and python2.7? Jun 3, 2020
@Algabri Algabri changed the title OpenCV4.2, Ubuntu16.04 and python2.7? OpenCV4.2 and Ubuntu16.04 with python2.7? Jun 3, 2020
@ijnek
Copy link
Member

ijnek commented Sep 12, 2022

Hi @Algabri, I just came maintainer of this repo and I'm sorry this issue didn't get a response previously. As this is related to ROS Kinetic which has reached EOL, I'm going to close the issue. Please feel free to re-open it if the problem persists in the active distributions. Thanks!

@ijnek ijnek closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants