//-----------------------------------OpenCV学习1-------------------------------------
// 程序名称:利用OpenCV做标定板,可自定义!!!
// 2016年10月 Created by孙立波(Visual Studio 2013+OpenCV2.4.9)
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{
//---生成标定图
int dd = 80; //棋盘格大小,像素为单位
int dx = 3; //行:白块开头,竖着格数为6.竖向为4个焦点
int dy = 4; //列:白块开头,横格数为8,横向为6个角点
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//注意:Size第一个为行,第二个为列!!
Mat img(Size(2 * dy * dd, 2 * dx * dd), CV_8UC1, Scalar(0));
int flag = 0;
for (int i = 0; i < 2 * dx; i++)
for (int j = 0; j < 2 * dy; j++)
{
flag = (i + j)
if (flag == 0)
{
for (int m = i*dd; m < (i + 1)*dd; m++)
for (int n = j*dd; n < (j + 1)*dd; n++)
//or((uchar *)(img.data + m * img.step))[n] = 255;
(*(img.data + m * img.step + n * img.elemSize())) = 255;
//elemSize为一个像素所占用字节个数
//*(img->imageData+m*img->widthStep+n)=255;
}
}
//---END生成标定图
imwrite("F:\\棋盘格标定图(80像素).bmp", img);
cvNamedWindow("棋盘格", 1);
imshow("棋盘格", img);
cvWaitKey(0);
cvDestroyWindow("棋盘格");
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include "videoInput.h"
using namespace std;
using namespace cv;
void main()
{
videoInput VI;
int numID = VI.listDevices();
int device1 = 0;
VI.setupDevice(device1, 640, 480, 1);
int width = VI.getWidth(device1);
int height = VI.getHeight(device1);
int size = VI.getSize(device1);
cout << "width=" << width << "\t" << "height=" << height << endl;
cout << "framesize=" << size << endl;
Mat image;
Mat frame;
image.create(Size(width, height), CV_8UC3);
frame.create(Size(width, height), CV_8UC3);
uchar* yourBuffer = (uchar*)malloc(size);
while (1)
{
VI.getPixels(device1, yourBuffer, false, false);
image.data = (uchar*)yourBuffer;
flip(image, image, 0);
waitKey(50);
imshow("采集的图像", image);
}
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
#include "videoInput.h"
using namespace cv;
using namespace std;
int main()
{
videoInput VI;
int numID = VI.listDevices();
int device1 = 0;
VI.setupDevice(device1, 640, 480, 1);
int width = VI.getWidth(device1);
int height = VI.getHeight(device1);
int size = VI.getSize(device1);
cout << "width=" << width << "\t" << "height=" << height << endl;
cout << "framesize=" << size << endl;
Mat frame;
frame.create(Size(width, height), CV_8UC3);
uchar* yourBuffer = (uchar*)malloc(size);
cout << "开始提取21张标定板图像………………" << endl;
int imageCount = 21;
int key = 0;
int count1 = 0;
for (int i = 0; i != imageCount; i++)
{
cout << "Frame#" << i + 1 << "..." << endl;
std::stringstream StrStm;
string imageFileName;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += ".jpg";
cout << "按Enter开始抽取图像,进入后可按q或者ESC键重新抽取图像,若按Enter键表明这帧图像被存入文件夹中" << endl;
int flag = 1;
while (flag)
{
VI.getPixels(device1, yourBuffer, false, false);
frame.data = (uchar*)yourBuffer;
waitKey(50);
flip(frame, frame, 0);
Mat image0 = frame;
imshow("显示抓取图像", image0);
int key2;
key2 = waitKey();
if (key2 == 13)
{
cout << "提取标定板图像成功!………………" << endl;
std::stringstream str;
str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";
std::cout << "提取的图像保存路径及文件名" << str.str() << endl;
imwrite(str.str(), image0);
flag = 0;
count1 += 1;
}
else
if (key2 == 113 || key2 == 27)
cout << "这次提取的标定板图像不成功!重新提取!!!!………………" << endl;
};
}
if (count1 == imageCount)
{
cout << "***********************………………" << endl;
cout << "***********************………………" << endl;
cout << "下面开始标定图像...................." << endl;
count1 = 0;
}
system("pause");
cout << "开始提取角点………………" << endl;
cout << "开始提取角点………………" << endl;
double time0 = static_cast<double>(getTickCount());
ofstream fout("F:\\biaoding\\biaoding_result.txt");
Size image_size;
Size board_size(9, 9);
vector<Point2f> corners;
vector<vector<Point2f>> corners_Seq;
vector<Mat> image_Seq;
int count = 0;
int image_count = imageCount;
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
std::stringstream str;
str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";
std::cout << str.str() << std::endl;
Mat image = imread(str.str());
image_size = image.size();
Mat imageGray;
cvtColor(image, imageGray, CV_RGB2GRAY);
bool patternfound = findChessboardCorners(image, board_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +
CALIB_CB_FAST_CHECK);
if (!patternfound)
{
cout << "can not find chessboard corners!\n";
continue;
exit(1);
}
else
{
cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
Mat imageTemp = image.clone();
for (int j = 0; j < corners.size(); j++)
{
circle(imageTemp, corners[j], 10, Scalar(0, 0, 255), 2, 8, 0);
}
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_corner.jpg";
imwrite("F:\\biaoding\\" + imageFileName, imageTemp);
cout << "Frame corner#" << i + 1 << "...end" << endl;
count = count + corners.size();
corners_Seq.push_back(corners);
}
image_Seq.push_back(image);
}
cout << "角点提取完成!\n";
cout << "角点提取完成!下一步摄像机定标\n";
system("pause");
cout << "开始定标………………" << endl;
Size square_size = Size(80, 80);
vector<vector<Point3f>> object_Points;
Mat image_points = Mat(1, count, CV_32FC2, Scalar::all(0));
vector<int> point_counts;
Mat intrinsic_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
Mat distortion_coeffs = Mat(1, 4, CV_32FC1, Scalar::all(0));
vector<cv::Mat> rotation_vectors;
vector<cv::Mat> translation_vectors;
for (int t = 0; t<image_count; t++)
{
vector<Point3f> tempPointSet;
for (int i = 0; i<board_size.height; i++)
{
for (int j = 0; j<board_size.width; j++)
{
Point3f tempPoint;
tempPoint.x = i*square_size.width;
tempPoint.y = j*square_size.height;
tempPoint.z = 0;
tempPointSet.push_back(tempPoint);
}
}
object_Points.push_back(tempPointSet);
}
for (int i = 0; i< image_count; i++)
{
point_counts.push_back(board_size.width*board_size.height);
}
calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, 0);
cout << "定标完成!\n";
cout << "开始评价定标结果………………" << endl;
double total_err = 0.0;
double err = 0.0;
vector<Point2f> image_points2;
cout << "每幅图像的定标误差:" << endl;
fout << "每幅图像的定标误差:" << endl << endl;
for (int i = 0; i<image_count; i++)
{
vector<Point3f> tempPointSet = object_Points[i];
projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);
vector<Point2f> tempImagePoint = corners_Seq[i];
Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
for (size_t i = 0; i != tempImagePoint.size(); i++)
{
image_points2Mat.at<Vec2f>(0, i) = Vec2f(image_points2[i].x, image_points2[i].y);
tempImagePointMat.at<Vec2f>(0, i) = Vec2f(tempImagePoint[i].x, tempImagePoint[i].y);
}
err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
total_err += err /= point_counts[i];
cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
}
cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;
cout << "评价完成!" << endl;
cout << "开始保存定标结果………………" << endl;
Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
fout << "相机内参数矩阵:" << endl;
cout << "相机内参数矩阵:" << endl;
fout << intrinsic_matrix << endl;
cout << intrinsic_matrix << endl;
fout << "畸变系数:\n";
cout << "畸变系数:\n";
fout << distortion_coeffs << endl;
cout << distortion_coeffs << endl;
for (int i = 0; i<image_count; i++)
{
fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
fout << rotation_vectors[i] << endl;
Rodrigues(rotation_vectors[i], rotation_matrix);
fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
fout << rotation_matrix << endl;
fout << "第" << i + 1 << "幅图像的平移向量:" << endl;
fout << translation_vectors[i] << endl;
}
cout << "完成保存" << endl;
fout << endl;
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
Mat R = Mat::eye(3, 3, CV_32F);
cout << "保存矫正图像" << endl;
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
Mat newCameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);
Mat t = image_Seq[i].clone();
cv::remap(image_Seq[i], t, mapx, mapy, INTER_LINEAR);
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_校正图像.jpg";
imwrite("F:\\biaoding\\" + imageFileName, t);
}
cout << "保存结束" << endl;
time0 = ((double)getTickCount() - time0) / getTickFrequency();
cout << "标定用时:" << time0 << "秒" << endl;
system("pause");
return 0;
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
int imageCount = 17;
cout << "开始提取角点………………" << endl;
double time0 = static_cast<double>(getTickCount());
ofstream fout("F:\\biaoding\\biaoding_result.txt");
Size image_size;
Size board_size(9, 9);
vector<Point2f> corners;
vector<vector<Point2f>> corners_Seq;
vector<Mat> image_Seq;
int count = 0;
int image_count = imageCount;
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
std::stringstream str;
str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";
std::cout << str.str() << std::endl;
Mat image = imread(str.str());
image_size = image.size();
Mat imageGray;
cvtColor(image, imageGray, CV_RGB2GRAY);
bool patternfound = findChessboardCorners(image, board_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +
CALIB_CB_FAST_CHECK);
if (!patternfound)
{
cout << "can not find chessboard corners!\n";
continue;
exit(1);
}
else
{
cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
Mat imageTemp = image.clone();
for (int j = 0; j < corners.size(); j++)
{
circle(imageTemp, corners[j], 10, Scalar(0, 0, 255), 2, 8, 0);
}
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_corner.jpg";
imwrite("F:\\biaoding\\" + imageFileName, imageTemp);
cout << "Frame corner#" << i + 1 << "...end" << endl;
count = count + corners.size();
corners_Seq.push_back(corners);
}
image_Seq.push_back(image);
}
cout << "角点提取完成!\n";
cout << "角点提取完成!下一步摄像机定标\n";
system("pause");
cout << "开始定标………………" << endl;
Size square_size = Size(80, 80);
vector<vector<Point3f>> object_Points;
Mat image_points = Mat(1, count, CV_32FC2, Scalar::all(0));
vector<int> point_counts;
Mat intrinsic_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
Mat distortion_coeffs = Mat(1, 4, CV_32FC1, Scalar::all(0));
vector<cv::Mat> rotation_vectors;
vector<cv::Mat> translation_vectors;
for (int t = 0; t<image_count; t++)
{
vector<Point3f> tempPointSet;
for (int i = 0; i<board_size.height; i++)
{
for (int j = 0; j<board_size.width; j++)
{
Point3f tempPoint;
tempPoint.x = i*square_size.width;
tempPoint.y = j*square_size.height;
tempPoint.z = 0;
tempPointSet.push_back(tempPoint);
}
}
object_Points.push_back(tempPointSet);
}
for (int i = 0; i< image_count; i++)
{
point_counts.push_back(board_size.width*board_size.height);
}
calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, 0);
cout << "定标完成!\n";
cout << "开始评价定标结果………………" << endl;
double total_err = 0.0;
double err = 0.0;
vector<Point2f> image_points2;
cout << "每幅图像的定标误差:" << endl;
fout << "每幅图像的定标误差:" << endl << endl;
for (int i = 0; i<image_count; i++)
{
vector<Point3f> tempPointSet = object_Points[i];
projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);
vector<Point2f> tempImagePoint = corners_Seq[i];
Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
for (size_t i = 0; i != tempImagePoint.size(); i++)
{
image_points2Mat.at<Vec2f>(0, i) = Vec2f(image_points2[i].x, image_points2[i].y);
tempImagePointMat.at<Vec2f>(0, i) = Vec2f(tempImagePoint[i].x, tempImagePoint[i].y);
}
err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
total_err += err /= point_counts[i];
cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
}
cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;
cout << "评价完成!" << endl;
cout << "开始保存定标结果………………" << endl;
Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
fout << "相机内参数矩阵:" << endl;
cout << "相机内参数矩阵:" << endl;
fout << intrinsic_matrix << endl;
cout << intrinsic_matrix << endl;
fout << "畸变系数:\n";
cout << "畸变系数:\n";
fout << distortion_coeffs << endl;
cout << distortion_coeffs << endl;
for (int i = 0; i<image_count; i++)
{
fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
fout << rotation_vectors[i] << endl;
Rodrigues(rotation_vectors[i], rotation_matrix);
fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
fout << rotation_matrix << endl;
fout << "第" << i + 1 << "幅图像的平移向量:" << endl;
fout << translation_vectors[i] << endl;
}
cout << "完成保存" << endl;
fout << endl;
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
Mat R = Mat::eye(3, 3, CV_32F);
cout << "保存矫正图像" << endl;
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
Mat newCameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);
Mat t = image_Seq[i].clone();
cv::remap(image_Seq[i], t, mapx, mapy, INTER_LINEAR);
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_校正图像.jpg";
imwrite("F:\\biaoding\\" + imageFileName, t);
}
cout << "保存结束" << endl;
time0 = ((double)getTickCount() - time0) / getTickFrequency();
cout << "标定用时:" << time0 << "秒" << endl;
system("pause");
return 0;
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
double time0 = static_cast<double>(getTickCount());
ofstream fout("biaoding_result.txt");
cout << "开始提取角点………………" << endl;
int image_count = 17;
Size image_size;
Size board_size = Size(9, 9);
vector<Point2f> corners;
vector<vector<Point2f>> corners_Seq;
vector<Mat> image_Seq;
int count = 0;
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += ".jpg";
std::stringstream str;
str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";
std::cout << str.str() << std::endl;
Mat image = cv::imread(str.str());
image_size = image.size();
Mat imageGray;
cvtColor(image, imageGray, CV_RGB2GRAY);
bool patternfound = findChessboardCorners(image, board_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +
CALIB_CB_FAST_CHECK);
if (!patternfound)
{
cout << "can not find chessboard corners!\n";
continue;
exit(1);
}
else
{
cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
Mat imageTemp = image.clone();
for (int j = 0; j < corners.size(); j++)
{
circle(imageTemp, corners[j], 10, Scalar(0, 0, 255), 2, 8, 0);
}
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_corner.jpg";
imwrite("F:\\biaoding\\" + imageFileName, imageTemp);
cout << "Frame corner#" << i + 1 << "...end" << endl;
count = count + corners.size();
corners_Seq.push_back(corners);
}
image_Seq.push_back(image);
}
cout << "角点提取完成!\n";
cout << "开始定标………………" << endl;
Size square_size = Size(80, 80);
vector<vector<Point3f>> object_Points;
Mat image_points = Mat(1, count, CV_32FC2, Scalar::all(0));
vector<int> point_counts;
Mat intrinsic_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
Mat distortion_coeffs = Mat(1, 4, CV_32FC1, Scalar::all(0));
vector<cv::Mat> rotation_vectors;
vector<cv::Mat> translation_vectors;
for (int t = 0; t<image_count; t++)
{
vector<Point3f> tempPointSet;
for (int i = 0; i<board_size.height; i++)
{
for (int j = 0; j<board_size.width; j++)
{
Point3f tempPoint;
tempPoint.x =i*square_size.width;
tempPoint.y =j*square_size.height;
tempPoint.z = 0;
tempPointSet.push_back(tempPoint);
}
}
object_Points.push_back(tempPointSet);
}
for (int i = 0; i< image_count; i++)
{
point_counts.push_back(board_size.width*board_size.height);
}
calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, 0);
cout << "定标完成!\n";
cout << "开始评价定标结果………………" << endl;
double total_err = 0.0;
double err = 0.0;
vector<Point2f> image_points2;
cout << "每幅图像的定标误差:" << endl;
fout << "每幅图像的定标误差:" << endl << endl;
for (int i = 0; i<image_count; i++)
{
vector<Point3f> tempPointSet = object_Points[i];
projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);
vector<Point2f> tempImagePoint = corners_Seq[i];
Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
for (size_t i = 0; i != tempImagePoint.size(); i++)
{
image_points2Mat.at<Vec2f>(0, i) = Vec2f(image_points2[i].x, image_points2[i].y);
tempImagePointMat.at<Vec2f>(0, i) = Vec2f(tempImagePoint[i].x, tempImagePoint[i].y);
}
err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
total_err += err /= point_counts[i];
cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
}
cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;
cout << "评价完成!" << endl;
cout << "开始保存定标结果………………" << endl;
Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
fout << "相机内参数矩阵:" << endl;
cout << "相机内参数矩阵:" << endl;
fout << intrinsic_matrix << endl;
cout << intrinsic_matrix << endl;
fout << "畸变系数:\n";
cout << "畸变系数:\n";
fout << distortion_coeffs << endl;
cout << distortion_coeffs << endl;
for (int i = 0; i<image_count; i++)
{
fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
fout << rotation_vectors[i] << endl;
Rodrigues(rotation_vectors[i], rotation_matrix);
fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
fout << rotation_matrix << endl;
fout << "第" << i + 1 << "幅图像的平移向量:" << endl;
fout << translation_vectors[i] << endl;
}
cout << "完成保存" << endl;
fout << endl;
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
Mat R = Mat::eye(3, 3, CV_32F);
cout << "保存矫正图像" << endl;
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
Mat newCameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);
Mat t = image_Seq[i].clone();
cv::remap(image_Seq[i], t, mapx, mapy, INTER_LINEAR);
string imageFileName;
std::stringstream StrStm;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_校正图像.jpg";
imwrite("F:\\biaoding\\" + imageFileName, t);
}
cout << "保存结束" << endl;
time0 = ((double)getTickCount() - time0) / getTickFrequency();
cout << "标定用时:" << time0 << "秒" << endl;
system("pause");
return 0;
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
#include <iomanip>
#include <vector>
#include "CameraCalibrator.h"
int main()
{
cv::namedWindow("Board Image");
cv::Mat image;
std::vector<std::string> filelist;
for (int i = 1; i <= 20; i++) {
std::stringstream str;
str << "F:\\biaoding\\colar4\\\img" << std::setw(2) << std::setfill('0') << i << ".jpg";
std::cout << str.str() << std::endl;
filelist.push_back(str.str());
image = cv::imread(str.str(), 0);
cv::imshow("Board Image", image);
cv::waitKey(100);
}
waitKey(0);
CameraCalibrator cameraCalibrator;
cv::Size boardSize(9, 9);
cameraCalibrator.addChessboardPoints(
filelist,
boardSize, "Detected points");
cameraCalibrator.calibrate(image.size());
image = cv::imread(filelist[6], 0);
cv::Mat uImage = cameraCalibrator.remap(image);
cv::Mat cameraMatrix = cameraCalibrator.getCameraMatrix();
std::cout << " Camera intrinsic: " << cameraMatrix.rows << "x" << cameraMatrix.cols << std::endl;
std::cout << cameraMatrix.at<double>(0, 0) << " " << cameraMatrix.at<double>(0, 1) << " " << cameraMatrix.at<double>(0, 2) << std::endl;
std::cout << cameraMatrix.at<double>(1, 0) << " " << cameraMatrix.at<double>(1, 1) << " " << cameraMatrix.at<double>(1, 2) << std::endl;
std::cout << cameraMatrix.at<double>(2, 0) << " " << cameraMatrix.at<double>(2, 1) << " " << cameraMatrix.at<double>(2, 2) << std::endl;
cv::namedWindow("Original Image");
cv::imshow("Original Image", image);
cv::namedWindow("Undistorted Image");
cv::imshow("Undistorted Image", uImage);
waitKey();
return 0;
}
**CameraCalibrator.h:**
#ifndef CAMERACALIBRATOR_H
#define CAMERACALIBRATOR_H
#include <vector>
#include <iostream>
#include <opencv2/core/core.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/highgui/highgui.hpp>
class CameraCalibrator {
std::vector<std::vector<cv::Point3f> > objectPoints;
std::vector<std::vector<cv::Point2f> > imagePoints;
cv::Mat cameraMatrix;
cv::Mat distCoeffs;
int flag;
cv::Mat map1, map2;
bool mustInitUndistort;
public:
CameraCalibrator() : flag(0), mustInitUndistort(true) {}
int addChessboardPoints(const std::vector<std::string>& filelist, cv::Size & boardSize, std::string windowName = "");
void addPoints(const std::vector<cv::Point2f>& imageCorners, const std::vector<cv::Point3f>& objectCorners);
double calibrate(const cv::Size imageSize);
void setCalibrationFlag(bool radial8CoeffEnabled = false, bool tangentialParamEnabled = false);
cv::Mat remap(const cv::Mat &image);
cv::Mat getCameraMatrix() { return cameraMatrix; }
cv::Mat getDistCoeffs() { return distCoeffs; }
};
#endif
**CameraCalibrator.cpp:**
#include "CameraCalibrator.h"
int CameraCalibrator::addChessboardPoints(
const std::vector<std::string>& filelist,
cv::Size & boardSize,
std::string windowName) {
std::vector<cv::Point2f> imageCorners;
std::vector<cv::Point3f> objectCorners;
for (int i = 0; i<boardSize.height; i++) {
for (int j = 0; j<boardSize.width; j++) {
objectCorners.push_back(cv::Point3f(i, j, 0.0f));
}
}
cv::Mat image;
int successes = 0;
for (int i = 0; i<filelist.size(); i++) {
image = cv::imread(filelist[i], 0);
bool found = cv::findChessboardCorners(
image, boardSize, imageCorners);
cv::cornerSubPix(image, imageCorners,
cv::Size(5, 5),
cv::Size(-1, -1),
cv::TermCriteria(cv::TermCriteria::MAX_ITER +
cv::TermCriteria::EPS,
30,
0.1));
if (imageCorners.size() == boardSize.area()) {
addPoints(imageCorners, objectCorners);
successes++;
}
if (windowName.length()>0) {
cv::drawChessboardCorners(image, boardSize, imageCorners, found);
cv::imshow(windowName, image);
cv::waitKey(100);
}
}
return successes;
}
void CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, const std::vector<cv::Point3f>& objectCorners) {
imagePoints.push_back(imageCorners);
objectPoints.push_back(objectCorners);
}
double CameraCalibrator::calibrate(const cv::Size imageSize)
{
mustInitUndistort = true;
std::vector<cv::Mat> rvecs, tvecs;
return
calibrateCamera(objectPoints,
imagePoints,
imageSize,
cameraMatrix,
distCoeffs,
rvecs, tvecs,
flag);
}
cv::Mat CameraCalibrator::remap(const cv::Mat &image) {
cv::Mat undistorted;
if (mustInitUndistort) {
cv::initUndistortRectifyMap(
cameraMatrix,
distCoeffs,
cv::Mat(),
cv::Mat(),
image.size(),
CV_32FC1,
map1, map2);
mustInitUndistort = false;
}
cv::remap(image, undistorted, map1, map2,
cv::INTER_LINEAR);
return undistorted;
}
void CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, bool tangentialParamEnabled) {
flag = 0;
if (!tangentialParamEnabled) flag += CV_CALIB_ZERO_TANGENT_DIST;
if (radial8CoeffEnabled) flag += CV_CALIB_RATIONAL_MODEL;
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
#include "videoInput.h"
using namespace cv;
using namespace std;
void main()
{
videoInput VI;
int numID = VI.listDevices();
int device1 = 0;
VI.setupDevice(device1, 640, 480, 1);
int width = VI.getWidth(device1);
int height = VI.getHeight(device1);
int size = VI.getSize(device1);
cout << "width=" << width << "\t" << "height=" << height << endl;
cout << "framesize=" << size << endl;
Mat frame;
frame.create(Size(width, height), CV_8UC3);
uchar* yourBuffer = (uchar*)malloc(size);
cout << "开始提取21张标定板图像………………" << endl;
int imageCount = 21;
int key = 0;
int count1 = 0;
for (int i = 0; i != imageCount; i++)
{
cout << "Frame#" << i + 1 << "..." << endl;
std::stringstream StrStm;
string imageFileName;
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += ".jpg";
cout << "按Enter开始抽取图像,进入后可按q或者ESC键重新抽取图像,若按Enter键表明这帧图像被存入文件夹中" << endl;
int flag = 1;
while (flag)
{
VI.getPixels(device1, yourBuffer, false, false);
frame.data = (uchar*)yourBuffer;
waitKey(50);
flip(frame, frame, 0);
Mat image0 = frame;
imshow("显示抓取图像", image0);
int key2;
key2 = waitKey();
if (key2 == 13)
{
cout << "提取标定板图像成功!………………" << endl;
std::stringstream str;
str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";
std::cout << "提取的图像保存路径及文件名" << str.str() << endl;
imwrite(str.str(), image0);
flag = 0;
count1 += 1;
}
else
if (key2 == 113 || key2 == 27)
cout << "这次提取的标定板图像不成功!重新提取!!!!………………" << endl;
};
}
if (count1 == imageCount)
{
cout << "***********************………………" << endl;
cout << "***********************………………" << endl;
cout << "下面开始标定图像...................." << endl;
count1 = 0;
}
system("pause");
}