学习OpenCV 4(一)

系统Ubuntu 18.04
OpenCV 版本4.5.1
接口类型C++
安装OpenCV点我安装

0. Ubuntu OpenCV C++ 编译运行示例

  1. C++编译流程
    在这里插入图片描述

  2. 新建文件夹并进入

mkdir ShowImage && ShowImage
  1. 打开终端,新建cpp文件
touch read.cpp
  1. 编辑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);
}
  1. 拷贝CMakeLists.txt
    ../opencv-4.5.1/samples/cpp/example_cmake中的CMakeLists.txt拷贝到(你新建的文件夹)
  2. 修改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})
  1. 拷贝图像
    找一张jpg格式的图像重名为girl拷贝到ShowImage文件夹

  2. 编译运行

cmake .
make
./text
  1. 报错
    注意第3步和第6步图像名称
  2. 参考
    在这里插入图片描述

1. 显示图片

cv::imread 加载图像文件成为一个Mat对象
参数:

  1. 图像文件名
  2. 数值
    <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)彩色空间转换
参数:

  1. 源图像
  2. 转换后的图像
  3. 源和目标色彩空间
    COLOR_BGR2HLS(转HLS)
    COLOR_BGR2GRAY(转灰度)

  1. 新建文件夹并进入
mkdir Gray && Gray
  1. 打开终端,新建cpp文件
touch gray.cpp
  1. 编辑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);
}
  1. 拷贝CMakeLists.txt
    ../opencv-4.5.1/samples/cpp/example_cmake中的CMakeLists.txt拷贝到Gray文件夹
  2. 修改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})
  1. 拷贝图像
    找一张图像重名为girl拷贝到Gray文件夹

  2. 编译运行

cmake .
make
./text

3. 视频

  1. 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;
}
  1. 拷贝并修改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})
  1. 拷贝图像
    找一张图像重名为video拷贝到Video文件夹

  2. 编译运行

cmake .
make
./text

4. Canny边缘检测

  1. 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);
}
  1. 拷贝并修改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)
  1. 拷贝视频
    找一张图像重名为girl拷贝到Canny边缘检测文件夹

  2. 编译运行

cmake .
make
./text

5. 视频Canny边缘检测

  1. 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;
}
}
  1. 拷贝并修改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)
  1. 拷贝视频
    找一个mp4格式的视频并重名为girl拷贝到视频Canny边缘检测文件夹

  2. 编译运行

cmake .
make
./text

6. 导出视频Canny边缘检测

  1. 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();
}
  1. 拷贝并修改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)
  1. 拷贝视频
    找一个mp4格式的视频并重名为girl拷贝到导出视频Canny边缘检测文件夹

  2. 编译运行

cmake .
make
./text
  1. Ubuntu下C++ opencv详细教程,句句注释
  2. C++ OpenCV cvtColor CV_BGR2GRAY未声明的标识符的解决办法(四)
  3. OpenCV4 error: ‘CV_CAP_PROP_FRAME_WIDTH’ was not declared in this scope
  4. OpenCV学习笔记(四十七)——VideoWriter生成视频流highgui
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值