標(biāo)題:
張正友相機(jī)標(biāo)定源碼 利用opencv實(shí)現(xiàn)
[打印本頁(yè)]
作者:
mvp科比
時(shí)間:
2018-8-6 14:32
標(biāo)題:
張正友相機(jī)標(biāo)定源碼 利用opencv實(shí)現(xiàn)
張正友相機(jī)標(biāo)定。利用opencv實(shí)現(xiàn)
0.png
(45.3 KB, 下載次數(shù): 71)
下載附件
2018-8-7 00:35 上傳
單片機(jī)源程序如下:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace cv;
using namespace std;
void main()
{
//ifstream fin("calibdata.txt"); /* 標(biāo)定所用圖像文件的路徑 */
//ofstream fout("caliberation_result.txt"); /* 保存標(biāo)定結(jié)果的文件 */
// //讀取每一幅圖像,從中提取出角點(diǎn),然后對(duì)角點(diǎn)進(jìn)行亞像素精確化
//cout << "開(kāi)始提取角點(diǎn)………………";
//int image_count = 0; /* 圖像數(shù)量 */
//Size image_size; /* 圖像的尺寸 */
//Size board_size = Size(4, 6); /* 標(biāo)定板上每行、列的角點(diǎn)數(shù) */
//vector<Point2f> image_points_buf; /* 緩存每幅圖像上檢測(cè)到的角點(diǎn) */
//vector<vector<Point2f>> image_points_seq; /* 保存檢測(cè)到的所有角點(diǎn) */
//string filename;
//int count = -1;//用于存儲(chǔ)角點(diǎn)個(gè)數(shù)。
//while (getline(fin, filename))
//{
// image_count++;
// // 用于觀察檢驗(yàn)輸出
// cout << "image_count = " << image_count << endl;
// /* 輸出檢驗(yàn)*/
// cout << "-->count = " << count;
// Mat imageInput = imread(filename);
ofstream fout("caliberation_result.txt"); /* 保存標(biāo)定結(jié)果的文件 */
cv::namedWindow("Image");
cv::Mat imageInput;
std::vector<std::string> filelist;//存放標(biāo)定圖片路徑
int image_count = 0; /* 圖像數(shù)量 */
Size image_size; /* 圖像的尺寸 */
Size board_size = Size(4, 6); /* 標(biāo)定板上每行、列的角點(diǎn)數(shù) */
vector<Point2f> image_points_buf; /* 緩存每幅圖像上檢測(cè)到的角點(diǎn) */
vector<vector<Point2f>> image_points_seq; /* 保存檢測(cè)到的所有角點(diǎn) */
int count = -1;//用于存儲(chǔ)角點(diǎn)個(gè)數(shù)。
//生成路徑,此處表示圖片放在工程根目錄下的chessboards文件夾
for (int i = 1; i <= 21; i++)
{
std::stringstream str;//setw(int n)用來(lái)控制輸出間隔
str << "chessboards/chessboard" << std::setw(2) << std::setfill('0') << i << ".jpg";//圖片的相對(duì)路徑
std::cout << str.str() << std::endl;
filelist.push_back(str.str());
imageInput = cv::imread(str.str());
cv::imshow("Image", imageInput);
//if (image_count == 1) //讀入第一張圖片時(shí)獲取圖像寬高信息
//{
// image_size.width = imageInput.cols;
// image_size.height = imageInput.rows;
// cout << "image_size.width = " << image_size.width << endl;
// cout << "image_size.height = " << image_size.height << endl;
//}
/* 提取角點(diǎn) */
//if (0 == findChessboardCorners(imageInput, board_size, image_points_buf))
//{
// cout << "can not find chessboard corners!\n"; //找不到角點(diǎn)
// exit(1);
//}
//else
//{
findChessboardCorners(imageInput, board_size, image_points_buf);
Mat view_gray;
cvtColor(imageInput, view_gray, CV_RGB2GRAY);
findChessboardCorners(imageInput, board_size, image_points_buf);
/* 亞像素精確化 */
find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5)); //對(duì)粗提取的角點(diǎn)進(jìn)行精確化
//cornerSubPix(view_gray,image_points_buf,Size(5,5),Size(-1,-1),TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
image_points_seq.push_back(image_points_buf); //保存亞像素角點(diǎn)
/* 在圖像上顯示角點(diǎn)位置 */
drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在圖片中標(biāo)記角點(diǎn)
imshow("Camera Calibration", view_gray);//顯示圖片
waitKey(200);//暫停0.5S
}
int total = image_points_seq.size();
cout << "total = " << total << endl;
int CornerNum = board_size.width*board_size.height; //每張圖片上總的角點(diǎn)數(shù)
for (int ii = 0; ii<total; ii++)
{
if (0 == ii%CornerNum)// 24 是每幅圖片的角點(diǎn)個(gè)數(shù)。此判斷語(yǔ)句是為了輸出 圖片號(hào),便于控制臺(tái)觀看
{
int i = -1;
i = ii / CornerNum;
int j = i + 1;
cout << "--> 第 " << j << "圖片的數(shù)據(jù) --> : " << endl;
}
if (0 == ii % 3) // 此判斷語(yǔ)句,格式化輸出,便于控制臺(tái)查看
{
cout << endl;
}
else
{
cout.width(10);
}
//輸出所有的角點(diǎn)
cout << " -->" << image_points_seq[ii][0].x;
cout << " -->" << image_points_seq[ii][0].y;
}
cout << "角點(diǎn)提取完成!\n";
//以下是攝像機(jī)標(biāo)定
cout << "開(kāi)始標(biāo)定………………";
/*棋盤(pán)三維信息*/
Size square_size = Size(10, 10); /* 實(shí)際測(cè)量得到的標(biāo)定板上每個(gè)棋盤(pán)格的大小 */
vector<vector<Point3f>> object_points; /* 保存標(biāo)定板上角點(diǎn)的三維坐標(biāo) */
/*內(nèi)外參數(shù)*/
Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 攝像機(jī)內(nèi)參數(shù)矩陣 */
vector<int> point_counts; // 每幅圖像中角點(diǎn)的數(shù)量
Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 攝像機(jī)的5個(gè)畸變系數(shù):k1,k2,p1,p2,k3 */
vector<Mat> tvecsMat; /* 每幅圖像的旋轉(zhuǎn)向量 */
vector<Mat> rvecsMat; /* 每幅圖像的平移向量 */
/* 初始化標(biāo)定板上角點(diǎn)的三維坐標(biāo) */
int i, j, t;
for (t = 0; t<image_count; t++)
{
vector<Point3f> tempPointSet;
for (i = 0; i<board_size.height; i++)
{
for (j = 0; j<board_size.width; j++)
{
Point3f realPoint;
/* 假設(shè)標(biāo)定板放在世界坐標(biāo)系中z=0的平面上 */
realPoint.x = i*square_size.width;
realPoint.y = j*square_size.height;
realPoint.z = 0;
tempPointSet.push_back(realPoint);
}
}
object_points.push_back(tempPointSet);
}
/* 初始化每幅圖像中的角點(diǎn)數(shù)量,假定每幅圖像中都可以看到完整的標(biāo)定板 */
for (i = 0; i<image_count; i++)
{
point_counts.push_back(board_size.width*board_size.height);
}
/* 開(kāi)始標(biāo)定 */
calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
cout << "標(biāo)定完成!\n";
//對(duì)標(biāo)定結(jié)果進(jìn)行評(píng)價(jià)
cout << "開(kāi)始評(píng)價(jià)標(biāo)定結(jié)果………………\n";
double total_err = 0.0; /* 所有圖像的平均誤差的總和 */
double err = 0.0; /* 每幅圖像的平均誤差 */
vector<Point2f> image_points2; /* 保存重新計(jì)算得到的投影點(diǎn) */
cout << "\t每幅圖像的標(biāo)定誤差:\n";
fout << "每幅圖像的標(biāo)定誤差:\n";
for (i = 0; i<image_count; i++)
{
vector<Point3f> tempPointSet = object_points[i];
/* 通過(guò)得到的攝像機(jī)內(nèi)外參數(shù),對(duì)空間的三維點(diǎn)進(jìn)行重新投影計(jì)算,得到新的投影點(diǎn) */
projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
/* 計(jì)算新的投影點(diǎn)和舊的投影點(diǎn)之間的誤差*/
vector<Point2f> tempImagePoint = image_points_seq[i];
Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
for (int j = 0; j < tempImagePoint.size(); j++)
{
image_points2Mat.at<Vec2f>(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
tempImagePointMat.at<Vec2f>(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
}
err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
total_err += err /= point_counts[i];
std::cout << "第" << i + 1 << "幅圖像的平均誤差:" << err << "像素" << endl;
fout << "第" << i + 1 << "幅圖像的平均誤差:" << err << "像素" << endl;
}
std::cout << "總體平均誤差:" << total_err / image_count << "像素" << endl;
fout << "總體平均誤差:" << total_err / image_count << "像素" << endl << endl;
std::cout << "評(píng)價(jià)完成!" << endl;
//保存定標(biāo)結(jié)果
std::cout << "開(kāi)始保存定標(biāo)結(jié)果………………" << endl;
Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅圖像的旋轉(zhuǎn)矩陣 */
fout << "相機(jī)內(nèi)參數(shù)矩陣:" << endl;
fout << cameraMatrix << endl << endl;
fout << "畸變系數(shù):\n";
fout << distCoeffs << endl << endl << endl;
for (int i = 0; i<image_count; i++)
{
fout << "第" << i + 1 << "幅圖像的旋轉(zhuǎn)向量:" << endl;
fout << tvecsMat[i] << endl;
/* 將旋轉(zhuǎn)向量轉(zhuǎn)換為相對(duì)應(yīng)的旋轉(zhuǎn)矩陣 */
Rodrigues(tvecsMat[i], rotation_matrix);
fout << "第" << i + 1 << "幅圖像的旋轉(zhuǎn)矩陣:" << endl;
fout << rotation_matrix << endl;
fout << "第" << i + 1 << "幅圖像的平移向量:" << endl;
fout << rvecsMat[i] << endl << endl;
}
std::cout << "完成保存" << endl;
fout << endl;
/************************************************************************
顯示定標(biāo)結(jié)果
*************************************************************************/
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
Mat R = Mat::eye(3, 3, CV_32F);
std::cout << "保存矯正圖像" << endl;
string imageFileName;
std::stringstream StrStm;
for (int i = 0; i != image_count; i++)
{
std::cout << "Frame #" << i + 1 << "..." << endl;
initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
StrStm.clear();
imageFileName.clear();
string filePath = "chess";
StrStm << i + 1;
StrStm >> imageFileName;
filePath += imageFileName;
filePath += ".bmp";
Mat imageSource = imread(filePath);
Mat newimage = imageSource.clone();
//另一種不需要轉(zhuǎn)換矩陣的方式
//undistort(imageSource,newimage,cameraMatrix,distCoeffs);
remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
StrStm.clear();
filePath.clear();
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_d.jpg";
imwrite(imageFileName, newimage);
}
std::cout << "保存結(jié)束" << endl;
return;
}
復(fù)制代碼
所有資料51hei提供下載:
張正友相機(jī)標(biāo)定.zip
(6.57 MB, 下載次數(shù): 28)
2018-8-6 14:31 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1