Ubuntu +OPenCV与C++的测试
opencv编译依赖文件安装
sudo apt install build-essential
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
—————————————————————————–
sudo apt update && sudo apt upgrade #更新软件源
sudo apt install -y g++ #安装G++
sudo apt install -y cmake #安装CMake
sudo apt install -y wget unzip
wget -O opencv4.6.0.zip https://github.com/opencv/opencv/archive/4.6.0.zip #下载OpenCV源码包
unzip opencv4.6.0.zip #解压
mkdir -p opencv-4.6.0-build && cd opencv-4.6.0-build #创建build文件(独立于源码文件)
cmake ../opencv4.6.0 #生成cmake编译文件
make -j4 #正是编译(-j4指定线程数)
sudo make install #编译后安装
测试opencv(C++)
可以通过C++使用opencv了。
mkdir ~/opencv-lena #创建工作目录
cd ~/opencv-lena #进入工作目录
vim DisplayImage.cpp
编辑如下代码:
#include <stdio.h> #include <opencv2/opencv.hpp> using namespace cv; int main(int argc, char** argv ) { if ( argc != 2 ) { printf("usage: DisplayImage.out <Image_Path>/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; }
创建CMake编译文件
vim CMakeLists.txt
写入如下内容
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
编译
cd ~/opencv-lena
cmake .
make
执行
此时opencv-lena文件夹中已经产生了可执行文件DisplayImage,下载lena.jpg放在opencv-lena下,运行
./DisplayImage lena.jpg
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/281868.html