OPENCV3.0 單目攝像頭標定(使用官方自帶的標定圖片)
版權宣告:本文為博主原創文章,未經博主允許不得轉載。
- // opencv_test.cpp : 定義控制檯應用程式的入口點。
- //
- #include "stdafx.h"
- #include <opencv2/opencv.hpp>
- #include <highgui.hpp>
- #include "cv.h"
- #include <cv.hpp>
- #include <iostream>
- using namespace std;
- using namespace cv;
- const int imageWidth = 640; //攝像頭的解析度
- const int imageHeight = 480;
- const int boardWidth = 9; //橫向的角點數目
- const int boardHeight = 6; //縱向的角點資料
- const int boardCorner = boardWidth * boardHeight; //總的角點資料
- const int frameNumber = 13; //相機標定時需要採用的影像幀數
- const int squareSize = 20; //標定板黑白格子的大小 單位mm
- const Size boardSize = Size(boardWidth, boardHeight); //
- Mat intrinsic; //相機內引數
- Mat distortion_coeff; //相機畸變引數
- vector<Mat> rvecs; //旋轉向量
- vector<Mat> tvecs; //平移向量
- vector<vector<Point2f>> corners; //各個影像找到的角點的集合 和objRealPoint 一一對應
- vector<vector<Point3f>> objRealPoint; //各副影像的角點的實際物理座標集合
- vector<Point2f> corner; //某一副影像找到的角點
- Mat rgbImage, grayImage;
- /*計算標定板上模組的實際物理座標*/
- void calRealPoint(vector<vector<Point3f>>& obj, int boardwidth,int boardheight, int imgNumber, int squaresize)
- {
- // Mat imgpoint(boardheight, boardwidth, CV_32FC3,Scalar(0,0,0));
- vector<Point3f> imgpoint;
- for (int rowIndex = 0; rowIndex < boardheight; rowIndex++)
- {
- for (int colIndex = 0; colIndex < boardwidth; colIndex++)
- {
- // imgpoint.at<Vec3f>(rowIndex, colIndex) = Vec3f(rowIndex * squaresize, colIndex*squaresize, 0);
- imgpoint.push_back(Point3f(rowIndex * squaresize, colIndex * squaresize, 0));
- }
- }
- for (int imgIndex = 0; imgIndex < imgNumber; imgIndex++)
- {
- obj.push_back(imgpoint);
- }
- }
- /*設定相機的初始引數 也可以不估計*/
- void guessCameraParam(void )
- {
- /*分配記憶體*/
- intrinsic.create(3, 3, CV_64FC1);
- distortion_coeff.create(5, 1, CV_64FC1);
- /*
- fx 0 cx
- 0 fy cy
- 0 0 1
- */
- intrinsic.at<double>(0,0) = 256.8093262; //fx
- intrinsic.at<double>(0, 2) = 160.2826538; //cx
- intrinsic.at<double>(1, 1) = 254.7511139; //fy
- intrinsic.at<double>(1, 2) = 127.6264572; //cy
- intrinsic.at<double>(0, 1) = 0;
- intrinsic.at<double>(1, 0) = 0;
- intrinsic.at<double>(2, 0) = 0;
- intrinsic.at<double>(2, 1) = 0;
- intrinsic.at<double>(2, 2) = 1;
- /*
- k1 k2 p1 p2 p3
- */
- distortion_coeff.at<double>(0, 0) = -0.193740; //k1
- distortion_coeff.at<double>(1, 0) = -0.378588; //k2
- distortion_coeff.at<double>(2, 0) = 0.028980; //p1
- distortion_coeff.at<double>(3, 0) = 0.008136; //p2
- distortion_coeff.at<double>(4, 0) = 0; //p3
- }
- void outputCameraParam(void )
- {
- /*儲存資料*/
- //cvSave("cameraMatrix.xml", &intrinsic);
- //cvSave("cameraDistoration.xml", &distortion_coeff);
- //cvSave("rotatoVector.xml", &rvecs);
- //cvSave("translationVector.xml", &tvecs);
- /*輸出資料*/
- cout << "fx :" << intrinsic.at<double>(0, 0) << endl << "fy :" << intrinsic.at<double>(1, 1) << endl;
- cout << "cx :" << intrinsic.at<double>(0, 2) << endl << "cy :" << intrinsic.at<double>(1, 2) << endl;
- cout << "k1 :" << distortion_coeff.at<double>(0, 0) << endl;
- cout << "k2 :" << distortion_coeff.at<double>(1, 0) << endl;
- cout << "p1 :" << distortion_coeff.at<double>(2, 0) << endl;
- cout << "p2 :" << distortion_coeff.at<double>(3, 0) << endl;
- cout << "p3 :" << distortion_coeff.at<double>(4, 0) << endl;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Mat img;
- int goodFrameCount = 0;
- namedWindow("chessboard");
- cout << "按Q退出 ..." << endl;
- while (goodFrameCount < frameNumber)
- {
- char filename[100];
- sprintf_s(filename,"image\\left%02d.jpg", goodFrameCount + 1);
- // cout << filename << endl;
- rgbImage = imread(filename, CV_LOAD_IMAGE_COLOR);
- cvtColor(rgbImage, grayImage, CV_BGR2GRAY);
- imshow("Camera", grayImage);
- bool isFind = findChessboardCorners(rgbImage, boardSize, corner,0);
- if (isFind == true) //所有角點都被找到 說明這幅影像是可行的
- {
- /*
- Size(5,5) 搜尋視窗的一半大小
- Size(-1,-1) 死區的一半尺寸
- TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1)迭代終止條件
- */
- cornerSubPix(grayImage, corner, Size(5,5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.1));
- drawChessboardCorners(rgbImage, boardSize, corner, isFind);
- imshow("chessboard", rgbImage);
- corners.push_back(corner);
- //string filename = "res\\image\\calibration";
- //filename += goodFrameCount + ".jpg";
- //cvSaveImage(filename.c_str(), &IplImage(rgbImage)); //把合格的圖片儲存起來
- goodFrameCount++;
- cout << "The image is good" << endl;
- }
- else
- {
- cout << "The image is bad please try again" << endl;
- }
- // cout << "Press any key to continue..." << endl;
- // waitKey(0);
- if (waitKey(10) == 'q')
- {
- break;
- }
- // imshow("chessboard", rgbImage);
- }
- /*
- 影像採集完畢 接下來開始攝像頭的校正
- calibrateCamera()
- 輸入引數 objectPoints 角點的實際物理座標
- imagePoints 角點的影像座標
- imageSize 影像的大小
- 輸出引數
- cameraMatrix 相機的內參矩陣
- distCoeffs 相機的畸變引數
- rvecs 旋轉向量(外引數)
- tvecs 平移向量(外引數)
- */
- /*設定實際初始引數 根據calibrateCamera來 如果flag = 0 也可以不進行設定*/
- guessCameraParam();
- cout << "guess successful" << endl;
- /*計算實際的校正點的三維座標*/
- calRealPoint(objRealPoint, boardWidth, boardHeight,frameNumber, squareSize);
- cout << "cal real successful" << endl;
- /*標定攝像頭*/
- calibrateCamera(objRealPoint, corners, Size(imageWidth, imageHeight), intrinsic, distortion_coeff, rvecs, tvecs, 0);
- cout << "calibration successful" << endl;
- /*儲存並輸出引數*/
- outputCameraParam();
- cout << "out successful" << endl;
- /*顯示畸變校正效果*/
- Mat cImage;
- undistort(rgbImage, cImage, intrinsic, distortion_coeff);
- imshow("Corret Image", cImage);
- cout << "Correct Image" << endl;
- cout << "Wait for Key" << endl;
- waitKey(0);
- system("pause");
- return 0;
- }
OPENCV3.0版本跟2.x版本是有一點差距的,這個程式在2.4.11版本里面跑不起來。
找了很久都沒有找到錯誤,主要是在calibrateCamera()函式的時候出錯。
也參考了官方的calibration例程,但還是找不到錯誤在什麼地方。
下面是3.0版本的程式碼 註釋都在程式碼裡面
相關文章
- OpenCV攝像頭標定(待修改)OpenCV
- 一次實踐:給自己的手機攝像頭進行相機標定
- 關於基於OPENCV攝像機標定的一點感受OpenCV
- 筆記本攝像頭怎麼開啟 筆記本設定攝像頭教程筆記
- win10 怎麼檢測攝像頭_win10怎麼設定攝像頭Win10
- Jetson AGX Xavier ROS下呼叫USB單目攝像頭ROS
- 《OpenCV設定和獲取攝像頭引數》OpenCV
- WebRTC從攝像頭獲取圖片傳入canvasWebCanvas
- OpenCV開發筆記(七十七):相機標定(二):透過棋盤標定計算相機內參矩陣矯正畸變攝像頭影像OpenCV筆記矩陣
- 單目標定:從理論到OpenCV實踐OpenCV
- flash呼叫攝像頭彈出設定框監聽
- win10找不到攝像頭怎麼辦_win10自帶攝像頭找不到如何解決Win10
- WPF呼叫攝像頭,對圖片相似度識別 1.0
- EclipseCDT標準庫標頭檔案設定Eclipse
- 使用思維導圖工具MindManager幫助實現目標設定
- Python基於opencv呼叫攝像頭獲取個人圖片PythonOpenCV
- OPENCV版本的單目標定示例程式碼(張正友)OpenCV
- win10如何開啟電腦攝像頭錄影_win10用電腦自帶攝像頭錄影的方法Win10
- 電腦釘釘攝像頭許可權在哪設定 電腦釘釘視訊會議攝像頭黑屏
- 設定一個怎樣的小目標
- 高速攝影機輔助的相機方位線上標定
- 攝像頭背後的應用–資訊圖
- 給玩家定一個小目標:關於數值卡牌遊戲中目標設定的一些思考遊戲
- 避免標頭檔案重複定義
- 使用點陣圖選單項——設定點陣圖型別標記 (轉)型別
- 如何有效設定OKR中目標? - RedditOKR
- html5呼叫攝像頭截圖HTML
- 如何使用PYTHON操作攝像頭Python
- Maui Blazor 使用攝像頭實現UIBlazor
- JavaCV的攝像頭實戰之四:抓圖Java
- 設定網頁圖片熱點連結以及座標值網頁
- 攝像頭操作指南
- pycharm設定python標頭檔案模版PyCharmPython
- Google如何設定目標與衡量成功Go
- MINIEYE首席科學家吳建鑫:用單目攝像頭實現自動駕駛的視覺感知自動駕駛視覺
- ToDesk勾上攝像頭會看到我嗎?如何關閉攝像頭
- mac CLion cmake 呼叫自己定義的標頭檔案Mac
- win10內建攝像頭為什麼無法使用 win10攝像頭無法使用的方法Win10