热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CentOS里安装OpenCV

CentOS里安装OpenCV,Go语言社区,Golang程序员人脉社



文章目录


    • 安装所需的程序包
    • 下载最新的OpenCV
    • 编译安装OpenCV
    • 测试C程序


参考:
https://www.learnopencv.com/install-opencv-4-on-centos-7/


安装所需的程序包

sudo yum -y install epel-release # 安装epel扩展源
sudo yum -y install git gcc gcc-c++ cmake3
sudo yum -y install qt5-qtbase-devel
sudo yum install -y python34 python34-devel python34-pip
sudo yum install -y python python-devel python-pip
sudo yum -y install python-devel numpy python34-numpy
sudo yum -y install gtk2-devel
sudo yum install -y libpng-devel
sudo yum install -y jasper-devel
sudo yum install -y openexr-devel
sudo yum install -y libwebp-devel
sudo yum -y install libjpeg-turbo-devel
sudo yum install -y freeglut-devel mesa-libGL mesa-libGL-devel
sudo yum -y install libtiff-devel
sudo yum -y install libdc1394-devel
sudo yum -y install tbb-devel eigen3-devel
sudo yum -y install boost boost-thread boost-devel
sudo yum -y install libv4l-devel
sudo yum -y install gstreamer-plugins-base-devel
#=========== 安装 ffmpeg ===========
yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
(可选) rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
(可选) rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum -y install ffmpeg ffmpeg-devel # 安装ffmpeg依赖

下载最新的OpenCV

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout master # 选择master版本分支
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout master # 选择master版本分支
cd ..

编译安装OpenCV

cd opencv
mkdir build
cd build
cmake3 -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
# cmake3 也可以加上参数 -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules
# cmake3 执行的时候如果有下错误,可以重复执行直接没有错误出现。
make -j$(nproc) #有几个CPU就用几个CPU
make install

在这里插入图片描述
安装完成。可以输入命令opencv_version检查已安装的opencv的版本号。


测试C程序

mkdir tt && cd tt
vi DisplayImage.cpp

#include
#include
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}

vi CMakeLists.txt

set(CMAKE_CXX_STANDARD 14) #设置c++标准为14
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

cmake 并测试如下:

[root@localhost tt]# cmake3 .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "4.1.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /root/tt
[root@localhost tt]# make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage
[root@localhost tt]# ./DisplayImage lena.jpg
(Display Image:7859): Gtk-WARNING **: 09:04:15.519: cannot open display:



推荐阅读
author-avatar
老猫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有