opencv的入门我从看代码入门
1
opencv图片的输入与输出:
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
int i;
const char* imagename = "H:\\matlab\\tuxaing1\\3.jpg";
//从文件中读入图像
for (int i=1; i < 10; i++)
{
Mat img = imread(imagename);
//如果读入图像失败
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
//显示图像
imshow("image", img);
}
//此函数等待按键,按键盘任意键就返回
waitKey();
return 0;
}
2
opencv如何打开自己电脑所带的摄像头
学习之后我会继续学习目标追踪的相关知识:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // 打开摄像头0
if (!cap.isOpened()) // 异常处理 判断摄像头是否出现打开等为题
{
cout << "Cannot open the video cam" << endl;
waitKey(60000);
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //输入流的宽 在这里可以
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //输入流的高
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows\\Network Shortcuts", CV_WINDOW_AUTOSIZE); // 尺寸可以自己设置,但是摄像头的文件位置要对,创建一个窗体
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // 获取一帧
if (!bSuccess) //异常处理
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows\\Network Shortcuts", frame); //显示获取到的视频帧
if (waitKey(30) == 27) //按键退出
{
cout << "esc key is pressed by user" << endl;
break;
}
}
waitKey(60000);
return 0;
}