OpenCV相机标定
环境准备
vs2015+opencv4.10安装与配置
https://blog.csdn.net/weixin_44718794/article/details/104775010
找不到opencv_world410d.dll,无法执行代码,重新安装程序可能会解决此问题
https://blog.csdn.net/Feeryman_Lee/article/details/106114718
相机标定
棋盘格图片
可以自己生成,然后打印到A4纸上。(也可以去TB买一块,平价买亚克力板的,不反光买氧化铝材质,高精度买陶瓷的)
int generateCalibrationPicture()
{
Mat frame(1600, 2580, CV_8UC3, Scalar(0, 0, 0));
int nc = frame.channels();
int nWidthOfROI = 320;
for (int j = 10; j<frame.rows - 10; j++)
{
uchar* data = frame.ptr<uchar>(j);
for (int i = 10; i<(frame.cols - 10)*nc; i += nc)
{
if ((i / nc / nWidthOfROI + j / nWidthOfROI) % 2)
{
data[i / nc*nc + 0] = 255;
data[i / nc*nc + 1] = 255;
data[i / nc*nc + 2] = 255;
}
}
}
imshow("test", frame);
waitKey(0);
return 0;
}
实时显示相机的画面
准备一个相机,我的是usb相机(罗技100多的)。
int displayCameraRealTime()
{
VideoCapture capture(0);
if (!capture.isOpened()) {
std::cout << "无法开启摄像头!" << std::endl;
return -1;
}
while (1)
{
Mat cam;
capture >> cam;
namedWindow("实时相机画面", WINDOW_AUTOSIZE);
imshow("实时相机画面", cam);
waitKey(20);
}
}
效果如下图:
在线标定
把打印的棋盘格固定在板子上
int calibrateCameraRealTime(int numBoards, cv::Size boardSize, float squareSize = 1, int delay = 50, bool flipHorizontal = false);
实时显示相机画面,按键保存能检测到角点的 棋盘格图片
int saveChessboardImages(cv::Size boardSize, string savePath)
{
VideoCapture capture(0);
if (!capture.isOpened()) {
std::cout << "无法开启摄像头!" << std::endl;
return -1;
}
if (savePath != "./")
{
myMkdir(savePath);
}
while (1) {
Mat image0, image;
capture >> image0;
image0.copyTo(image);
vector<Point2f> corners;
bool found = findChessboardCorners(image, boardSize, corners, CALIB_CB_FAST_CHECK);
drawChessboardCorners(image, boardSize, corners, found);
int action = waitKey(30) & 255;
if (action == ACTION_SPACE) {
if (found) {
string imgFileName = savePath + getCurrentTime() + ".png";
imwrite(imgFileName, image0);
cout << imgFileName << " saved" << endl;
}
else {
printf("%s\n", "未检测到角点");
}
}
else if (action == ACTION_ESC) {
break;
}
cv::imshow("Calibration", image);
}
cv::destroyAllWindows();
return 1;
}
离线标定
int calibrateCameraOffLine(string imagePath, const Size boardSize, float squareSize = 1);
畸变矫正
int undistortRectifyImage(string paraPath, string imagePath = " ");
矫正效果貌似不明显 完整工程地址: https://gitee.com/xuruilong111/camera-calibration-open-cv.git
|