使用opencv在图像上画带刻度线的十字线,以图像中心点为0点

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

cpp 复制代码
#include<opencv2\opencv.hpp>
//画十字标注线带刻度线,以图像中心点为0点
void DrawCross_new(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 = 6;
    int LenghtLineSize = 12;
    double testsie = 0.35;
    int Linesize = 1;
    cv::Point CenterPoint = cv::Point(mat.cols / 2.0, mat.rows / 2.0);
    cv::Point Point{};

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


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


    }

    //画X右刻度线
    for (int i_right = 1; i_right < int(CenterPoint.x / dFactor) + 1; i_right++)
    {
        Point = cv::Point(CenterPoint.x + dFactor * i_right, CenterPoint.y);
        if (i_right % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x, Point.y - LenghtLineSize), color, Linesize);//横线
            cv::putText(mat, std::to_string(i_right), cv::Point(Point.x - dFactor, Point.y - LenghtLineSize - 5), cv::FONT_HERSHEY_SIMPLEX, testsie, color, 1);
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x, Point.y - ShortLineSize), color, Linesize);//横线
        }


    }

    //画Y上刻度线

    for (int i_up = 1; i_up < int(CenterPoint.y / dFactor) + 1; i_up++)
    {
        Point = cv::Point(CenterPoint.x, CenterPoint.y - dFactor * i_up);
        if (i_up % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x - LenghtLineSize, Point.y), color, Linesize);//横线
            cv::putText(mat, std::to_string(i_up), cv::Point(Point.x - LenghtLineSize * 3, Point.y + dFactor / 2.0), cv::FONT_HERSHEY_SIMPLEX, testsie, color, 1);
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x - ShortLineSize, Point.y), color, Linesize);//横线
        }


    }

    //画Y下刻度线
    for (int i_down = 1; i_down < int(CenterPoint.y / dFactor) + 1; i_down++)
    {
        Point = cv::Point(CenterPoint.x, CenterPoint.y + dFactor * i_down);
        if (i_down % 5 == 0) {
            //画长线
            cv::line(mat, Point, cv::Point(Point.x - LenghtLineSize, Point.y), color, Linesize);//横线
            cv::putText(mat, std::to_string(i_down), cv::Point(Point.x - LenghtLineSize * 3, Point.y + dFactor / 2.0), cv::FONT_HERSHEY_SIMPLEX, testsie, color, 1);
        }
        else
        {
            //画短线
            cv::line(mat, Point, cv::Point(Point.x - ShortLineSize, Point.y), color, Linesize);//横线
        }


    }
}
//测试程序
int main() {
	// 创建一个空白图像
	int width = 800;
	int height = 600;
	cv::Mat image = cv::Mat::zeros(height, width, CV_8UC3);
	int tickLength = 5;  
	// 绘制带刻度线的十字线
	DrawCross_new(image, tickLength);
	// 显示图像
	cv::imshow("Crosshair with Ticks", image);
	cv::waitKey(0);

	return 0;
}

绘画的结果如下图所示:

相关推荐
嵌入式大圣4 分钟前
单片机结合OpenCV
单片机·嵌入式硬件·opencv
xrgs_shz11 分钟前
MATLAB读入不同类型图像并显示图像和相关信息
图像处理·计算机视觉·matlab
WeeJot嵌入式22 分钟前
卷积神经网络:深度学习中的图像识别利器
人工智能
脆皮泡泡32 分钟前
Ultiverse 和web3新玩法?AI和GameFi的结合是怎样
人工智能·web3
机器人虎哥35 分钟前
【8210A-TX2】Ubuntu18.04 + ROS_ Melodic + TM-16多线激光 雷达评测
人工智能·机器学习
码银42 分钟前
冲破AI 浪潮冲击下的 迷茫与焦虑
人工智能
用户37791362947551 小时前
【循环神经网络】只会Python,也能让AI写出周杰伦风格的歌词
人工智能·算法
何大春1 小时前
【弱监督语义分割】Self-supervised Image-specific Prototype Exploration for WSSS 论文阅读
论文阅读·人工智能·python·深度学习·论文笔记·原型模式
uncle_ll1 小时前
PyTorch图像预处理:计算均值和方差以实现标准化
图像处理·人工智能·pytorch·均值算法·标准化
宋138102797201 小时前
Manus Xsens Metagloves虚拟现实手套
人工智能·机器人·vr·动作捕捉