系统相关
首页 > 系统相关> > OpenCV基础应用1.Ubuntu18安装OpenCV4.0

OpenCV基础应用1.Ubuntu18安装OpenCV4.0

作者:互联网

一. 安装OpenCV4.0

1. 下载OpenCV4.0源文件,官网地址下载。

2. 将下载的opencv-4.0.0.zip文件解压。

$ unzip opencv-4.0.0.zip -d .

3. 安装相关工具和依赖库。

$ sudo apt-get install cmake build-essential libgtk2.0-dev libavcodec
    libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev:i386 
    libtiff5-dev

4. 进入opencv-4.0.0目录,创建build文件夹。

$ cd opencv-4.0.0/
$ mkdir build
$ cd build

5. cmake创建编译配置文件,CMAKE_INSTALL_PREFIX选项指定了opencv安装路径。

$ cmake -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..

在此过程中会联网下载ippicv文件,可能等待较长时间。

6. 编译安装

$ sudo make -j4
$ sudo make install

7. 配置OpenCV环境变量

$ sudo gedit /etc/ld.so.conf.d/opencv.conf

编辑文件,添加以下内容,如果没有安装过opencv则该文件为空。

/usr/local/opencv4/lib

执行以下命令使配置生效

$ sudo ldconfig

二. 安装contrib库

1. 下载4.0版本的contrib库

2. 解压到OpenCV同级目录下。

3. 进入OpenCV的build目录,重新配置CMAKE,编译并安装

$ cd opencv-4.0.0/build
$ cmake -D CMAKE_BUILD_TYPE=Release 
    -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.0/modules 
    -D OPENCV_ENABLE_NONFREE=ON 
    -D BUILD_opencv_legacy=ON ..
$ make -j4
$ sudo make instal
$ sudo ldconfig

三. Eclipse中配置OpenCV编译环境

1. 在新建的工程中右键选择工程属性(Properties),选择C/C++ Build->Settings,在GCC C++ Compiler的Dialect中选择Language standard=ISO C++11(-std=c++0x)。

2. 在GCC C++ Compiler中的Includes中输入OpenCV的头文件路径。

3. 在GCC C++ Linker->Libraries中输入Opencv的库文件名和搜索路径。

进入opencv4的库目录,使用如下命令可以提取opencv的依赖库名称:

$ cd /usr/local/opencv4/lib
$ ls lib*.so | awk -v head="lib" -v tail=".so" '{print substr($0, index($0,head)+length(head),index($0,tail)-index($0,head)-length(head))}'

库名:

opencv_aruco
opencv_bgsegm
opencv_bioinspired
opencv_calib3d
opencv_ccalib
opencv_core
opencv_datasets
opencv_dnn_objdetect
opencv_dnn
opencv_dpm
opencv_face
opencv_features2d
opencv_flann
opencv_freetype
opencv_fuzzy
opencv_gapi
opencv_hfs
opencv_highgui
opencv_imgcodecs
opencv_img_hash
opencv_imgproc
opencv_line_descriptor
opencv_ml
opencv_objdetect
opencv_optflow
opencv_phase_unwrapping
opencv_photo
opencv_plot
opencv_reg
opencv_rgbd
opencv_saliency
opencv_shape
opencv_stereo
opencv_stitching
opencv_structured_light
opencv_superres
opencv_surface_matching
opencv_text
opencv_tracking
opencv_videoio
opencv_video
opencv_videostab
opencv_xfeatures2d
opencv_ximgproc
opencv_xobjdetect
opencv_xphoto

4. 编写测试程序测试OpenCV是否配置正确。

测试程序如下:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
	Mat img = imread("100RMB.png");
	if(img.data == NULL)
	{
		cout<<"Read image failed"<<endl;
		return 0;
	}
	imshow("Image", img);
	waitKey(0);
	return 0;
}

100RMB.png文件在工程的根目录下,在运行过程中,imread输入路径与Eclipse配置的工作空间设置有关,如果在终端中启动程序,则路径应该为:

imread("../../100RMB.png");

Eclipse的Run as->Run Configurations->C/C++ Application->OpencvTest, 在Arguments中设置了working directory,因此图片的父路径为OpencvTest/。

 


 

标签:CMAKE,4.0,..,opencv,sudo,OpenCV4.0,OpenCV,Ubuntu18
来源: https://blog.csdn.net/u010580016/article/details/118052567