C++版本安装openCV(一步到位,服务器版)

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 

结束。

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值