Ubuntu下安装opencv真心不容易,原因在于很多人在ubuntu下一般都安装了anaconda!下面是总结的安装opencv并运行一个小程序的示例过程!如有错误,请读者指出!
网址:https://github.com/opencv
可以进入github下载,也可使用如下命令下载:
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git
下载后,将opencv_contrib文件夹移动到opencv中去。
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-dev libtiff-dev libjasper-dev libdc1394-22-dev
里面有些东西诸如cmake、git、python-dec、python-numpy一般早都安装好了,确定的话可以直接去掉。
1.opencv目录下,创建build文件夹
mkdir build
2.进入build目录,执行cmake命令:
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D ENABLE_CXX11=1 -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D OPENCV_ENABLE_NONFREE=True ..
注意:上面的cmake命令必须按照我的执行。
3.使用make开始编译
make -j8
-j8是同时使用8核CPU来执行编译过程,这样速度比较快,具体需要根据自己的电脑情况修改。
4.编译install
sudo make install
然后在/usr/local/lib下可以看到编译的结果。
5.卸载opencv
sudo make uninstall
6.手动卸载opencv
sudo rm -r /usr/local/include/opencv4
sudo rm -r /usr/local/include/opencv
sudo rm -r /usr/include/opencv
sudo rm -r /usr/include/opencv4
sudo rm -r /usr/local/share/opencv
sudo rm -r /usr/local/share/OpenCV
sudo rm -r /usr/share/opencv
sudo rm -r /usr/share/OpenCV
sudo rm -r /usr/local/bin/opencv*
sudo rm -r /usr/local/lib/libopencv*
sudo rm -r /usr/local/lib/pkgconfig/opencv4.pc
sudo rm -r /usr/local/lib/cmake/opencv4
1.配置pkg-config路經
经过上面的步骤后,在/usr/local/lib/pkgconfig下生成了opencv4.pc文件,这个文件很重要,里面记录了OpenCV头文件、库文件的路經。需要进行如下配置:
#进入 bash.bashrc
sudo gedit /etc/bash.bashrc
#在文件最后添加如下内容
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
#更新
sudo updatedb
2.配置库路經
#打开下列文件
sudo gedit /etc/ld.so.conf.d/opencv.conf
# 添加lib路經
/usr/local/lib
# 更新
sudo ldconfig
NOTE:一切有关” cannot open shared object file: No such file or directory “的问题基本都可以通过上面的方式来解决(除非是操作系统里确实没有包含该共享库(lib*.so.*文件)或者共享库版本不对)
查看opencv版本号:
pkg-config --modversion opencv4
1.编写一个test.cpp文件,内容如下:
#include
#include
using namespace std;
using namespace cv;
int main(void)
{
Mat img = imread("/home/dulingwen/Pictures/ai.jpg",0);
imwrite("/home/dulingwen/Pictures/aj.jpg",img);
return 0;
}
2.构建CMakeLists.txt文件
这个步骤,最简单的方式是去opencv的文件夹里直接复制一份过来,位置:/opencv/samples/cpp/example_cmake下,在里面修改部分内容如下所示:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Define project name
project(opencv_test_project)
# 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 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 " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Declare the executable target built from your sources
add_executable(opencv_test_project test.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_test_project LINK_PRIVATE ${OpenCV_LIBS})
cmake的知识可以参考:cmake的一些基础知识
3.编译并运行
进入test.cpp所在目录,输入如下命令
cmake ./
make
也可以创建一个build文件夹,在这个文件夹里执行编译过程,这样生成的一些中间文件会都保存在build文件夹中而不会与源文件混在一起,此时cmake命令如下:
cmake .. //两个点表示从上一级文件夹中寻找CMakeLists.txt并执行
make
最后会生成一个名为opencv_test_project的可执行程序,输入如下命令即可执行它:
./opencv_test_project
根据程序,目录/home/dulingwen/Pictures下会生成一个单通道的图像,名为aj.jpg,如果你能看到说明程序运行成功了!
4.使用命令行编译:
这个我还不会,后面学会了再来完善!
1.使用如下命令
pip install opencv-python
但是opencv-pyhon中某些不免费的功能没有实现,如sift、surf特征点算法,需要卸载opencv-python,然后安装opencv-contrib-python。
2.安装opencv-contrib-python
pip install opencv-contrib-python
将自动安装最新版本。
3.如何使用不免费的模块?
以上安装的是最新版本,non-free算法依然不能用,因此改为安装opencv-contrib-python3.4.2.17版本:
pip install opencv-contrib-python==3.4.2.17
如果在商业项目中使用surf算法,这个是要交费的,因为算法发明者持有该算法的专利。但是平常的学习中还是会用到的,有知道怎么在最新版opencv中用这些non-free算法的请告知,谢谢了!
问题1.如果遇到如下类似的问题,这个可能是权限导致的。
../../lib/libopencv_imgcodecs.so.4.1.1: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.1.1: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.1.1: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0'
../../lib/libopencv_imgcodecs.so.4.1.1: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
......
建议修改命令为:
sudo make -j8
但是也有可能是libtiff的库与opencv所要调用的版本不同,建议重新安装:
sudo apt-get uninstall libtiff5
sudo apt-get install libtiff5
问题2.IPPICV文件下载失败。
IPPICV: Download: ippicv_2019_lnx_intel64_general_20180723.tgz
CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
IPPICV: Download failed: 28;"Timeout was reached"
这个由于有墙,只好手动下载了,路經隐含在3rdparty/ippicv/ippicv.cmake中:
在第5行有:set(IPPICV_COMMIT "32e315a5b106a7b89dbed51c28f8120a48b368b4")
第47行有: "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
两者个结合加上文件名就是文件的下载路經了:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz
下载好后在该文件中替换掉原来的路經即可
"/data/下载/opencv"
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
问题3.face_landmark_model.dat下载失败
解决方法同上,这个文件的路經隐含在/opencv_contrib/modules/face/CMakeLists.txt中
下载路經为:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
然后在该文件中替换掉原来的路經即可:
"/data/下载/opencv/"
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"
当然随着opencv版本更新,路經有可能会发生变化,但目前就是这个路經了