C++ 结合 opencv读取图片与视频
文章目录
C++ 结合 opencv读取图片与视频 一、安装opencv 二 、配置文件准备 2.1 新建立文件夹 2.2 .vscode文件下配置文件 (1)配置tasks.json文件 (1)配置launch.json
三 、src文件下代码编写 3.1 图片的读取和显示(代码文件:1.img.cpp) 3.2 视频流的读取(代码文件:2.video.cpp) 3.3 视频流的读取并保存(代码文件:3.write.cpp)
四、cmake配置 五、运行
一、安装opencv
操作系统: Linux(采用远程服务器主机进行代码编写) 需提前配置(安装)cmake 远程连接服务器进行操作, 直接新建立的终端输入
sudo apt install libopencv- dev
二 、配置文件准备
2.1 新建立文件夹
建立一个新的文件夹,并在文件夹下面建立如下面的子文件夹 其中CMakeLists.txt是txt形式的文件 .vscode里面放的是配置文件 media放的图片和视频 output 是输出的文件夹路径 src 放的是源代码cpp文件
2.2 .vscode文件下配置文件
(1)配置tasks.json文件
{
"version" : "2.0.0" ,
"tasks" : [
{
"type" : "cppbuild" ,
"label" : "cmake配置" ,
"command" : "cmake" ,
"args" : [
"-S ." ,
"-B build" ,
"-DCMAKE_BUILD_TYPE=Debug"
] ,
"options" : {
"cwd" : "${workspaceFolder}"
} ,
"problemMatcher" : [
"$gcc"
] ,
"group" : "build" ,
} ,
{
"type" : "cppbuild" ,
"label" : "CMake编译" ,
"command" : "cmake" ,
"args" : [
"--build" ,
"build" ,
] ,
"options" : {
"cwd" : "${workspaceFolder}"
} ,
"problemMatcher" : [
"$gcc"
] ,
"group" : "build" ,
"dependsOn" : [
"CMake配置"
]
} ,
{
"type" : "shell" ,
"label" : "删除build目录" ,
"command" : "rm -rf build" ,
"options" : {
"cwd" : "${workspaceFolder}"
} ,
"problemMatcher" : [
"$gcc"
] ,
"group" : "build" ,
} ,
{
"label" : "运行可执行文件" ,
"type" : "shell" ,
"command" : "./build/demo_${fileBasenameNoExtension}" ,
"problemMatcher" : [ ] ,
"group" : {
"kind" : "build" ,
"isDefault" : true
} ,
"options" : {
"cwd" : "${workspaceFolder}"
} ,
"dependsOn" : [
"cmake构建"
]
}
]
}
(1)配置launch.json
{
"version" : "0.2.0" ,
"configurations" : [
{
"name" : "C++ Cmake Debug" ,
"type" : "cppdbg" ,
"request" : "launch" ,
"program" : "${workspaceFolder}/build/demo_${fileBasenameNoExtension}" ,
"args" : [ ] ,
"stopAtEntry" : false ,
"cwd" : "${workspaceFolder}" ,
"environment" : [ ] ,
"externalConsole" : false ,
"MIMode" : "gdb" ,
"miDebuggerPath" : "/usr/bin/gdb" ,
"setupCommands" : [
{
"description" : "Enable pretty-printing for gdb" ,
"text" : "-enable-pretty-printing" ,
"ignoreFailures" : true
}
] ,
"preLaunchTask" : "CMake编译"
}
]
}
三 、src文件下代码编写
3.1 图片的读取和显示(代码文件:1.img.cpp)
在导入 #include"opencv2/opencv.hpp" 会出现错误, 鼠标点击头文件并按住ctrl键,界面会出现配置,添加配置即可, 配置文件自动生成放在.vscode文件下。 由于是远程服务器主机, 所以无法显示图片,只能另存为。
# include "opencv2/opencv.hpp"
# include <iostream>
int main ( int argc, char * * argv)
{
cv :: Mat image = cv :: imread ( "./media/cat.jpg" ) ;
if ( image. empty ( ) )
{
std :: cout << "Could not read the image: " << std :: endl;
return 1 ;
}
std :: cout << "图片高度: " << image. rows << "图片宽度: " << image. cols << std :: endl;
std :: cout << "图片的 data: " << cv :: format ( image, cv :: Formatter :: FMT_NUMPY) << std :: endl;
std :: cout << "图片的 data: " << cv :: format ( image, cv :: Formatter :: FMT_PYTHON) << std :: endl;
cv :: Mat gray;
cv :: Mat hsv;
cv :: Mat rgb;
cv :: cvtColor ( image, gray, cv :: COLOR_BGR2GRAY) ;
cv :: cvtColor ( image, hsv, cv :: COLOR_BGR2HSV) ;
cv :: cvtColor ( image, rgb, cv :: COLOR_BGR2RGB) ;
cv :: imwrite ( "./output/gray.jpg" , gray) ;
return 0 ;
}
3.2 视频流的读取(代码文件:2.video.cpp)
# include "opencv2/opencv.hpp"
# include <iostream>
# include <gflags/gflags.h>
int main ( int argc, char * * argv)
{
gflags :: ParseCommandLineFlags ( & argc, & argv, true ) ;
cv :: VideoCapture capture ( "./media/dog.mp4" ) ;
if ( ! capture. isOpened ( ) )
{
std :: cout << "无法读取视频" << std :: endl;
return 1 ;
}
cv :: Mat frame;
cv :: Mat gray;
while ( true )
{
capture. read ( frame) ;
if ( frame. empty ( ) )
{
std :: cout << "文件读取完毕: " << std :: endl;
break ;
}
cv :: cvtColor ( frame, gray, cv :: COLOR_BGR2GRAY) ;
cv :: imshow ( "raw demo" , frame) ;
cv :: imshow ( "gray demo" , gray) ;
int k = cv :: waitKey ( 30 ) ;
if ( k == 27 )
{
std :: cout << "退出" << std :: endl;
break ;
}
}
return 0 ;
}
3.3 视频流的读取并保存(代码文件:3.write.cpp)
# include "opencv2/opencv.hpp"
# include <iostream>
# include <gflags/gflags.h>
int main ( int argc, char * * argv)
{
gflags :: ParseCommandLineFlags ( & argc, & argv, true ) ;
cv :: VideoCapture capture ( "./media/dog.mp4" ) ;
if ( ! capture. isOpened ( ) )
{
std :: cout << "无法读取视频" << std :: endl;
return 1 ;
}
int frame_width = capture. get ( cv :: CAP_PROP_FRAME_WIDTH) ;
int frame_height = capture. get ( cv :: CAP_PROP_FRAME_HEIGHT) ;
double fps = capture. get ( cv :: CAP_PROP_FPS) ;
std :: cout << "图像宽度: " << frame_width << std :: endl;
std :: cout << "图像高度: " << frame_height << std :: endl;
std :: cout << "图像帧率: " << fps << std :: endl;
cv :: Mat frame;
cv :: Mat gray;
cv:: VideoWriter writer ( "./output/record.avi" , cv:: VideoWriter :: fourcc ( 'M' , 'J' , 'P' , 'G' ) , fps, cv:: Size ( frame_width, frame_height) ) ;
while ( true )
{
capture. read ( frame) ;
if ( frame. empty ( ) )
{
std :: cout << "文件读取完毕: " << std :: endl;
break ;
}
cv :: cvtColor ( frame, gray, cv :: COLOR_BGR2GRAY) ;
writer. write ( frame) ;
}
return 0 ;
}
四、cmake配置
# 最低版本要求
cmake_minimum_required ( VERSION 3.10 )
# 项目信息
project ( opencv_demo)
# 使用find_package命令查找OpenCV库
find_package ( OpenCV REQUIRED)
find_package ( gflags REQUIRED)
if ( OpenCV_FOUND)
message ( STATUS "OpenCV library status:" )
message ( STATUS " version: ${OpenCV_VERSION}" )
message ( STATUS " libraries: ${OpenCV_LIBS}" )
message ( STATUS " include path: ${OpenCV_INCLUDE_DIRS}" )
else ( )
message ( FATAL_ERROR "Could not find OpenCV library" )
endif ( )
# 添加头文件
include_directories ( ${ OpenCV_INCLUDE_DIRS} ${ gflags_INCLUDE_DIRS} )
# 链接库
link_libraries ( ${ OpenCV_LIBS} ${ gflags_LIBRARIES} )
# 添加可执行文件
add_executable ( demo_1. img src/ 1. img. cpp)
add_executable ( demo_2. video src/ 2. video. cpp)
add_executable ( demo_3. write src/ 3. write. cpp)
五、运行
运行1.img.cpp,终端选项 – 运行任务 会出现下列界面, 依次点击 删除 build目录表,在选择终端 – 运行任务 – cmake 配置,结束后,在点击终端 – 运行任务 – CMake 编译
上述过程结束后,就会正常运行与输出1.img.cpp 输出后结果如图所示, 主要看 output 文件夹,可能和我下面不一样,因为我又在写其他的代码