系统 | Ubuntu 18.04 |
---|---|
OpenCV 版本 | 4.5.1 |
接口类型 | C++ |
安装OpenCV | 点我安装 |
0. Ubuntu OpenCV C++ 编译运行示例
-
C++编译流程
-
新建文件夹并进入
mkdir ShowImage && ShowImage
- 打开终端,新建
cpp
文件
touch read.cpp
- 编辑
read.cpp
gedit read.cpp
拷入下列代码并保存
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc,char **argv)
{
Mat img = imread("girl.jpg");
imshow("meinv",img);
waitKey(0);
}
- 拷贝
CMakeLists.txt
将../opencv-4.5.1/samples/cpp/example_cmake
中的CMakeLists.txt
拷贝到(你新建的文件夹) - 修改
CMakeLists.txt
修改前
# Define project name
project(opencv_example_project)
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})
修改后(其他不要动,仔细对比)
# Define project name
project(text)
# Declare the executable target built from your sources
add_executable(text read.cpp)
# Link your application with OpenCV libraries
target_link_libraries(text ${OpenCV_LIBS})
-
拷贝图像
找一张jpg
格式的图像重名为girl
拷贝到ShowImage
文件夹 -
编译运行
cmake .
make
./text
- 报错
注意第3
步和第6
步图像名称 - 参考
1. 显示图片
cv::imread
加载图像文件成为一个Mat对象
参数:
- 图像文件名
- 数值
<0
(IMREAD_UNCHANGED原图
)
0
(IMREAD_GRAYSCLAE灰度图
)
>0
(IMREAD_COLORRGB图
)
//头文件
#include <opencv2/opencv.hpp>
//主函数
int main(int argc, char** argv){
//读取图片(支持多种格式),返回一个cv::Mat结构(用来处理所有类型的图像)
cv::Mat img = cv::imread(argv[1],-1);
//如果图片为空(没有载入图片),返回-1
if(img.empty()) return -1;
//设置窗口
cv::namewindow("Examples1", cv::WINDOW_AUTOSIZE);
//显示
cv::imshow("Example1", img);
//<=0,无限等待直到有键按下,>0,等待,继续运行程序(毫秒ms)
cv::waitKey(0);
//关闭窗口,释放相关联的内存空间
cv::destroyWindow("Example1");
return 0;
}
OpenCV的函数都在cv空间下,调用OpenCV的函数需要在函数前面加上cv::,这样相当繁琐,我们将使用using namespace cv;
指令告诉编译器所以函数都在cv空间中。但是这和其他命名空间有冲突的风险,如果函数foo()存在于cv和std的命名空间,必须指定是位于cv::foo()的函数,还是std::foo()的函数。
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc,char** argv){
Mat img = imread(argv[1],-1);
if(img.empty()) return -1;
nameWindiw("Example1",cv::WINDOW_SIZE);
imshow("Example1",img);
waitKey(0);
destroyWindow("Example1");
}
2. 修改图像
cv::cvtColor(img,img_gray,CV_BGR2GRAY)
彩色空间转换
参数:
- 源图像
- 转换后的图像
- 源和目标色彩空间
COLOR_BGR2HLS
(转HLS)
COLOR_BGR2GRAY
(转灰度)
- 新建文件夹并进入
mkdir Gray && Gray
- 打开终端,新建cpp文件
touch gray.cpp
- 编辑gray.cpp
gedit gray.cpp
拷入下列代码并保存
#include <opencv2/opencv.hpp>
//
#include <opencv2/imgproc/types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
Mat img = imread("girl.jpg"),img_gray;
imshow("girl",img);
cvtColor(img,img_gray,CV_BGR2GRAY);
imshow("gray",img_gray);
imwrite("gray.jpg",img_gray);
waitKey(0);
}
- 拷贝
CMakeLists.txt
将../opencv-4.5.1/samples/cpp/example_cmake
中的CMakeLists.txt
拷贝到Gray
文件夹 - 修改
CMakeLists.txt
修改前
# Define project name
project(opencv_example_project)
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})
修改后(其他不要动,仔细对比)
# Define project name
project(text)
# Declare the executable target built from your sources
add_executable(text gray.cpp)
# Link your application with OpenCV libraries
target_link_libraries(text ${OpenCV_LIBS})
-
拷贝图像
找一张图像重名为girl
拷贝到Gray
文件夹 -
编译运行
cmake .
make
./text
3. 视频
- video.cpp
mkdir Video && cd Video
touch video.cpp && gedit video.cpp
拷贝下列代码
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <string> //学习OpenCV书中无此行,疑似OpenCV版本问题
using namespace std;
int main(int argc, char** argv)
{
cv::Mat frame;
cv::namedWindow("Example3",cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap;
cap.open(string("girl.mp4"));
for(;;)
{
cap >> frame;
if(frame.empty()) break;
cv::imshow("Example3",frame);
if(cv::waitKey(33.3333) >=0 ) break;//每秒30帧,每帧停留33.3333秒
}
return 0;
}
- 拷贝并修改
CMakeLists.txt
将../opencv-4.5.1/samples/cpp/example_cmake
中的CMakeLists.txt
拷贝到Video
文件夹
修改前
# Define project name
project(opencv_example_project)
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})
修改后(其他不要动,仔细对比)
# Define project name
project(text)
# Declare the executable target built from your sources
add_executable(text video.cpp)
# Link your application with OpenCV libraries
target_link_libraries(text ${OpenCV_LIBS})
-
拷贝图像
找一张图像重名为video
拷贝到Video
文件夹 -
编译运行
cmake .
make
./text
4. Canny边缘检测
cpp
mkdir Canny边缘检测 && cd Canny边缘检测
touch canny.cpp && gedit canny.cpp
拷贝代码
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
int main(int argc, char** argv)
{
cv::Mat img = imread("girl.jpg");
cv::imshow("girl",img);
cvtColor(img,img,CV_BGR2GRAY);
//模糊
blur(img,img,Size(4,4));
cv::Canny(img,img,3,3,3);
cv::imshow("Canny",img);
cv::imwrite("canny.jpg",img);
cv::waitKey(0);
}
- 拷贝并修改
CMakeLists.txt
将../opencv-4.5.1/samples/cpp/example_cmake
中的CMakeLists.txt
拷贝到Canny边缘检测
文件夹
修改后(其他不要动,仔细对比)
# Declare the executable target built from your sources
add_executable(text canny.cpp)
-
拷贝视频
找一张图像重名为girl
拷贝到Canny边缘检测
文件夹 -
编译运行
cmake .
make
./text
5. 视频Canny边缘检测
cpp
mkdir 视频Canny边缘检测 && cd 视频Canny边缘检测
touch canny.cpp && gedit canny.cpp
拷贝代码
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
int main(int argc,char** argv)
{
cv::VideoCapture capture("girl.mp4");
while(1)
{
Mat frame;
capture >> frame;
//cv::imshow("girl",frame);
cv::cvtColor(frame,frame,CV_BGR2GRAY);
cv::blur(frame,frame,Size(4,4));
cv::Canny(frame,frame,3,60,3);
cv::imshow("Canny",frame);
//cv::waitKey(0);
if(cv::waitKey(33) >= 0) break;
}
return 0;
}
}
- 拷贝并修改
CMakeLists.txt
将../opencv-4.5.1/samples/cpp/example_cmake
中的CMakeLists.txt
拷贝到视频Canny边缘检测
文件夹
修改后(其他不要动,仔细对比)
# Declare the executable target built from your sources
add_executable(text canny.cpp)
-
拷贝视频
找一个mp4格式的视频并重名为girl
拷贝到视频Canny边缘检测
文件夹 -
编译运行
cmake .
make
./text
6. 导出视频Canny边缘检测
cpp
mkdir 导出视频Canny边缘检测 && cd 导出视频Canny边缘检测
touch canny.cpp && gedit canny.cpp
拷贝代码
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <iostream>
using namespace cv;
int main(int argc,char** argv)
{
//读取
cv::VideoCapture capture("girl.mp4");
double fps = capture.get(cv::CAP_PROP_FPS);
cv::Size size((int)capture.get(cv::CAP_PROP_FRAME_WIDTH),(int)capture.get(cv::CAP_PROP_FRAME_HEIGHT));
//写入
cv::VideoWriter writer;
//仅支持avi格式
//opencv3为CV_FOURCC(),opencv4为VideoWriter::fourcc()
//灰度图参数isColor设为false(默认为true)
writer.open("bgirl.avi",VideoWriter::fourcc('M','J','P','G'),fps,size,false);
cv::Mat frame;
for(;;)
{
capture >> frame;
if(frame.empty())break;//帧数据为空,跳出
cv::cvtColor(frame,frame,CV_BGR2GRAY);//转灰度
cv::blur(frame,frame,Size(4,4));//模糊
cv::Canny(frame,frame,3,60,3);
cv::imshow("Canny",frame);//显示帧
cv::waitKey(1000/fps);//每帧停留时间
//写入帧
writer << frame;
}
//释放内存
capture.release();
writer.release();
}
- 拷贝并修改
CMakeLists.txt
将../opencv-4.5.1/samples/cpp/example_cmake
中的CMakeLists.txt
拷贝到导出视频Canny边缘检测
文件夹
修改后(其他不要动,仔细对比)
# Declare the executable target built from your sources
add_executable(text canny.cpp)
-
拷贝视频
找一个mp4格式的视频并重名为girl
拷贝到导出视频Canny边缘检测
文件夹 -
编译运行
cmake .
make
./text