VC++中使用OpenCV讀取圖像、讀取本地視頻、讀取攝像頭并實時顯示
最近閑著跟著油管博主murtazahassan,學習了一下LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision,對應(yīng)的Github源代碼地址為:Learn-OpenCV-cpp-in-4-Hours
關(guān)于OpenCV
OpenCV是一個開源的計算機視覺庫,其官網(wǎng)地址為:https://opencv.org/,對應(yīng)Github源碼地址為:https://github.com/opencv/opencv,目前來說OpenCV對C++、Python的支持比較友好,同時還支持Java、Javascript、C#等語言。
OpenCV官網(wǎng)文檔地址:https://docs.opencv.org/4.x/d9/df8/tutorial_root.html
OpenCV官方給的示例C++程序:
// 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;
}
關(guān)于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對應(yīng)的Github倉庫Learn-OpenCV-cpp-in-4-Hours里面的Installing OpenCV on Windows for C++,如下圖所示:
關(guān)于在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庫讀取圖像并顯示很簡單,示例代碼如下:
#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;
}
運行結(jié)果如下圖所示:
使用OpenCV讀取本地視頻
示例代碼如下:
#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;
}
運行結(jié)果如下圖所示:
OpenCV讀取攝像頭數(shù)據(jù)
OpenCV讀取本地攝像頭數(shù)據(jù)也很簡單,示例代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-799267.html
#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 對應(yīng)ESC鍵的ASCII碼
// 如果按下了ESC鍵,則退出循環(huán)
break;
}
}
}
cap.release();
cv::destroyAllWindows();
return 0;
}
運行結(jié)果如下圖所示:文章來源地址http://www.zghlxwxcb.cn/news/detail-799267.html
參考資料
- LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision
- murtazahassan/Learn-OpenCV-cpp-in-4-Hours
- OpenCV官網(wǎng)
- OpenCV-Get Started
- OpenCV Github倉庫源代碼
- OpenCV tutorial
到了這里,關(guān)于VC++中使用OpenCV讀取圖像、讀取本地視頻、讀取攝像頭并實時顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!