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++ Development或OpenCV 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;
}运行结果如下图所示:
