使用usb相机以及内参标定相关
使用opencv标定
参考链接
opencv4.0 中文文档 https://apachecn.github.io/opencv-doc-zh/#/docs/4.0.0/7.1-tutorial_py_calibration 使用的是python版本
原文档https://docs.opencv.org/4.x/d4/d94/tutorial_camera_calibration.html
标定
棋盘https://docs.opencv.org/4.x/pattern.png
程序
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
// Defining the dimensions of checkerboard
// 定义棋盘格的尺寸
int CHECKERBOARD[2] {6,9};
int main()
{
// Creating vector to store vectors of 3D points for each checkerboard image
// 创建矢量以存储每个棋盘图像的三维点矢量
std::vector<std::vector<cv::Point3f> > objpoints;
// Creating vector to store vectors of 2D points for each checkerboard image
// 创建矢量以存储每个棋盘图像的二维点矢量
std::vector<std::vector<cv::Point2f> > imgpoints;
// Defining the world coordinates for 3D points
// 为三维点定义世界坐标系
std::vector<cv::Point3f> objp;
for (int i{ 0 }; i < CHECKERBOARD[1]; i++)
{
for (int j{ 0 }; j < CHECKERBOARD[0]; j++)
{
objp.push_back(cv::Point3f(j, i, 0));
}
}
// Extracting path of individual image stored in a given directory
// 提取存储在给定目录中的单个图像的路径
std::vector<cv::String> images;
// Path of the folder containing checkerboard images
// 包含棋盘图像的文件夹的路径
std::string path = "../images/CameraCalibration/*.jpg";
// 使用glob函数读取所有图像的路径
cv::glob(path, images);
cout << images[0] << endl;
cv::Mat frame, gray;
// vector to store the pixel coordinates of detected checker board corners
// 存储检测到的棋盘转角像素坐标的矢量
std::vector<cv::Point2f> corner_pts;
bool success;
frame = cv::imread(images[0]);
cv::cvtColor(frame,gray,cv::COLOR_BGR2GRAY);
// Looping over all the images in the directory
// 循环读取图像
for (int i{ 0 }; i < images.size(); i++)
{
frame = cv::imread(images[i]);
if (frame.empty())
{
continue;
}
if (i == 40)
{
int b = 1;
}
cout << "the current image is " << i << "th" << endl;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); // COLOR_BGR2GRAY 从BGR 转换到灰度图
cv::resize(gray, gray, cv::Size(), 0.125, 0.125, cv::INTER_LINEAR);
cv::imshow("gray", gray);
cv::waitKey(1);
// Finding checker board corners
// 寻找角点
// If desired number of corners are found in the image then success = true
// 如果在图像中找到所需数量的角,则success = true
// opencv4以下版本,flag参数为CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE
success = cv::findChessboardCorners(gray, cv::Size(CHECKERBOARD[0], CHECKERBOARD[1]), corner_pts, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);
/*
* If desired number of corner are detected,
* we refine the pixel coordinates and display
* them on the images of checker board
*/
// 如果检测到所需数量的角点,我们将细化像素坐标并将其显示在棋盘图像上
if (success)
{
// 如果是OpenCV4以下版本,第一个参数为CV_TERMCRIT_EPS | CV_TERMCRIT_ITER
cv::TermCriteria criteria(TermCriteria::EPS | TermCriteria::Type::MAX_ITER, 30, 0.001);
// refining pixel coordinates for given 2d points.
// 为给定的二维点细化像素坐标
cv::cornerSubPix(gray, corner_pts, cv::Size(11, 11), cv::Size(-1, -1), criteria);
// Displaying the detected corner points on the checker board
// 在棋盘上显示检测到的角点
cv::drawChessboardCorners(frame, cv::Size(CHECKERBOARD[0], CHECKERBOARD[1]), corner_pts, success);
objpoints.push_back(objp);
imgpoints.push_back(corner_pts);
}
//cv::imshow("Image", frame);
//cv::waitKey(0);
}
cv::destroyAllWindows();
cv::Mat cameraMatrix, distCoeffs, R, T;
/*
* Performing camera calibration by
* passing the value of known 3D points (objpoints)
* and corresponding pixel coordinates of the
* detected corners (imgpoints)
*/
// 通过传递已知3D点(objpoints)的值和检测到的角点(imgpoints)的相应像素坐标来执行相机校准
cv::calibrateCamera(objpoints, imgpoints, cv::Size(gray.rows, gray.cols), cameraMatrix, distCoeffs, R, T);
// 内参矩阵
std::cout << "cameraMatrix : " << cameraMatrix << std::endl;
// 透镜畸变系数
std::cout << "distCoeffs : " << distCoeffs << std::endl;
// rvecs
std::cout << "Rotation vector : " << R << std::endl;
// tvecs
std::cout << "Translation vector : " << T << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(CameraCalibration)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(LIBRARY_OUTPUT_PATH ${PROJECT_NAME_DIR}/lib)
find_package(OpenCV 4.0 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIR})
include_directories(${PROJECT_NAME_DIR}/include)
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
add_executable(CameraCalibration src/camera_calibration.cpp)
target_link_libraries(CameraCalibration ${OpenCV_LIBS})
结果
报错,
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.2.0) ../modules/calib3d/src/calibration.cpp:3681: error: (-215:Assertion failed) nimages > 0 in function 'calibrateCameraRO'
不知道为啥,
猜测是因为ipone拍摄的相机像素太高,减小尺寸试试
在for循环中加入
cv::resize(gray, gray, cv::Size(), 0.125, 0.125, cv::INTER_LINEAR);
可以出结果了
使用usb摄像头,并标定
安装usb_cam
ubuntu20.04
ros版本 noetic
-
查看usb连接设备,显示有camera
lsusb
-
安装usb_cam包
sudo apt-get install ros-noetic-usb-cam
-
安装image-view
sudo apt-get install ros-noetic-image-view
-
启动
roscore
-
运行launch文件, 启动 usb_cam_node 节点
roslaunch usb_cam usb_cam-test.launch
在rviz中显示
- 启动
rviz
- 点击 Add , 添加
image
不是camera
相机标定
本文使用ros链接https://wiki.ros.org/camera_calibration
opencv链接https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html
校准使用棋盘的内部顶点,因此“10x7”棋盘使用内部顶点参数“9x6”
-
查看话题
rostopic list
,, 打印话题信息rostopic echo
-
新开终端
rosrun camera_calibration cameracalibrator.py --size 9x6 --square 0.24 image:=/usb_cam/image_raw camera:=/head_camera --no-service-check
-
参数
–size 9x6 为棋盘内部角点的个数,方格几列几行(需要减1),比如我的标定板方格是10X8,则siez为9x6 –square 0.24为每个棋盘格的边长 image:=/usb_cam/image_raw 为当前订阅的图像来自名为/usb_cam/image_raw的topic camera:=/head_camera为摄像机名
-
-
移动标定板
- In order to get a good calibration you will need to move the checkerboard around in the camera frame such that:
- checkerboard on the camera's left, right, top and bottom of field of view
- X bar - left/right in field of view
- Y bar - top/bottom in field of view
- Size bar - toward/away and tilt from the camera
- checkerboard filling the whole field of view
- checkerboard tilted to the left, right, top and bottom (Skew)
- checkerboard on the camera's left, right, top and bottom of field of view
- 直到条形变为绿色。当calibration按钮亮起时,代表已经有足够的数据进行摄像头的标定,此时请按下calibration并等待一分钟左右,标定界面会变成灰色,无法进行操作,属于正常情况。
- In order to get a good calibration you will need to move the checkerboard around in the camera frame such that:
-
标定结果将在终端显示,参数如下
camera matrix:摄像头的内部参数矩阵 distortion:畸变系数矩阵 rectification:矫正矩阵,一般为单位阵 projection:外部世界坐标到像平面的投影矩阵
-
点击save按钮
-
如果对结果满意,click COMMIT to send the calibration parameters to the camera for permanent storage
Simply loading a calibration file does not rectify the image. For rectification, use the image_proc package.
运行后警告
Camera calibration file
[ WARN] [1719882910.689573001]: Camera calibration file /home/wenming/.ros/camera_info/head_camera.yaml not found.
参考http://t.csdnimg.cn/E0XA4,ROS下采用camera_calibration进行单目相机标定
[ WARN] [1719882910.877910518]: unknown control 'white_balance_temperature_auto'
[ WARN] [1719882910.884822220]: unknown control 'focus_auto'
https://blog.csdn.net/newbeixue/article/details/102796474
说是将下面launch文件中关闭了
<param name="autoexposure" value="false" /> <param name="auto_whitebalance" value="false" /> <param name="auto_focus" value="false" /> <param name="auto_brigthness" value="false" />
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具