环境准备
vs2015+opencv4.10安装与配置
相机标定
棋盘格图片
可以自己生成,然后打印到A4纸上。(也可以去TB买一块,平价买亚克力板的,不反光买氧化铝材质,高精度买陶瓷的)
int generateCalibrationPicture()
{
//Mat frame = imread("3A4.bmp"); // cols*rows = 630*891
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)
{
// bgr
data[i / nc*nc + 0] = 255;
data[i / nc*nc + 1] = 255;
data[i / nc*nc + 2] = 255;
}
}
}
imshow("test", frame);
//imwrite("3.bmp", frame);
waitKey(0);
return 0;
}
实时显示相机的画面
准备一个相机,我的是usb相机(罗技100多的)。
int displayCameraRealTime()
{
//1.从摄像头读入视频
VideoCapture capture(0);
if (!capture.isOpened()) {
std::cout << "无法开启摄像头!" << std::endl;
return -1;
}
//2.循环显示每一帧
while (1)
{
Mat cam;
capture >> cam;//获取当前帧图像
namedWindow("实时相机画面", WINDOW_AUTOSIZE);
imshow("实时相机画面", cam);//显示当前帧图像
//imwrite(to_string(i) + ".png", cam);
waitKey(20);//延时20ms
}
}
效果如下图:
在线标定
把打印的棋盘格固定在板子上
int calibrateCameraRealTime(int numBoards, cv::Size boardSize, float squareSize = 1, int delay = 50, bool flipHorizontal = false);
实时显示相机画面,按键保存能检测到角点的 棋盘格图片
int saveChessboardImages(cv::Size boardSize, string savePath)
{
//1.从摄像头读入视频
VideoCapture capture(0);
if (!capture.isOpened()) {
std::cout << "无法开启摄像头!" << std::endl;
return -1;
}
if (savePath != "./")
{
myMkdir(savePath);
}
//2.循环显示每一帧
while (1) {
Mat image0, image;
capture >> image0;
// 将图像复制到image
image0.copyTo(image);
// 查找标定板(不对称圆网格板)
vector<Point2f> corners;
//bool found = findCirclesGrid(image, boardSize, corners, CALIB_CB_ASYMMETRIC_GRID);
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) { // 用户按下了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 = " ");
矫正效果貌似不明显
完整工程地址
到此这篇关于OpenCV实现相机标定示例详解的文章就介绍到这了,更多相关OpenCV 相机标定内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!