1.介紹
-
網上關于這類標定有挺多教程的,但由于這個攝像頭的特殊性,所以不可能完全安裝教程來走,
目前來說有3種標定方法: -
ROS作業系統來標定
-
matlab標定
-
opencv標定
這三種方法我先試了用ROS來標定,本來信心滿滿,但是中途遇到賊多錯誤,我干脆放棄了,如果有興趣也可以參考下面這篇文章,但不建議這么干因為真的很麻煩,有興趣可以看看下面的鏈接
參考鏈接 -
由于這個攝像頭的特殊性:通過兩條像紙一樣的線直接連接到jetson nano的板子上,而不是通過usb資料線連接,所以我不能將它連到我自己的window系統上用matlab進行標定,(聽政哥說用win上matlab進行標定很簡單,好像Linux下也有matlab?不知道,有興趣的可以去了解下)
-
所以最終我采用了opencv的方法進行標定,
2.opencv進行標定
-
準備一塊標定板,可以列印出來然后貼在一塊木板或者其他東西上,如下圖(10x7的棋盤圖的pdf檔案我會放在文章最后的資料包里)

-
進行拍攝照片,采集圖片集,
這里我們需要用到拍攝用的原始碼,我的原始碼是微雪公司客服發給我的,應該是專門為imx-219雙目攝像頭準備的,如果你是其他型號的攝像頭,建議找客服要資料,同理,原始碼我也會放在文章的結尾,
第一步
我們先打開下載好的09xxxx檔案,如下圖

檔案夾里有個readme.txt檔案,也可以打開它照著他的步驟走也行,
這里我簡單介紹一下操作

