使用OpenCV在图像上绘制带刻度线的对角线,可以通过以下步骤实现。我们将首先找到图像的中心点,然后绘制对角线线,并在这些线的适当位置绘制刻度线。以下是详细的C++代码示例:

void Draw_diagonal(cv::Mat& mat, double dFactor) {
    cv::Scalar color = cv::Scalar(0, 0, 255);

    if (mat.empty())
    {
        return;
    }
    int nSizeFace = cv::FONT_HERSHEY_PLAIN;
    int ShortLineSize = 5;
    int LenghtLineSize = 10;
    int Linesize = 1;
    cv::Point CenterPoint = cv::Point(mat.cols / 2.0, mat.rows / 2.0);
    cv::Point Point{};

    //画十字对角
    cv::line(mat, cv::Point(0, 0), cv::Point(mat.cols, mat.rows), color, 1);//竖线
    cv::line(mat, cv::Point(0, mat.rows), cv::Point(mat.cols, 0), color, 1);//横线

    double lenghtX = sqrt(pow(CenterPoint.x, 2) + pow(CenterPoint.y, 2));
    double dx = CenterPoint.x / lenghtX;
    double dy = CenterPoint.y / lenghtX;
    //std::cout << lenghtX << std::endl;
        //画左上刻度线
    for (int i_left = 1; i_left < int(lenghtX / dFactor) + 1; i_left++)
    {
        Point = cv::Point(CenterPoint.x - i_left * dFactor * dx, CenterPoint.y - i_left * dFactor * dy);
        if (i_left % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x + dy * LenghtLineSize, Point.y - dx * LenghtLineSize), color, Linesize);//横线
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x + dy * ShortLineSize, Point.y - dx * ShortLineSize), color, Linesize);//横线
        }


    }
    //画左下刻度线
    for (int i_left = 1; i_left < int(lenghtX / dFactor) + 1; i_left++)
    {
        Point = cv::Point(CenterPoint.x + i_left * dFactor * dx, CenterPoint.y + i_left * dFactor * dy);
        if (i_left % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x + dy * LenghtLineSize, Point.y - dx * LenghtLineSize), color, Linesize);//横线
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x + dy * ShortLineSize, Point.y - dx * ShortLineSize), color, Linesize);//横线
        }

    }

    //画右上
    for (int i_left = 1; i_left < int(lenghtX / dFactor) + 1; i_left++)
    {
        Point = cv::Point(CenterPoint.x + i_left * dFactor * dx, CenterPoint.y - i_left * dFactor * dy);
        if (i_left % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x - dy * LenghtLineSize, Point.y - dx * LenghtLineSize), color, Linesize);//横线
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x - dy * ShortLineSize, Point.y - dx * ShortLineSize), color, Linesize);//横线
        }


    }

    //画左下
    for (int i_left = 1; i_left < int(lenghtX / dFactor) + 1; i_left++)
    {
        Point = cv::Point(CenterPoint.x - i_left * dFactor * dx, CenterPoint.y + i_left * dFactor * dy);
        if (i_left % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x - dy * LenghtLineSize, Point.y - dx * LenghtLineSize), color, Linesize);//横线
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x - dy * ShortLineSize, Point.y - dx * ShortLineSize), color, Linesize);//横线
        }


    }


}
//测试程序
int main() {
	// 创建一个空白图像
	int width = 800;
	int height = 600;
	cv::Mat image = cv::Mat::zeros(height, width, CV_8UC3);
	int tickLength = 5;   // 刻度长度为10像素

	// 绘制带刻度线的对角线
	Draw_diagonal(image, tickLength);
	// 显示图像
	cv::imshow("Diagonal with Ticks", image);
	cv::waitKey(0);

	return 0;
}

绘画的结果如下图所示:

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部