【OpenCV 学习之路】(4)画出时钟并动态同步系统时间之一

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u011897411/article/details/80325858

看到了 @冰不语 有一个时钟例子,看起来挺酷炫的,马上实现了下。
先画一个总体的轮廓。
先看效果:
总体

Talk is cheap,show you the code.

#include<opencv2\core\core.hpp>  
#include<opencv2\highgui\highgui.hpp>  
#include<opencv2\imgproc\imgproc.hpp>  
using namespace cv;
int main()
{
    Mat clock_background(500, 500, CV_8UC3, Scalar::all(0));//创一个背景
    int radius = clock_background.cols / 2;//设半径
    Point center(clock_background.cols / 2, clock_background.rows / 2);//设圆心
    circle(clock_background, center, radius, Scalar(255, 100, 0), 1, 8, 0);//画时钟外轮廓
    circle(clock_background, center, 5, Scalar(0, 0, 255), -1, 8, 0);//画圆心
    Point second_begin, second_end;//设刻度的起点,终点
    int scale_long = 10;//刻度的长度
    int scale_width = 2;//刻度的宽度
    //画一个背景
    for (int second_Scale = 0; second_Scale < 60; second_Scale++)
    {
        if (second_Scale % 5 == 0)//每五个刻度就变长度,也就是整点1,2,3,···,12这些数字对应的刻度长一点
        {
            scale_long = 20;
            scale_width = 5;
        }
        else
        {
            scale_long = 10;
            scale_width = 2;
        }
        second_begin.x = center.x + radius * cos(6 * second_Scale* CV_PI / 180);//刻度的起点x坐标赋值
        second_begin.y = center.y + radius * sin(6 * second_Scale* CV_PI / 180);//刻度的起点y坐标赋值
        second_end.x = center.x + (radius - scale_long) * cos(6 * second_Scale* CV_PI / 180);//刻度的终点x坐标赋值
        second_end.y = center.y + (radius - scale_long) * sin(6 * second_Scale* CV_PI / 180);//刻度的终点y坐标赋值
        line(clock_background, second_begin, second_end, Scalar(50, 205, 50), scale_width);//连接起点终点
    }
    imshow("clock", clock_background);
    waitKey(0);
    return 0;    
}

我画时钟第一步就是先把整体的轮廓画出来。
步骤:

  • 第一步:画一个外轮廓,这个很简单,确定圆心,半径,就可以画出。
  • 第二步:把刻度给画出来,这一步就相对有点思考,不过其实也很简单。
    首先,明确OpenCV中的坐标原点是在图片的左上角,那么,我们画的圆的原点也就相当于(center.x,center.y)
    然后如果找圆上一点,怎么找呢?是不是可以通过
    KaTeX parse error: Expected group after '_' at position 46: …cond_Scale * CV_̲_PI / 180)
    因为每个刻度相隔6°,所以要找每隔6°的x坐标,y坐标同理。
    至此,总体画出来了。

猜你喜欢

转载自blog.csdn.net/u011897411/article/details/80325858
今日推荐