目录
1、安装环境
1.1、安装C++开发环境,安装编译器GCC和G++
1.2、安装build-essential
1.3、安装cmake
1.4、安装依赖库
1.5、安装OpenCV、下载OpenCV连接OpenCV
1.6、配置环境变量
1.7、查看是否安装成功
1.8、创建Opencv.pc
1.9、查看opencv版本
2、OpenCV测试
2.1、测试用例
文章来源地址https://www.toymoban.com/news/detail-716468.html
1、安装环境
1.1、安装C++开发环境,安装编译器GCC和G++
sudo apt-get gcc
sudo apt-get g++
1.2、安装build-essential
sudo apt-get install build-essential
1.3、安装cmake
sudo apt-get install cmake
1.4、安装依赖库
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config
sudo apt-get install ffmpeg
sudo apt-get install libavcodec-dev
sudo apt-get install libavformat-dev
sudo apt-get install libswscale-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libpng-dev
1.5、安装OpenCV、下载OpenCV连接OpenCV
unzip opencv-4.5.5.zip
cd open-4.5.5
mkdir build
open build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
sudo cmake
sudo make -j2
sudo make install
1.6、配置OpenCV环境变量
vi /etc/ld.so.conf.d/opencv.conf 输入 /usr/local/lib
vi /etc/bash.bashrc 尾行输入 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
1.7、查看OpenCV是否安装成功
sudo ldconfig -v |grep opencv
1.8、创建Opencv.pc
cd /usr/local/lib
mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc
vi opencv.pc
输入
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.5.5
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
1.9、查看OpenCV版本
pkg-config opencv --modversion
2、OpenCV测试
2.1、测试用例
任意文件夹下新建工程,进入新建test.cpp ,CMakeList.txt,随意一张图片1.jpg
test.cpp
#include <iostream>
#include <opencv4/opencv2/core/core.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/opencv.hpp>
using namespace std;
int main(int argc, char *argv[])
{
if(argc !=2)
{
std::cerr<<"usage:./showpic FILE"<<std::endl;
return -1;
}
cv::Mat img = cv::imread(argv[1],1);
if(!img.data)
{
printf("No image data\n");
return -1;
}
cv::imshow("window",img);
cv::waitKey(0);
return 0;
}
CMakeList.txt
cmake_minimum_required(VERSION 2.8.0)
project(DisplayImage)
find_package(OpenCV REQUIRED)
add_executable(DisplayImage test.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
cmake-make编译 ,注意图片路径
mkdir bulid
cd build
cmake ..
make
./DisplayImage 1.jpg
成功则会显示图片
文章来源:https://www.toymoban.com/news/detail-716468.html
到了这里,关于第一章:Linux(Ubuntu)环境下OpenCV的安装配置和实例,亲测可用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!