點擊打開09-save-xxxx-xxx(xxx只是我太懶不行打字),然后右鍵點擊選擇在終端打開,然后就依次運行readme.txt里的指令就行了(這里我就不把指令打出來了,檔案里都有,值得一提的是最后那個./imagecapture_camera指令運行的時候別搞錯路徑,是在build檔案夾這個路徑下運行,
當你成功運行后會出現這樣的視窗,有左右兩個攝像頭分別對應,

然后這是要注意的有: -
按s是進行拍照,一次同時拍下左右兩張,
-
按q或者esc鍵是退出拍照,
-
如果按s的時候沒反應(按s的時候不是在程式視窗里輸入s,應該退出輸入狀態,可以先點擊除任務框外的任意一處地方,在按s)
-
拍的照片盡量在30張左右,這樣比較準確,
-
按s回傳images saved!證明已經拍了一張,
-
拍攝完的照片會保存在images檔案夾里面
-
如果沒有images檔案夾,那就自己先建一個在進行拍攝,


棋盤圖的擺設方法可以參考下面的例子:

(圖片來源于ros-wiki)
第二步
將09-save-xxx-xxx檔案夾下的images里的圖片分類,在images下新建兩個檔案夾一個叫l一個叫r,然后把左攝像頭拍的照片放進l檔案夾,將右攝像頭拍的照片放進r檔案夾,
最終整理為下圖

第三步
使用opencv進行標定,根據之前的教程,我們應該已經下載并編譯好了opencv-3.4.1,我們打開主目錄,發現有個opencv-3.4.1檔案夾,如果沒有那就是還沒裝opencv或者那個檔案夾不知道給你放哪去了,沒有的話請看我之前的教程:我的教程
點擊進入opencv-3.4.1 新建一個檔案夾test1,然后點擊進入test1檔案夾,右鍵在終端打開,在終端輸入touch test.cpp然后再輸入gedit test.cpp,然后將下面的代碼拷貝進去
#if 1
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include<stdlib.h>
//此處引數需要根據棋盤格個數修改
//例如 黑白棋盤格 寬(w)為10個棋盤格 那么 w 為 10 -1 = 9
#define w 9 //棋盤格寬的黑白交叉點個數
#define h 6 //棋盤格高的黑白交叉點個數
const float chessboardSquareSize = 24.6f; //每個棋盤格方塊的邊長 單位 為 mm
using namespace std;
using namespace cv;
//從 xml 檔案中讀取圖片存盤路徑
static bool readStringList(const string& filename, vector<string>& list)
{
list.resize(0);
FileStorage fs(filename, FileStorage::READ);
if (!fs.isOpened())
return false;
FileNode n = fs.getFirstTopLevelNode();
if (n.type() != FileNode::SEQ)
return false;
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
list.push_back((string)*it);
return true;
}
//記錄棋盤格角點個數
static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners)
{
corners.resize(0);
for (int i = 0; i < boardSize.height; i++) //height和width位置不能顛倒
for (int j = 0; j < boardSize.width; j++)
{
corners.push_back(Point3f(j*squareSize, i*squareSize, 0));
}
}
bool calibrate(Mat& intrMat, Mat& distCoeffs, vector<vector<Point2f>>& imagePoints,
vector<vector<Point3f>>& ObjectPoints, Size& imageSize, const int cameraId,
vector<string> imageList)
{
double rms = 0; //重投影誤差
Size boardSize;
boardSize.width = w;
boardSize.height = h;
vector<Point2f> pointBuf;
float squareSize = chessboardSquareSize;
vector<Mat> rvecs, tvecs; //定義兩個攝像頭的旋轉矩陣 和平移向量
bool ok = false;
int nImages = (int)imageList.size() / 2;
cout <<"圖片張數"<< nImages;
namedWindow("View", 1);
int nums = 0; //有效棋盤格圖片張數
for (int i = 0; i< nImages; i++)
{
Mat view, viewGray;
cout<<"Now: "<<imageList[i * 2 + cameraId]<<endl;
view = imread(imageList[i * 2 + cameraId], 1); //讀取圖片
imageSize = view.size();
cvtColor(view, viewGray, COLOR_BGR2GRAY); //轉化成灰度圖
bool found = findChessboardCorners(view, boardSize, pointBuf,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);//尋找棋盤格角點
if (found)
{
nums++;
cornerSubPix(viewGray, pointBuf, Size(11, 11),
Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
drawChessboardCorners(view, boardSize, Mat(pointBuf), found);
bitwise_not(view, view);
imagePoints.push_back(pointBuf);
cout << '.';
}
else{
cout<<"Wrong"<<endl;
}
imshow("View", view);
waitKey(100);
}
cout << "有效棋盤格張數" << nums << endl;
//calculate chessboardCorners
calcChessboardCorners(boardSize, squareSize, ObjectPoints[0]);
ObjectPoints.resize(imagePoints.size(), ObjectPoints[0]);
rms = calibrateCamera(ObjectPoints, imagePoints, imageSize, intrMat, distCoeffs,
rvecs, tvecs);
ok = checkRange(intrMat) && checkRange(distCoeffs);
if (ok)
{
cout << "done with RMS error=" << rms << endl;
return true;
}
else
return false;
}
int main()
{
//initialize some parameters
bool okcalib = false;
Mat intrMatFirst, intrMatSec, distCoeffsFirst, distCoffesSec;
Mat R, T, E, F, RFirst, RSec, PFirst, PSec, Q;
vector<vector<Point2f>> imagePointsFirst, imagePointsSec;
vector<vector<Point3f>> ObjectPoints(1);
Rect validRoi[2];
Size imageSize;
int cameraIdFirst = 0, cameraIdSec = 1;
double rms = 0;
//get pictures and calibrate
vector<string> imageList;
string filename = "stereo_calibration.xml";
bool okread = readStringList(filename, imageList);
if (!okread || imageList.empty())
{
cout << "can not open " << filename << " or the string list is empty" << endl;
return false;
}
if (imageList.size() % 2 != 0)
{
cout << "Error: the image list contains odd (non-even) number of elements\n";
return false;
}
FileStorage fs("intrinsics.yml", FileStorage::WRITE);
//calibrate
cout << "calibrate left camera..." << endl;
okcalib = calibrate(intrMatFirst, distCoeffsFirst, imagePointsFirst, ObjectPoints,
imageSize, cameraIdFirst, imageList);
if (!okcalib)
{
cout << "fail to calibrate left camera" << endl;
return -1;
}
else
{
cout << "calibrate the right camera..." << endl;
}
okcalib = calibrate(intrMatSec, distCoffesSec, imagePointsSec, ObjectPoints,
imageSize, cameraIdSec, imageList);
fs << "M1" << intrMatFirst << "D1" << distCoeffsFirst <<
"M2" << intrMatSec << "D2" << distCoffesSec;
if (!okcalib)
{
cout << "fail to calibrate the right camera" << endl;
return -1;
}
destroyAllWindows();
//estimate position and orientation
cout << "estimate position and orientation of the second camera" << endl
<< "relative to the first camera..." << endl;
cout << "intrMatFirst:";
cout << intrMatFirst << endl;
cout << "distCoeffsFirst:";
cout << distCoeffsFirst << endl;
cout << "intrMatSec:";
cout << intrMatSec << endl;
cout << "distCoffesSec:";
cout << distCoffesSec << endl;
rms = stereoCalibrate(ObjectPoints, imagePointsFirst, imagePointsSec,
intrMatFirst, distCoeffsFirst, intrMatSec, distCoffesSec,
imageSize, R, T, E, F, CALIB_USE_INTRINSIC_GUESS,//CV_CALIB_FIX_INTRINSIC,
TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, 1e-6)); //計算重投影誤差
cout << "done with RMS error=" << rms << endl;
//stereo rectify
cout << "stereo rectify..." << endl;
stereoRectify(intrMatFirst, distCoeffsFirst, intrMatSec, distCoffesSec, imageSize, R, T, RFirst,
RSec, PFirst, PSec, Q, CALIB_ZERO_DISPARITY, -1, imageSize, &validRoi[0], &validRoi[1]);
cout << "Q" << Q << endl;
cout << "P1" << PFirst << endl;
cout << "P2" << PSec << endl;
//read pictures for 3d-reconstruction
if (fs.isOpened())
{
cout << "in";
fs << "R" << R << "T" << T << "R1" << RFirst << "R2" << RSec << "P1" << PFirst << "P2" << PSec << "Q" << Q;
fs.release();
}
namedWindow("canvas", 1);
cout << "read the picture for 3d-reconstruction..."<<endl;;
Mat canvas(imageSize.height, imageSize.width * 2, CV_8UC3), viewLeft, viewRight;
Mat canLeft = canvas(Rect(0, 0, imageSize.width, imageSize.height));
Mat canRight = canvas(Rect(imageSize.width, 0, imageSize.width, imageSize.height));
viewLeft = imread(imageList[6], 1);//cameraIdFirst
viewRight = imread(imageList[7], 1); //cameraIdSec
cout<<"Choose: "<<imageList[6]<<" "<<imageList[7]<<endl;
viewLeft.copyTo(canLeft);
viewRight.copyTo(canRight);
cout << "done" << endl;
imshow("canvas", canvas);
waitKey(1500); //必須要加waitKey ,否則可能存在無法顯示影像問題
//stereoRectify
Mat rmapFirst[2], rmapSec[2], rviewFirst, rviewSec;
initUndistortRectifyMap(intrMatFirst, distCoeffsFirst, RFirst, PFirst,
imageSize, CV_16SC2, rmapFirst[0], rmapFirst[1]);//CV_16SC2
initUndistortRectifyMap(intrMatSec, distCoffesSec, RSec, PSec,//CV_16SC2
imageSize, CV_16SC2, rmapSec[0], rmapSec[1]);
remap(viewLeft, rviewFirst, rmapFirst[0], rmapFirst[1], INTER_LINEAR);
imshow("remap", rviewFirst);
waitKey(2000);
remap(viewRight, rviewSec, rmapSec[0], rmapSec[1], INTER_LINEAR);
rviewFirst.copyTo(canLeft);
rviewSec.copyTo(canRight);
//rectangle(canLeft, validRoi[0], Scalar(255, 0, 0), 3, 8);
//rectangle(canRight, validRoi[1], Scalar(255, 0, 0), 3, 8);
Mat before_rectify = imread("/home/cxm-irene/檔案/Two-eye/Image-Collect/Picture/thumbnail_3.jpg");
for (int j = 0; j <= canvas.rows; j += 16) //畫綠線
line(canvas, Point(0, j), Point(canvas.cols, j), Scalar(0, 255, 0), 1, 8);
for (int j = 0; j <= canvas.rows; j += 16) //畫綠線
line(before_rectify, Point(0, j), Point(canvas.cols, j), Scalar(0, 255, 0), 1, 8);
cout << "stereo rectify done" << endl;
imshow("Before", before_rectify); //顯示畫綠線的校正后影像
imshow("After", canvas); //顯示畫綠線的校正前影像
waitKey(400000);//必須要加waitKey ,否則可能存在無法顯示影像問題
//官方解釋 http://masikkk.com/article/OpenCV-imshow-waitkey/
/* http://masikkk.com/article/OpenCV-imshow-waitkey/
A common mistake for OpenCV newcomers is to call cv::imshow() in a loop through video frames,
without following up each draw with cv::waitKey(30).In this case, nothing appears on screen,
because highgui is never given time to process the draw requests from cv::imshow().
*/
return 0;
}
#endif
原始碼來源于大佬
(這里要注意根據自己列印的棋盤格子更改引數,可以用尺子量一量格子邊長)
#define w 9 //棋盤格寬的黑白交叉點個數
#define h 6 //棋盤格高的黑白交叉點個數
const float chessboardSquareSize = 26.0f; //每個棋盤格方塊的邊長 單位 為 mm
保存退出,并運行(g++ test.cpp -o test pkg-config --cflags --libs opencv )括號里的都要運行,如果出錯可能是我手打的打錯了,詳情可以參考opencv檔案的編譯
編譯完成后會生產一個名為test的檔案,在該路徑下,運行./test即可運行,
第三步
這里如果直接運行肯定報錯,哈哈哈,因為我們沒有把剛剛拍的照片的路徑宣告,所以我們要寫應該xml檔案來告訴他我的圖片在哪里,方法:
將剛剛的images檔案夾拷貝到test1檔案夾下,然后在test檔案夾下新建stereo_calibration.xml檔案點擊打開它,在里面將我們剛剛拍的照片的路徑寫在里面,可以參考下面的代碼:

