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

ROS opencv image publisher

asked 2021-06-24 16:00:27 -0600

sgttrieu gravatar image

I run a simple program to continuously publish image to ROS server. I cannot get rid of below error, anyone know the solution please help.

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>

int main(int argc, char** argv)
  ros::init(argc, argv, "image_publisher");
  ros::NodeHandle nh;

  image_transport::ImageTransport it(nh);
  image_transport::Publisher pub = it.advertise("camera/image", 1);
  cv::Mat image = cv::imread(argv[1], cv::IMREAD_COLOR);
  sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();

 ros::Rate loop_rate(5);
  while (nh.ok()) {


[ 97%] Built target vtd2ros_generate_messages_lisp
[ 97%] Built target vtd2ros_generate_messages
/usr/bin/ld: cannot find -lopencv_xphoto}
collect2: error: ld returned 1 exit status
make[2]: *** [vtd2ros/CMakeFiles/camera.dir/build.make:207: `/home/robotest/RoboTest/Vu_2/vtd2ros/devel/lib/vtd2ros/camera] Error 1`
make[1]: *** [CMakeFiles/Makefile2:685: vtd2ros/CMakeFiles/camera.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Invoking "make -j16 -l16" failed

The content of CMakelist.txt

find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg message_generation cv_bridge image_transport)
find_package( OpenCV REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(camera src/camera.cpp )
add_dependencies(camera ${camera_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})



OpenCV version 4.2.0

I also install sudo apt-get install ros-noetic-vision-opencv

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-06-25 04:47:47 -0600

sgttrieu gravatar image

The problem is solved. I did not pay attention that I leave a " } " in the target_link_libraries configuration.

edit flag offensive delete link more

answered 2021-06-25 02:46:05 -0600

Ranjit Kathiriya gravatar image

updated 2021-06-25 04:30:26 -0600

Hello there,

I think that your linker can't find the OpenCV library. A SImilar issue was raised over here.

You want to achieve the linkers that can find the OpenCV library. For that

The solution there was to make a link for the library to a known lib location:

sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/

Another option:

Assuming that OpenCV library located in /usr/lib/x86_64-linux-gnu/ you can also add the library folder to the Libraries path:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/"

You may need to update the "Dynamic Linker":

sudo ldconfig
edit flag offensive delete link more


/usr/bin/ld: cannot find -lopencv_xphoto

that seems to say opencv, not opencl.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-25 04:23:33 -0600 )edit

Thanks for the update @gvdhoorn

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-06-25 04:30:58 -0600 )edit

Hi gvdhoorm & Janjit, Thanks for your info. The problem is solved. I did not pay attention that I leave a " } " in the target_link_libraries configuration.

sgttrieu gravatar image sgttrieu  ( 2021-06-25 04:32:43 -0600 )edit

@sgttrieu: please post your comment as an answer, and then accept your own answer.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-25 04:40:08 -0600 )edit

Question Tools

1 follower


Asked: 2021-06-24 16:00:27 -0600

Seen: 1,136 times

Last updated: Jun 25 '21