标定前的第一步,写了一个小程序来捕获摄像头的图片。每个摄像头都有畸变参数,通过标定摄像头来去取畸变。
(1)单目照相
#include "opencv2/imgproc/imgproc.hpp"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
char filename[100]; //声明一个字符型数组,用来存放图片命名
int main(int argc, char** argv[])
{
IplImage *frame; //声明一个图像指针,用来存放下一帧图像
CvCapture *capture; //声明一个视频抽象接口
//capture = cvCreateFileCapture("1.avi");//加载本地视频
capture = cvCreateCameraCapture(0);//打开摄像头
cvNamedWindow("capture"); //声明一个播放视频窗口
int frames = (int)cvGetCaptureProperty(
capture,
CV_CAP_PROP_FRAME_COUNT
); //获取视频总帧数
int i = 0;
while (1)
{
frame = cvQueryFrame(capture);//获取视频下一帧
if (!frame) //如果读取识别则停止
break;
cvShowImage("capture", frame);
char c = cvWaitKey(33);
if (c == 's')
{
sprintf(filename, "%s%d%s", "frame", i++, ".jpg");//保存的图片名
cvSaveImage(filename, frame);
cout << "Capture the pictures successfully!" << endl;
}
if (c == 27)
break;
}
return 0;
}
(2)双目照相
#include "opencv2/imgproc/imgproc.hpp"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
char filename1[100]; //声明一个字符型数组,用来存放图片命名
char filename2[100];
int main(int argc, char** argv[])
{
IplImage *frame1; //声明一个图像指针,用来存放下一帧图像
IplImage *frame2;
CvCapture *capture1; //声明一个视频抽象接口
CvCapture *capture2;
capture1 = cvCreateCameraCapture(0);//打开摄像头
capture2 = cvCreateCameraCapture(1);
cvNamedWindow("capture1"); //声明一个播放视频窗口
cvNamedWindow("capture2");
int frames1 = (int)cvGetCaptureProperty(
capture1,
CV_CAP_PROP_FRAME_COUNT
); //获取视频总帧数
int frames2 = (int)cvGetCaptureProperty(
capture2,
CV_CAP_PROP_FRAME_COUNT
);
int i = 0,j=0;
cout << "Please press 's' to save the left and right cameras' pictures." << endl;
while (1)
{
frame1 = cvQueryFrame(capture1);//获取视频下一帧
frame2 = cvQueryFrame(capture2);
if (!(frame1||frame2)) //如果读取识别则停止
break;
cvShowImage("capture1", frame1);
cvShowImage("capture2", frame2);
char c = cvWaitKey(33);
if (c == 's')
{
sprintf(filename1, "%s%d%s", "left", i++, ".jpg");//保存的图片名
sprintf(filename2, "%s%d%s", "right", j++, ".jpg");
cvSaveImage(filename1, frame1);
cvSaveImage(filename2, frame2);
cout << "Capture the pictures successfully!" << endl;
}
if (c == 27)
break;
}
cvReleaseCapture(&capture1);
cvReleaseCapture(&capture2);
cvDestroyWindow("capture1");
cvDestroyWindow("capture2");
}