1 linux上编译使用OpenCV的程序出现undefined reference to cv::dnn::experimental_dnn错误

在程序中使用了

cv::Mat blob = cv::dnn::blobFromImage(input_image, 1.0 / 255.0, input_image.size(), cv::Scalar(0, 0, 0), true, false, CV_32F);

将opencv默认的BGR,HWC图片数据转换为深度学习模型所需要的RGB,CHW数据,但是在编译的时候出现了以下错误,

undefined reference to `cv::dnn::experimental_dnn_34_v17::blobFromImage(cv::_InputArray const&, double, cv::Size_<int> const&, cv::Scalar_<double> const&, bool, bool, int)'

出现问题时CMakeLists.txt的动态链接语句是这样写的,

target_link_libraries(main stdc++ opencv_core opencv_imgproc opencv_imgcodecs dl rt)

发现上面没有链接libopencv_dnn.so,所以加上libopencv_dnn.so,则CMakeLists.txt的动态链接语句修改为,

target_link_libraries(main stdc++ opencv_core opencv_imgproc opencv_imgcodecs opencv_dnn dl rt)

重新编译即可解决。