OpenCV练习1中的基本配置参考上一篇文章,关键操作在于把项目本身的库引入好以及系统环境变量配置好即可。
读取图片需要的Resources资源链接:Download Resources - Computer Vision Zone
基本操作
话不多说直接上代码,我切割成为三个部分,主要是图片、视频和摄像头操作
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
// 以下为图片操作
void main()
{
string path = "Resources/test.png";
Mat img = imread(path);
imshow("Image", img);
waitKey(0);//0代表无限大
}
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
// 以下为视频操作
void main()
{
string path = "Resources/test_video.mp4";
VideoCapture cap(path);
Mat img;
while (true)
{
cap.read(img);
imshow("Image", img);
waitKey(20);//报错是因为没有更多的图片显示
}
}
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
// 以下为摄像头操作
void main()
{
VideoCapture cap(0);//0自带,有其他的自选
Mat img;
while (true)
{
cap.read(img);
imshow("Image", img);
waitKey(1);//写1避免太慢
}
}
Comments NOTHING