目录
通过OpenCV + C++绘制非对称圆点标定图案,注意,非对称圆点图案的维度中的一行实质是一组非对称圆点行(两行),其标定图案示意如下图所示:
原始代码
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
// 图像宽高
int width = 1400;
int height = 1000;
// 边框大小
int thicknum = 2;
// 标定图案两个维度的大小
int sqXnum = 11;
int sqYnum = 4; // 非对称圆点图案的一行实质是一组非对称圆点行
sqYnum = 2 * sqYnum;
// 根据输入的特征点数目,自适应计算圆点半径大小(此处预设两圆点圆心之间距离为4*半径)
int radius = min(width / (4 * sqXnum + 2), height / (4 * sqYnum + 2));
if (radius <= 0.01 * min(height, width)) {
cout << "警告:圆点过小,可能无法识别!" << endl;
}
int space = 4 * radius;
// 生成两个维度方向上的边缘空白
int x_st = (width - 2 * radius * (2 * sqXnum - 1)) / 2;
int y_st = (height - 2 * radius * (2 * sqYnum - 1)) / 2;
// 生成空白画布
Mat img(height + 2 * thicknum, width + 2 * thicknum, CV_8UC4, Scalar(255, 255, 255, 255));
// 生成起始点圆心坐标
int cir_x = x_st + radius + thicknum;
int cir_y = y_st + radius + thicknum;
// 用于做非对称的偏移
int Asym_offset = 0;
int y_count = 0;
// 绘制非对称圆点图案
for (int i = 0; i < img.rows; i++) {
if (y_count % 2 == 0)
Asym_offset = space / 2;
else
Asym_offset = 0;
for (int j = 0; j < img.cols; j++) {
// 绘制边框
if (i < thicknum || i >= thicknum + height || j < thicknum || j >= thicknum + width) {
img.at<Vec<uchar, 4>>(i, j) = Scalar(0, 0, 0, 255);
continue;
}
// 绘制圆点
if (cir_y >= img.rows - y_st - thicknum) {
continue;
}
if (i == cir_y && j == cir_x) {
// 绘制圆点,LINE_AA得到的边缘最为光滑
circle(img, Point(j, i), radius, cv::Scalar(0, 0, 0, 255), -1, LINE_AA);
cir_x += space;
}
if (cir_x >= img.cols - x_st - thicknum) {
cir_x = Asym_offset + x_st + radius + thicknum;
cir_y += space;
y_count++;
}
}
}
imwrite("asymmetric_dot_calib.png", img);
imshow("非对称圆点标定图案", img);
waitKey(0);
return 0 ;
}