Linux环境下使用opencv的dnn模块调用yolov4遇到的坑(纯CPU)

1. 无法安装opencv依赖包libjasper-dev

无法定位软件包libjasper-dev

2. 编译opencv时报错缺少boostdesc_bgm.i文件

把这个里面的所有文件添加到opencv_contrib/modules/xfeatures2d/src/中即可

另外,如果报错没有features2d/XXX.hpp,可以直接将/opencv/modules/features这个文件夹拷贝到opencv_contrib/modules/xfeatures2d/test文件夹下,但是可能会有一些问题,见3

3. 编译opencv时报错没有common.h

features2d_converters.cpp:2:10: fatal error: common.h: 没有那个文件或目录

4. 测试dnn模块时报错undefined reference to `cv::dnn::dnn4_v20191202::Net::~Net()‘

https://blog.csdn.net/weixin_50497501/article/details/131622923

5. 调用yolov4-tiny返回结果没有检测框

可能是makefile中指定的opencv库文件和头文件版本不一致,具体看这篇文章的第三条

总结以及备忘录

1. 关于opencv的编译选项

cmake -DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr/local/opencv455 \
		-DOPENCV_GENERATE_PKGCONFIG=ON \
		-DOPENCV_EXTRA_MODULES_PATH=/home/opencv-install/opencv4.5.5/opencv_contrib-4.5.5/modules ..
  1. OpenCV4以上版本默认不使用pkg-config,但是可以通过相应的编译选项开启该功能:
    -DOPENCV_GENERATE_PKGCONFIG=ON
  2. 可能是概率性事件,-DOPENCV_EXTRA_MODULES_PATH写完整路径的话,boostdesc_bgm.i这些文件在cmake时下载成功了
  3. 最后的. .两个点别忘了

2. 关于管理多版本opencv

前提:编译opencv时添加-DOPENCV_GENERATE_PKGCONFIG=ON选项,或者自行配置opencv.pc(要创建文件夹/opencv_install_path/lib/pkgconfig,并将opencv.pc放在该文件夹下)

opencv.pc示例

# Package Information for pkg-config

prefix=/usr/local/opencv455(改)
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.5(改)
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}

之后,步骤如下:

# 0. 可以先对opencv.pc文件改名,之后使用pkg-config查找opencv时,用的就是pc文件名(可以尾缀版本号)
sudo mv -i /opencv_install_path/lib/pkgconfig/opencv.pc /opencv_install_path/lib/pkgconfig/opencv455.pc

# 1. 将opencv的pc配置文件添加到系统全局配置文件中
sudo vi /etc/bash.bashrc

# 2. 向文件中添加如下内容
export PKG_CONFIG_PATH_OPENCV455=$PKG_CONFIG_PATH_OPENCV455:/opencv_install_path/lib/pkgconfig
# 我个人而言,添加内容如下
export PKG_CONFIG_PATH_OPENCV455=$PKG_CONFIG_PATH_OPENCV455:/usr/local/opencv455/lib/pkgconfig
export PKG_CONFIG_PATH_OPENCV440=$PKG_CONFIG_PATH_OPENCV440:/usr/local/opencv440/lib/pkgconfig

# 3. 重新加载bash配置 或者重启shell
source /etc/bash.bashrc

# 4. 使用pkg-config查找opencv
pkg-config --modversion opencv455
> 4.5.5
pkg-config --modversion opencv440
> 4.4.0

3. opencv Makefile的通用模板

CXX = aarch64-linux-gnu-g++

INCLUDE = $(shell pkg-config --cflags opencv455)  # 获得opencv相关头文件路径
LIBS = $(shell pkg-config --libs opencv455) # 获得opencv扩展库。

SOURCES = test.cpp 
TARGET = test

$(TARGET):$(SOURCES)
	@$(CXX) -std=c++11 -O1 -o $(TARGET) $(SOURCES) $(INCLUDE) $(LIBS)

clean:
	rm -rf $(TARGET) *.o

$(shell pkg-config --cflags opencv455)中的opencvx,和opencv安装路径下的lib/pkgconfig/opencvx.pc文件名是对应的

4. 调用yolo的代码

可以看这篇博客:4种YOLO目标检测的C++和Python两种版本实现

你可能感兴趣的:(各路问题汇总,opencv,linux,opencv,YOLO,c++)