<?xml version="1.0"?>
<opencv_storage>
<imagelist>
"./images/l/left0.jpg"
"./images/r/right0.jpg"
"./images/l/left1.jpg"
"./images/r/right1.jpg"
"./images/l/left2.jpg"
"./images/r/right2.jpg"
"./images/l/left3.jpg"
"./images/r/right3.jpg"
"./images/l/left4.jpg"
"./images/r/right4.jpg"
"./images/l/left5.jpg"
"./images/r/right5.jpg"
"./images/l/left6.jpg"
"./images/r/right6.jpg"
"./images/l/left7.jpg"
"./images/r/right7.jpg"
"./images/l/left8.jpg"
"./images/r/right8.jpg"
"./images/l/left9.jpg"
"./images/r/right9.jpg"
"./images/l/left10.jpg"
"./images/r/right10.jpg"
"./images/l/left11.jpg"
"./images/r/right11.jpg"
"./images/l/left12.jpg"
"./images/r/right12.jpg"
"./images/l/left13.jpg"
"./images/r/right13.jpg"
"./images/l/left14.jpg"
"./images/r/right14.jpg"
"./images/l/left15.jpg"
"./images/r/right15.jpg"
"./images/l/left17.jpg"
"./images/r/right17.jpg"
"./images/l/left18.jpg"
"./images/r/right18.jpg"
"./images/l/left19.jpg"
"./images/r/right19.jpg"
</imagelist>
</opencv_storage>
./的意思是在當前路徑下,即test1檔案下,然后images下的l或者r檔案下的照片,這里的話,我覺的挺好理解的,就是把剛剛的照片的路徑依次寫進xml檔案就行了,要注意的是:照片路徑要先從左照片然后右照片,依次下去排列,(細心的讀者可能會發現為什么我這個xml里面沒有照片16,哈哈哈,好眼力,這個我們后面再說)
一般情況下保存退出再運行./test,經過一系列的運算,就沒問題了,等運行完成會在test1檔案夾下生成一個intrinsics.yml檔案,里面存放的就是我們要的標定資料,
如果出錯或者生產的檔案里面沒有資料那么,問題肯定在xml檔案里,這里的xml檔案非常重要,請認真對待,順序非常重要,左右照片都要安裝先左再右的順序排列,所以先檢查xml檔案看是否安裝要求填寫,
如果運行沒問題,但是生產的檔案就是沒有資料,那么應該是這個問題:
第一種(左右照片搞反)如圖

從0開始到19,強迫癥患者可以看到第12是一張左照片,這是因為xml檔案里的第12個路徑我搞反了,
在xml把第12的照片路徑左右轉回來就好了,
第二種
左右攝像頭的有效棋盤張數不一致,這里我左有效是19張而右有效是20張,解決方法是將報wrong的上一張左右照片路徑都刪掉,這就是我前面之所以把xml里面的第16路徑刪掉的原因,
其他問題我暫時沒遇到過,
當成功之后打開生產的yml檔案,可以看到


ok!大功告成,這個鬼玩意搞了我3天,這3天也發生了很多事,雖然很難受但是也得接受,加油騷年!
資料通過百度云盤方式分享,需要的自取:
鏈接:https://pan.baidu.com/s/1HheeX-zTiluejYQYpi_Ouw
提取碼:yyds
3.結尾
文章創作不易,給個贊,點個關注,收藏唄!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292598.html
標籤:其他
上一篇:Pytorch—SSD模型訓練—步驟一:訓練前的了解
下一篇:keras神經網路水體預測
