这里附上OpenCV简单的图片采集程序,按‘q' 获取,按esc退出!
#include "opencv2/opencv.hpp"
#include
#include
using namespace cv;
using namespace std;
int main()
{
VideoCapture inputVideo(0);
inputVideo.set(CV_CAP_PROP_FRAME_WIDTH, 800);
inputVideo.set(CV_CAP_PROP_FRAME_HEIGHT, 600);
if (!inputVideo.isOpened())
{
cout << "Could not open the input video " << endl;
return -1;
}
Mat frame;
string imgname;
int f = 1;
while (1) //Show the image captured in the window and repeat
{
inputVideo >> frame; // read
if (frame.empty()) break; // check if at end
imshow("Camera", frame);
char key = waitKey(1);
if (key == 27)break;
if (key == 'q' || key == 'Q')
{
imgname = to_string(f++) + ".jpg";
imwrite(imgname, frame);
}
}
cout << "Finished writing" << endl;
return 0;
}
注意这里inputVideo.set(CV_CAP_PROP_FRAME_WIDTH, 800);
为了防止可能产生极大的扭曲,所以选择使用两参数,并且选择错切和桶形畸变。
#include "opencv2/opencv.hpp"
#include
#include
using namespace cv;
using namespace std;
int main()
{
VideoCapture inputVideo(1);
inputVideo.set(CV_CAP_PROP_FRAME_WIDTH, 800);
inputVideo.set(CV_CAP_PROP_FRAME_HEIGHT, 600);
if (!inputVideo.isOpened())
{
cout << "Could not open the input video: " << endl;
return -1;
}
Mat frame;
Mat frameCalibration;
inputVideo >> frame;
Mat cameraMatrix = Mat::eye(3, 3, CV_64F);
cameraMatrix.at(0, 0) = 917.052472085750;
cameraMatrix.at(0, 1) = 0.657056681717874;
cameraMatrix.at(0, 2) = 408.884053678499;
cameraMatrix.at(1, 1) = 916.541676777971;
cameraMatrix.at(1, 2) = 332.189066871859;
Mat distCoeffs = Mat::zeros(5, 1, CV_64F);
distCoeffs.at(0, 0) = -0.108750634204250;
distCoeffs.at(1, 0) = -0.155068804309725;
distCoeffs.at(2, 0) = -0.00261486335789016;
distCoeffs.at(3, 0) = 0.00154770538482982;
distCoeffs.at(4, 0) = 0;
Mat view, rview, map1, map2;
Size imageSize;
imageSize = frame.size();
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
imageSize, CV_16SC2, map1, map2);
while (1) //Show the image captured in the window and repeat
{
inputVideo >> frame; // read
if (frame.empty()) break; // check if at end
remap(frame, frameCalibration, map1, map2, INTER_LINEAR);
imshow("Origianl", frame);
imshow("Calibration", frameCalibration);
char key = waitKey(1);
if (key == 27 || key == 'q' || key == 'Q')break;
}
return 0;
}