因项目需求,需要将原本在windows下的工程移植到Linux平台下,需要在Linux下重新编译安装Opencv。
本文主要参考Opencv官方的安装教程https://docs.opencv.org/3.4.0/d7/d9f/tutorial_linux_install.html
1、安装依赖库
#通过以下命令安装依赖库
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
其中compiler为必要的编译器,required为必选依赖,optional为可选项。
2、下载源码
进入官网https://opencv.org/releases/下载Opencv3.4,选择sources并解压
3、编译安装opencv
进入解压后的文件夹,创建build文件夹
cd ~/opencv
mkdir build
cd build
编译安装
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j7 # runs 7 jobs in parallel
sudo make install
4、测试opencv是否安装成功
参考博客https://blog.csdn.net/qq_33431368/article/details/84754421
创建opencv-demo文件夹
mkdir opencv-demo
cd opencv-demo
创建DisplayImage.cpp和CMakeLists.txt
gedit DisplayImage.cpp
gedit CMakeLists.txt
DisplayImage.cpp代码如下
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
Mat image;
image = imread(argv[1], 1);
if (!image.data)
{
printf("No image data\n");
return -1;
}
namedWindow("display", WINDOW_AUTOSIZE);
imshow("display", image);
waitKey(0);
return 0;
}
CMakeList.txt代码如下:
cmake_minimum_required(VERSION 2.8)
project(DisplayImage)
find_package(OpenCV REQUIRED)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
编译执行程序
cmake .
make
./DisplayImage ouwen.jpeg
执行结果,成功显示图片