OpenCV学习笔记(1)
(1)图片显示
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
char *filename = "C:/Users/Administrator/Desktop/picture/1.jpg";
Mat Image = imread(filename, IMREAD_UNCHANGED); //从文件中读取图片
if (Image.data==NULL)
{
return -1;
}
namedWindow("Example1", WINDOW_AUTOSIZE);
imshow("Example1", Image); //显示窗口
waitKey(0);
return 0;
}
(2)视频显示
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture(string("C:Users/Administrator/Desktop/picture/fff.avi"));
if (!capture.isOpened())
{
std::cout << "video not open." << std::endl;
return 1;
}
//获取当前视频帧率
double rate = capture.get(CV_CAP_PROP_FPS);
//当前视频帧
Mat frame;
//每一帧之间的延时
//与视频的帧率相对应
int delay = 1000 / rate;
bool stop(false);
while (!stop)
{
if (!capture.read(frame))
{
cout << "no video frame" << endl;
break;
}
//此处为添加对视频的每一帧的操作方法
int frame_num = capture.get(CV_CAP_PROP_POS_FRAMES);
cout << "Frame Num : " << frame_num << endl;
if (frame_num == 20)
{
capture.set(CV_CAP_PROP_POS_FRAMES, 10);
}
imshow("video", frame);
//引入延时
//也可通过按键停止
if (waitKey(delay)>0)
stop = true;
}
//关闭视频,手动调用析构函数(非必须)
capture.release();
}
注:一开始视频显示不了,随后通过查阅资料发现,整个项目缺少Opencv_ffmpeg_64.dll文件,放入项目中即可。
(3)边缘检测
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat ImageIn;
VideoCapture cap(0); //从摄像头中读取图像数据
while (true)
{
cap >> ImageIn;
imshow("ExampleIn", ImageIn);
Mat ImageOut1;
Canny(ImageIn, ImageOut1, 40, 140, 3, true); //边缘检测
imshow("ExampleOut1", ImageOut1);
waitKey(20);
}
return 0;
}
上面绝大多数代码都是我在学习《学习OPenCV3》时参(chao)考(xie)的代码。