opencv - 如何使用 catkin 将 OpenCV 3.4.5 dnn 模块与自定义 cv_bridge 链接?错误:未定义的引用 cv::dnn:experimental_dnn_v2::Net::Net()

标签 opencv compiler-errors compilation ros dotnetnuke

我正在尝试使用 publisher-subcriber no 通过从我的网络摄像头流式传输图像来运行我的对象识别程序。在我的程序中,我使用的是 opencv_dnn 库提供的 readNetFromTensorflow 函数。但是当我在我的工作区上进行 catkin_make 时,总是会弹出这个错误:

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

到目前为止,这是我的程序:
#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)
{

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



    cv::waitKey(10);
  }
  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;
  cv::namedWindow("view");
  cv::startWindowThread();
  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");
  net.setPreferableBackend(3);
  net.setPreferableTarget(1);
  //! [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

  //net.setInput(inputBlob);
  //Mat detection = net.forward();

  ros::spin();
  cv::destroyWindow("view");
}

如果我评论 cv::dnn::Net,我可以 100% 完成构建。

这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)

#Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)


project(image_transport_tutorial)

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)
add_dependencies(my_publisher ${catkin_EXPORTED_TARGETS} ${${PROJECT_NAME}_EXPORTED_TARGETS})
target_link_libraries(my_publisher ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})

# add the subscriber example
add_executable(my_subscriber src/my_subscriber.cpp)
add_dependencies(my_subscriber ${catkin_EXPORTED_TARGETS} ${${PROJECT_NAME}_EXPORTED_TARGETS})
target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})

# add the plugin example
add_library(resized_publisher src/manifest.cpp src/resized_publisher.cpp src/resized_subscriber.cpp)
add_dependencies(resized_publisher ${catkin_EXPORTED_TARGETS} ${${PROJECT_NAME}_EXPORTED_TARGETS})
target_link_libraries(resized_publisher ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})


# Mark executables and/or libraries for installation
install(TARGETS my_publisher my_subscriber resized_publisher
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(DIRECTORY launch/
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
)

install(DIRECTORY model/
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/model
)

install(FILES resized_plugins.xml
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

**

UPDATES:



**

如果我使用安装 ROS Kinetic 时构建的 opencv 3.3.1 catkin_make,我可以 100% 编译成功。但是当我做一些rosrun时,它会导致一些错误。这是错误:
Preprocessor/sub:Sub(Preprocessor/mul)(Preprocessor/sub/y) OpenCV Error: Unspecified error (Unknown layer type Sub in op Preprocessor/sub) in populateNet, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/tensorflow/tf_importer.cpp, line 1311

浏览了几天,发现一个人说是因为ROS自带的opencv的cv_bridge和image_transport。这里链接:https://answers.ros.org/question/318146/using-opencv-dnn-module-with-ros/

请帮助我,任何帮助将不胜感激。

最佳答案

我终于解决了这个问题,这里的解决方案:

$ cd ~/catkin_ws
$ git clone https://github.com/ros-perception/vision_opencv src/vision_opencv
$ git clone https://github.com/ros-perception/image_transport_plugins.git src/image_transport_plugins
$ catkin_make

曼塔普!

关于opencv - 如何使用 catkin 将 OpenCV 3.4.5 dnn 模块与自定义 cv_bridge 链接?错误:未定义的引用 cv::dnn:experimental_dnn_v2::Net::Net(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583115/

相关文章:

c++ - 静态变量未初始化

visual-studio-2015 - 为什么没有管理员权限就无法编译Visual Studio

python - OpenCV将多个帧转换成视频

Python OpenCv 如何检测视频何时播放完毕?

java - Java 编译器有哪些怪癖?

c# - ToDouble() 不工作?

编译具有有限库访问权限的程序

c++ - 使用 Qt 和 opencv 交叉编译到 Raspberry Pi

python - 使用 cv2.findContours() OpenCV 时出现 ValueError

c++ - 在循环外使用for循环变量是错误的吗?