小白-《学习OpenCV3》-安装OpenCV
1、安装环境
- 树莓派4B
- Raspberry Pi OS with desktop
2、安装开发包
sudo apt-get install libgtk-dev -y
sudo apt-get install build-essential git cmake pkg-config -y
sudo apt-get install libpng-dev -y
sudo apt-get install libjpeg-dev -y
sudo apt-get install libtiff-dev -y
sudo apt-get install libjasper-dev -y
sudo pip3 install numpy
sudo apt-get install libavcodec-dev libavformat-dev -y
3、下载OpenCV源码,编译,安装
链接:https://opencv.org/releases/ 建一个文件夹暂存文件:~/Opencv 在OpenCV-4.5.3中新建一个release文件夹 cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local … make -j4 (老版本的OpenCV源码编译过程中有可能会有一些问题,尽量下载新版进行编译,这个版本暂未在编译时遇到问题) sudo make install
如果需要下载和编译Contributed模块:放在~/OpenCV路径下,与OpenCV-4.5.3平行放置 git clone https://github.com/opencv/opencv_contrib.git 在opencv-4.5.3/release文件夹下再次配置OpenCV的CMake: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_EXTRA_MODULES_PATH=…/…/opencv_contrib/modules … make -j4 sudo make install
4、安装VScode
这个不是必须的,只是习惯了用VScode。 链接:https://code.visualstudio.com/#alt-downloads 下载.deb(ARM) sudo dpkg -i code_1.53.2-1613044047_armhf.deb
5、第一个程序
example.cpp:
#include "opencv2/opencv.hpp"
int main(int argc, char **argv)
{
cv::Mat img = cv::imread(argv[1],-1);
if(img.empty()) return -1;
cv::namedWindow("example",cv::WINDOW_AUTOSIZE);
cv::imshow("example",img);
cv::waitKey(0);
cv::destroyWindow("example");
return 0;
}
6、例程编译报错
如果用gcc编译不指定头文件和链接库会报错:
gcc example.cpp
example.cpp:1:10: fatal error: opencv2/opencv.hpp: No such file or directory
#include "opencv2/opencv.hpp"
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
创建makefile:
CXX ?= g++
CXXFLAGS += -c -Wall $(shell pkg-config --cflags opencv)
LDFLAGS += $(shell pkg-config --libs --static opencv)
all: opencv_example
opencv_example: example.o; $(CXX) $< -o $@ $(LDFLAGS)
%.o: %.cpp; $(CXX) $< -o $@ $(CXXFLAGS)
clean: ; rm -f example.o opencv_example
make还是报错,但是多了一些东西:
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
g++ example.cpp -o example.o -c -Wall
example.cpp:1:10: fatal error: opencv2/opencv.hpp: No such file or directory
#include "opencv2/opencv.hpp"
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [makefile:10: example.o] Error 1
解决方案: 1、在/usr/local/lib中新建一个文件夹pkgconfig,在里面新建一个文件opencv.pc:
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.5.3
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
~
2、添加环境变量: sudo vim /etc/bash.bashrc:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
sudo vim /etc/profile:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
7、编译运行
make ./opencv_example 1.jpg
|