VC++中使用OpenCV读取图像、读取本地视频、读取摄像头并实时显示

VC++中使用OpenCV读取图像、读取本地视频、读取摄像头并实时显示

最近闲着跟着油管博主murtazahassan,学习了一下LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision,对应的Github源代码地址为:Learn-OpenCV-cpp-in-4-Hours

关于OpenCV

OpenCV是一个开源的计算机视觉库,其官网地址为:https://opencv.org/,对应Github源码地址为:https://github.com/opencv/opencv,目前来说OpenCV对C++、Python的支持比较友好,同时还支持Java、Javascript、C#等语言。

OpenCV官网文档地址:https://docs.opencv.org/4.x/d9/df8/tutorial_root.html

OpenCV官方给的示例C++程序:

cpp 复制代码
// Change path/to/image to a real path of an image, then build this demo with OpenCV package and run it
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main()
{
    std::string image_path = "path/to/image";
    Mat img = imread(image_path, IMREAD_COLOR);

    imshow("Display window", img);
    int k = waitKey(0); // Wait for a keystroke in the window
    return 0;
}

关于Windows下Visual Studio 2022中配置OpenCV库的使用我就不赘余了,方便的话可以直接看Setup OpenCV in Visual Studio 2022 for C/C++ DevelopmentOpenCV C++ and Microsoft Visual Studio: A Complete Tutorial on Installation and Usage for Beginners这个视频教程。当然也可以参考油管博主murtazahassan对应的Github仓库Learn-OpenCV-cpp-in-4-Hours里面的Installing OpenCV on Windows for C++,如下图所示:

关于在Mac下使用XCode运行OpenCV的可以参考Installing OpenCV on Mac for C++

Visual Studio中使用OpenCV读取图像

首先使用VS2017新建一个控制台项目OpencvDemo01,并在项目根目录放置一些资源文件,资源文件下载地址为:https://github.com/murtazahassan/Learn-OpenCV-cpp-in-4-Hours/tree/main/Resources

如下图所示:

在Visual Studio中使用C++ OpenCV库读取图像并显示很简单,示例代码如下:

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <string>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	std::string imgPath = "Resources/test.png";

	cv::Mat img;
	img = cv::imread(imgPath);
	cv::imshow("Show Image", img);

	cv::waitKey(0);

	cv::destroyAllWindows();
	
	return 0;
}

运行结果如下图所示:

使用OpenCV读取本地视频

示例代码如下:

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
	std::string videoPath = "Resources/test_video.mp4";
	cv::VideoCapture videoCap(videoPath);

	cv::Mat frame;

	if (videoCap.isOpened()) {
		while (videoCap.read(frame)) {
			cv::imshow("Video Frame", frame);

			char chKey = cv::waitKey(10);
			if (chKey == 27) {
				break;
			}
		}
	}

	videoCap.release();

	cv::destroyAllWindows();


	return 0;
}

运行结果如下图所示:

OpenCV读取摄像头数据

OpenCV读取本地摄像头数据也很简单,示例代码如下:

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

// 摄像头中读取每一帧图像,然后输出到屏幕,按ESC键退出

int main()
{
	cv::VideoCapture cap(0);

	cv::Mat frame;

	if (cap.isOpened()) {	// 如果VideoCapture初始化成功
		while (cap.read(frame)) {
			cv::imshow("WebCam Image", frame);

			char chKey = cv::waitKey(10);
			if (chKey == 27) {	// 27 对应ESC键的ASCII码
				// 如果按下了ESC键,则退出循环
				break;
			}
		}
	}

	cap.release();
	cv::destroyAllWindows();

	return 0;
}

运行结果如下图所示:

参考资料

相关推荐
EasyCVR4 分钟前
国标GB28181/RTSP/ONVIF/RTMP视频监控平台EasyCVR视频质量诊断花屏/蓝屏/画面模糊/冻结检测
网络·数据库·音视频
C^h8 分钟前
RTthread中的内存池理解
linux·数据库·c++·算法·嵌入式
lcj251114 分钟前
蓝桥杯C++:数据结构(功能导向速查)
数据结构·c++·蓝桥杯
liulilittle33 分钟前
eBPF tc prog
服务器·网络·c++·网络协议·tcp/ip·性能·perf
cui_ruicheng34 分钟前
C++ 新特性(下):可变参数模板与 STL 扩展机制
开发语言·c++·c++11
枳实-叶41 分钟前
嵌入式 Linux 下 ALSA 音频采集与 PCM 播放流程详解
linux·音视频·pcm
|_⊙1 小时前
C++ 多态
c++
福楠1 小时前
现代C++ | 智能指针
c语言·开发语言·c++
汉克老师1 小时前
GESP5级C++考试语法知识(十二、递归算法(二))
c++·算法·记忆化搜索·时间复杂度·递归算法·gesp5级·gesp五级
旺仔.2911 小时前
顺序容器:Array 数组 详解
c++