C++ OpenCV绘制对称圆点标定图案

目录

  • 原始代码
  • 效果图
  • 参考引用

通过OpenCV + C++绘制对称型圆点标定图案

原始代码

#include 
#include 
#include 

using namespace cv;
using namespace std;


int main()
{
     
	// 图像宽高
	int width = 1400;
	int height = 1000;
	
	// 边框大小
	int thicknum = 2;
	// 两个维度的特征圆点数目
	int sqXnum = 14;
	int sqYnum = 10;

	// 根据输入的特征点数目,自适应计算圆点半径大小(此处预设两圆点圆心之间距离为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;
	for (int i = 0; i < img.rows; i++) {
     
		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 = x_st + radius + thicknum;
				cir_y += space;
			}
			
		}
	}
	imwrite("dot_calib.png", img);
	imshow("圆点标定图案", img);
	waitKey(0);
	return 0 ;
}

效果图

C++ OpenCV绘制对称圆点标定图案_第1张图片

参考引用

  1. opencv生成圆形标定版程序

你可能感兴趣的:(OpenCV,C++,opencv,c++)