目录
一、导入图片
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//导入图片
int main()
{
string path="C:\\Users\\Rong\\Desktop\\Resources\\test.png";
Mat img = imread(path);
imshow("Image", img);
waitKey(0);
}
注:图片路径应该是双反斜杠!
二、导入视频
视频就是一帧一帧的图片,使用while循环遍历每一帧
//导入视频
int main() {
string path = "C:\\Users\\Rong\\Desktop\\Resources\\test_video.mp4";
VideoCapture cap(path);
Mat img;
while (true) {
cap.read(img);
imshow("Video", img);
waitKey(20);
}
}
三、摄像头
把那个视频的路径变为0 因为在我自己电脑上只有一个摄像头(如果写1会报错!)
//摄像头
int main() {
VideoCapture cap(0);
Mat img;
while (true) {
cap.read(img);
imshow("Webcom", img);
waitKey(20);
}
}