C++版本安装openCV
1 安装opencv之前需要先安装依赖:
sudo apt update
sudo apt install build-essential cmake pkg-config libpng-dev libjpeg-dev libopenblas-dev libeigen3-dev libtbb-dev libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev libcanberra-gtk-module libcanberra-gtk3-module
2 Linux 系统中,C++环境下 OpenCV 的安装,可以使用如下的命令:
sudo apt install libopencv-dev
C++版本验证openCV
1 新建一个文件命名为test.cpp
,并在同级目录下放置一张图片test.jpg
,代码内容:
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
int main(int argc, char **argv)
{
cv::Mat img = cv::imread("../test.jpg");
if(img.empty())
{
std::cout<<"----------image read error!--------------"<<std::endl;
return 0;
}
cv::Scalar color;
color[0]=0;color[1]=0;color[2]=255;// 红色
cv::Rect rec1;
rec1=cv::Rect(10,10,500,500);
cv::rectangle(img, rec1,color,100,-1,0);
cv::imwrite("../result.jpg", img);
cv::waitKey(0);
// std::count<<img.empty()<<std::endl;
std::cout<<"done!"<<std::endl;
return 0;
}
2 编写CMakeLists.txt:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Define project name
project(opencv_example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Declare the executable target built from your sources
add_executable(opencv_example test.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})
3 在同级目录下新建build文件夹,并进入,编译,运行:
mkdir build
cd build
cmake ..
make
./opencv_example
结束。