ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Which Opencv should I use for ros Noetic?

asked 2022-11-11 16:11:23 -0600

Hannes Bauer gravatar image

updated 2022-11-12 07:56:20 -0600

ravijoshi gravatar image

Hi, I am trying to implement OpenCV into a roscpp. I already managed to compile it successfully, even though it did not work to read and print a Image. It prints me this as a warning, which is the reason why it should not work I think. I have installed OpenCV 4.6.0

/usr/bin/ld: Warnung:, benötigt von /usr/local/lib/, kann in Konflikt geraten mit
/usr/bin/ld: Warnung:, benötigt von /usr/lib/x86_64-linux-gnu/, kann in Konflikt geraten mit

CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 2.8)


find_package(catkin REQUIRED COMPONENTS

find_package(OpenCV 4 REQUIRED )

  #INCLUDE_DIRS include 
  #LIBRARIES Zajicek
  #CATKIN_DEPENDS geometry_msgs nav_msgs roscpp sensor_msgs std_msgs
  #DEPENDS system_lib


add_executable(OpenCV2 src/opencv_test2.cpp)

target_link_libraries(OpenCV2 ${catkin_LIBRARIES} ${OpenCV_LIBS} )

The code is a simple read and print code like this:

#include < opencv2/opencv.hpp>
#include < opencv2/core.hpp>
#include < opencv2/imgcodecs.hpp>
#include < opencv2/highgui.hpp>
#include < string>
#include < iostream>
#include < stdio.h>
#include " ros/ros.h"

int main(int argc, char* argv[])
  std::string image_path = "/home/nico/CATKINWS/src/OpenCV2/Loewe.jpg";

  // cv::Mat img= cv::imread("/home/nico/Greyscale.png", cv::IMREAD_GRAYSCALE);
  cv::Mat img;
  img = cv::imread(image_path, cv::IMREAD_COLOR);

  if (img.empty())
    std::cout << "Could not read the image: " << image_path << std::endl;
    return -1;
    cv::namedWindow("OpenCV Image", cv::WINDOW_AUTOSIZE);
    cv::imshow("Display window", img);
    cv::waitKey(0);  // Wait for a keystroke in the window
    return 0;

It prints the following message:

"Could not read the image: /home/nico/CATKINWS/src/OpenCV2/Loewe.jpg"

So it get stuck in the if clause when img.empty()

I tried nearly the same code without ROS, and compiled it with cmake . and it worked as it should, So my guess is that OpenCV 4.6.0 is not compatible! So my question now is, which version should I use? Is my guess for the problem correct? and can I just download an old version and change the find package in the CMakeLists.txt to make it run, or do I have to delete the 4.6 version too? Thank you in advance!

edit retag flag offensive close merge delete


You do not need to compile OpenCV separately because ROS binaries comes along with OpenCV. Please install ros-noetic-desktop-full. On the other hand, in my computer, I can see that ROS Noetic is using OpenCV 4.2.0.

ravijoshi gravatar image ravijoshi  ( 2022-11-11 22:29:52 -0600 )edit

May I ask how you mean that I don't have to compile opencv alone? I generally compile the program with catkin_make ? I just tried without ros with cmake and it worked?

Apart from that I installed the ros-noetic-desktop-full? Should I now delete opencv4.6 and download 4.2? Best regards

Hannes Bauer gravatar image Hannes Bauer  ( 2022-11-12 08:02:18 -0600 )edit

@Hannes Bauer You have confusingly named your own ros package "opencv". Anyone you share this code with is going to be confused because of the system library with a very similar name.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-11-12 11:42:32 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2022-11-12 11:40:09 -0600

Mike Scheutzow gravatar image

If you want to use the binary ros apt repository, noetic requires your code to compile with the ubuntu libopencv-dev package, which is opencv version 4.2 in ubuntu 20. Most people install this library from apt.

It takes much more effort for you to use any other version of opencv in noetic. You will need to compile any ros-packages that use opencv from source code, and you will have to debug the problems created by the API changes that the opencv devs make frequently.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2022-11-11 16:11:23 -0600

Seen: 107 times

Last updated: Nov 12 '22