1.本例已经安装好OpenCV3.4.1,但是很多案例确还是用旧版本的cv模块,目前还无法解决import cv2.cv 模块不存在的问题,于是还得安装旧版本2.4.13.6;
OpenCV3.4.1安装参考:
树莓派 OpenCV3.4.1 安装血泪史,分享如何规避各种坑
https://blog.csdn.net/jacka654321/article/details/80728795
初次安装可能需要,本例在安装OpenCV3.4.1时已经安装过了,直接跳过,可以根据自己需要补充安装一些;
编译安装前的必要依赖库的安装:
[编译器]
sudo apt-get install build-essential
[必须安装]
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[选择安装]
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-devlibtiff-devlibjasper-dev libdc1394-22-dev
1、安装所需的安装工具和包:
# sudo apt-get install build-essential cmake pkg-config
build-essential 和 pkg-config可能都已经安装。以防万一,我们还是将它们键入apt-get命令中。
2、安装必要的图像I/O包,这样才可以读入JPEG,PNG,TIFF等这些格式的图像。
# sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
3、安装GTK开发库,这个库用来构建GUI。同时OpenCV中的highgui库也需要它来在屏幕上显示图像。
# sudo apt-get install libgtk2.0-dev
4、安装必要的视频I/O包,OpenCV需要它们来读入视频文件。
# sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
5、安装OpenCV优化操作时所需库。
# sudo apt-get install libatlas-base-dev gfortran
2、VNC登录树莓派,打开LX终端,下载、解压并进入 文件夹:(要直接在树莓派上操作,不要用ssh)
#wget -O opencv-2.4.10.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.10/opencv-2.4.10.zip/download
#unzip opencv-2.4.10.zip
#cd opencv-2.4.10
#或浏览器打开http://opencv.org/downloads.html 选择opencv-2.4.13.6版本直接下载,任意一个wget都能下载
#wget -O opencv-2.4.13.zip https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.13/opencv-2.4.13.zip/download
wget -O opencv-2.4.13.6.zip https://github.com/opencv/opencv/archive/2.4.13.6.zip
wget -O opencv-2.4.13.6.zip https://codeload.github.com/opencv/opencv/zip/2.4.13.6
unzip opencv-2.4.13.6.zip
cd opencv-2.4.13.6
3、准备编译
mkdir build
cd build
4、更改安装目录(先在/usr/local下新建文件夹opencv2)
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv2 .. -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_FFMPEG=OFF
注意:要在cmake最后加上 -DENABLE_PRECOMPILED_HEADERS=OFF,之前没加出错了:
fatal error: stdlib.h: 没有那个文件或目录 #include_next <stdlib.h>
补充在后面加上
参考链接:
https://blog.csdn.net/m0_38024766/article/details/78715016
注意需要加上 -DWITH_FFMPEG=OFF ,不然编译过程中会出现错误,原因是ffmpeg的更新问题。
参考链接:
http://tieba.baidu.com/p/5087553814
若opencv_perf_gpu 出错:
加上 -D CUDA_GENERATION=Kepler
5、编译(本例树莓派3B+,选择 4核编译 -j4,或者直接 make 也行)
注意散热,风扇强制散热,温度三四十度,安装过程中,CPU飙升100%,温度上升至54度,如果不散热树莓派一般70度左右,安装时温度再升高,很容易gogo;
make -j4
sudo make install
make -j4 花费1.5小时
sudo make install 很快就完成
6、安装完成后,安装目录有4个文件夹
7.目前默认是opencv3.4版本,想切换到2.4版本,可以通过如下的方式,
通过修改bashrc文件来设置PKG_ CONFIG_PATH 和 LD_LIBRARY_PATH的路径来选择对应的opencv版本
sudo vim ~/.bashrc
vim没有修改默认配置不能复制粘贴,可以换其他文本工具修改如:
sudo gedit ~/.bashrc
在文本末尾加上新编译的2.4版本的路径,也就是cmake时:CMAKE_INSTALL_PREFIX=/usr/local/opencv2
export PKG_CONFIG_PATH=/usr/local/opencv2/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opencv2/lib
然后执行命令,后面用pkg-config --modversion opencv 查看opencv版本的时候就是2.4版本
source ~/.bashrc
3.上面的这些过程并不能保证在写makefile中调用的是2.4版本的库,可能还是调用之前的3.4的库,具体还是要在CMakeList.txt中指定要找的opencv的目录,
打开python测试:
import cv2
cv2.__version__
opencv2.4的编译目录/home/pi/opencv-2.4.13.6/build 下找到OpenCVConfig.cmake 文件,并在CMakeList.txt中添加对应的路径信息。这样cmake的时候调用的就是2.4的库;
想切换成原来的3.4版本只需将第二步的bashrc文件和CMakeList.txt文件中修改的内容改成对应的opencv3.4的位置就好了。
后面这些,本人也看不懂,贴出来给会的人参考吧!!
在使用的时候,在CMakelists.txt中,如要用2,在find_package前指明路径:
set(OpenCV_DIR /usr/local/opencv2/share/OpenCV)
find_package(OpenCV 2 REQUIRED)
用3的时候:
find_package(OpenCV 3 REQUIRED)
参考例子:
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
#set (OpenCV_DIR "/home/pi/opencv-2.4.13.6/build")
set(OpenCV_DIR /usr/local/opencv2/share/OpenCV)
# Define project name
project(myTest)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV 2 REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(demo demo.cpp)
# Link your application with OpenCV libraries
target_link_libraries(demo ${OpenCV_LIBS})
参考链接:
如何让opencv2.4和opencv3共存
https://zhidao.baidu.com/question/1835658466861558100.html
opencv3.2 和opencv2.4同时安装(亲测有效)
https://www.jianshu.com/p/782a291ac151
Ubuntu下安装多个版本的opencv,以及切换使用
https://blog.csdn.net/qq_29828623/article/details/73556713
ubuntu下opencv2.4.10 和 opencv 3.10 多版本切换问题
https://blog.csdn.net/kekong0713/article/details/53688131
用Python和OpenCV创建一个图片搜索引擎的完整指南
http://python.jobbole.com/80860/
vacation-image-search-engine