Ask Your Question

How to linked OpenCV 3.4.5 dnn module with custom cv_bridge using catkin? Error: undefined reference cv::dnn:experimental_dnn_v2::Net::Net()

asked 2019-04-09 03:02:16 -0500

Budhie gravatar image

I'm trying to use publisher-subcriber no to run my object recognition program by streaming images from my webcam. In my program, I am using readNetFromTensorflow function that provided by opencv_dnn library. But while I catkin_make on my workspace, there is always popup this error:

CMakeFiles/my_subscriber.dir/src/my_subscriber.cpp.o: In function `main':
my_subscriber.cpp:(.text+0x2ee): undefined reference to `cv::dnn::experimental_dnn_v2::readNetFromTensorflow(cv::String const&, cv::String const&)'
my_subscriber.cpp:(.text+0x30e): undefined reference to `cv::dnn::experimental_dnn_v2::Net::setPreferableBackend(int)'
my_subscriber.cpp:(.text+0x31a): undefined reference to `cv::dnn::experimental_dnn_v2::Net::setPreferableTarget(int)'
my_subscriber.cpp:(.text+0x4c4): undefined reference to `cv::dnn::experimental_dnn_v2::Net::~Net()'
my_subscriber.cpp:(.text+0x5f0): undefined reference to `cv::dnn::experimental_dnn_v2::Net::~Net()'
collect2: error: ld returned 1 exit status
image_transport_tutorial/CMakeFiles/my_subscriber.dir/build.make:176: recipe for target '/home/odroid/image_transport_ws/devel/lib/image_transport_tutorial/my_subscriber' failed
make[2]: *** [/home/odroid/image_transport_ws/devel/lib/image_transport_tutorial/my_subscriber] Error 1
CMakeFiles/Makefile2:1125: recipe for target 'image_transport_tutorial/CMakeFiles/my_subscriber.dir/all' failed
make[1]: *** [image_transport_tutorial/CMakeFiles/my_subscriber.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2

And here is my program so far:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>

#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/calib3d.hpp>

using namespace std;
using namespace cv;

const size_t inWidth = 320;
const size_t inHeight = 320;
const char* classNames[] = {"background",
                            "A", "B", "C"};
cv_bridge::CvImagePtr cv_ptr;
int h,w;

void imageCallback(const sensor_msgs::ImageConstPtr& msg)

    cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    w = cv_ptr->image.cols;
    h = cv_ptr->image.rows;

  catch (cv_bridge::Exception& e)
    ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());

int main(int argc, char **argv)
  ros::init(argc, argv, "image_listener");
  ros::NodeHandle nh;
  image_transport::ImageTransport it(nh);
  //! [Initialize network]
  cv::dnn::Net net = dnn::readNetFromTensorflow("/home/odroid/Desktop/Archive/frozen_inference_graph.pb","/home/odroid/Desktop/Archive/graph.pbtxt");
  //! [Initialize network]

  image_transport::Subscriber sub = it.subscribe("camera/image", 1, &imageCallback);
  //cv::Mat inputBlob = cv::dnn::blobFromImage(cv_ptr->image,1.0, Size(inWidth,inHeight),Scalar(127.5,127.5,127.5), true,false); //Convert Mat to batch of images

  //Mat detection = net.forward();


If I comment cv::dnn::Net, I can build 100% complete.

And here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)

#Enable C++11


find_package(catkin REQUIRED COMPONENTS cv_bridge image_transport message_generation sensor_msgs)

# add the resized image message
add_message_files(DIRECTORY msg
   FILES ResizedImage.msg
generate_messages(DEPENDENCIES sensor_msgs)

catkin_package(CATKIN_DEPENDS cv_bridge image_transport message_runtime sensor_msgs)

find_package(OpenCV REQUIRED)
#Set OpenCV
set(OpenCV_INCLUDE_DIRS /usr/local/include/ /usr/local/include/opencv2/)

#Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "config: ${OpenCV_DIR}")
message(STATUS "version: ${OpenCV_VERSION}")
message(STATUS "libraries: ${OpenCV_LIBRARIES}")
message(STATUS "include path: ${OpenCV_INCLUDE_DIRS}")

include_directories(include ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})

# add the publisher example
add_executable(my_publisher src/my_publisher.cpp ...
edit retag flag offensive close merge delete



I have post this issue on opencv github and I got reply from alalek saying that "there is mess with header with your build system" and suggest me to post it here.

Budhie gravatar imageBudhie ( 2019-04-09 09:47:45 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-10 09:22:30 -0500

Eduardo gravatar image

updated 2019-04-11 03:35:55 -0500

If you want to use an OpenCV version different than the one shipped with ROS:

  • do it at your own risks and understand the consequences of the following instructions
  • your ROS program cannot use OpenCV 4 (any different OpenCV version than the one in ROS) and in the same time use ROS image_transport that uses OpenCV 3.3.1, so you have to uninstall anything that is related to OpenCV 3.3.1 in ROS and build everything with the same OpenCV version
  • build your OpenCV version and use as CMAKE_INSTALL_PREFIX a local directory, e.g.: ~/src/opencv4/build/install
  • uninstall ROS-Kinetic-Opencv (of course assuming ROS Kinetic here): sudo apt-get remove ros-kinetic-opencv3
  • write the names of ROS packages that depend on ros-kinetic-opencv3 and that will be uninstalled
  • git clone the missing ROS packages in catkin_ws/src:

Something like:

git clone
git clone
git clone
git clone
  • build with your OpenCV version:

Go to your catkin_ws:

catkin_make VERBOSE=1 -j4 -DCMAKE_BUILD_TYPE=Release --cmake-args -DOpenCV_DIR=~/src/opencv4/build/install
edit flag offensive delete link more

Question Tools



Asked: 2019-04-09 03:02:16 -0500

Seen: 1,251 times

Last updated: Apr 11 '19