用OpenCV画出时钟并动态同步系统时间
程序员文章站
2022-03-19 20:20:43
过程大致分为两步:第一步,画出时钟;第二步,同步系统时间。其中第一步是跟opencv相关,第二步不太了解,不作过多记录。
一个简单的时钟就是线段和圆的组合。包括最外围的圆,60根...
过程大致分为两步:第一步,画出时钟;第二步,同步系统时间。其中第一步是跟opencv相关,第二步不太了解,不作过多记录。
一个简单的时钟就是线段和圆的组合。包括最外围的圆,60根刻度线(其中12根整点刻度线)和时针分针秒针三根长的线段。
画圆最简单:创建一个长宽相等的图片,以图片中心为中心,以一半的图片长宽为半径画圆即可。
画线段就相对麻烦许多,因为一共要画60+12+3=75根线段。opencv的画线函数line()需要知道线段的起始点,而我们需要画的75根直线的起始点各不相同,我们需要一一计算出来。如果我们的圆是以我们数学上的笛卡尔坐标系的原点为中心,以r为半径画的圆,大概就是下面这样:
已知角度α的情况下我们可以很容易算到P点的坐标:
x = r * cos(α)
y = r * sin(α)
但是我们知道opencv中图像的坐标原点在左上角,那么我们可以变化下坐标系,得到下面的公式:
x = r + r * cos(α)
y = r + r * sin(α)
一分钟60秒,秒针刻度就是把外围的圆60等分,每两个刻度的夹角是6度。同理整点刻度就是把圆12等分,夹角为30度。
我们可以用半径相差不大的一大一小两个圆来算出每一根刻度线的起始点坐标,然后就可以用line()画出它们了。整点刻度同理。
画出刻度线之后获取系统时间,然后根据时间实时画出时针分针秒针。这一部分不太了解,就不多说了。代码如下:
#include #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc_c.h" #include "opencv2/imgproc/imgproc.hpp" #include #include #include //#include #include #include using namespace std; using namespace cv; int main() { Mat clk(640, 640, CV_8UC3,Scalar(180,120,50)); //Mat to store clock image Mat back_up(640, 640, CV_8UC3, Scalar(180, 120, 50)); //Mat to store backup image Point cent(clk.rows/2, clk.cols/2); Point perim(clk.cols/2, 0); int rad = clk.cols / 2; float sec_angle = 270; float min_angle = 330; float hour_angle = 210; //画秒针刻度 vectorpt1,pt2; for (int i = 0; i < 60; i++) { int x1 = cent.x + rad*cos(i * 6 * CV_PI / 180.0); int y1 = cent.y + rad*sin(i * 6 * CV_PI / 180.0); pt1.push_back(Point(x1, y1)); int x2 = cent.x + (rad - 20)*cos(i * 6 * CV_PI / 180.0); int y2 = cent.y + (rad - 20)*sin(i * 6 * CV_PI / 180.0); pt2.push_back(Point(x2, y2)); line(clk, pt1[i], pt2[i], Scalar(0, 255, 0, 0), 1.5, CV_AA, 0); } //画整点刻度 vector pt3,pt4; for (int i = 0; i < 12; i++) { int x3 = cent.x + (rad - 40)*cos(i * 30 * CV_PI / 180.0); int y3 = cent.y + (rad - 40)*sin(i * 30 * CV_PI / 180.0); pt3.push_back(Point(x3, y3)); line(clk, pt1[(i*5)], pt3[i], Scalar(0, 255, 0, 0), 5, CV_AA, 0); } //画最外围的圆和圆心的三针连接点 circle(clk, cent, rad, Scalar(50, 50, 255, 0), 6, CV_AA, 0); //Dreaw outercircle of clock circle(clk, cent, 2, Scalar(0, 255, 0, 0), 5, CV_AA, 0); //Draw inner circle back_up = clk.clone(); // Clone to backup image time_t rawtime; struct tm * timeinfo; float second; float minute; float hour; float millisec; struct timeb tmb; while (1){ //获取本地时间 ftime(&tmb); rawtime = tmb.time; timeinfo = localtime (&rawtime); second = timeinfo->tm_sec; minute = timeinfo->tm_min; hour = timeinfo->tm_hour; millisec = tmb.millitm; second = second + millisec / 1000; sec_angle = (second * 6) + 270; //Convert second to angle minute = minute + second / 60; min_angle = minute * 6 + 270; //Conver minute to angle if (hour>12)hour = hour - 12; hour_angle = (hour * 30) + (minute*.5) + 270; //Conver hour to angle if (sec_angle>360)sec_angle = sec_angle - 360; if (min_angle>360)min_angle = min_angle - 360; if (hour_angle>360)hour_angle = hour_angle - 360; //画秒针 perim.x = (int)cvRound(cent.x + (rad - 5) * cos(sec_angle * CV_PI / 180.0)); perim.y = (int)cvRound(cent.y + (rad - 5) * sin(sec_angle * CV_PI / 180.0)); line(clk, cent, perim, Scalar(0, 255, 255, 0), 1.5, CV_AA, 0); //画分针 perim.x = (int)cvRound(cent.x + (rad - 30) * cos(min_angle * CV_PI / 180.0)); perim.y = (int)cvRound(cent.y + (rad - 30) * sin(min_angle * CV_PI / 180.0)); line(clk, cent, perim, Scalar(0, 255, 255, 0), 4, CV_AA, 0); //画时针 perim.x = (int)cvRound(cent.x + (rad - 100) * cos(hour_angle * CV_PI / 180.0)); perim.y = (int)cvRound(cent.y + (rad - 100) * sin(hour_angle * CV_PI / 180.0)); line(clk, cent, perim, Scalar(0, 255, 255, 0), 12, CV_AA, 0); imshow("Clock", clk); //Show result in a window clk.setTo(0); // set clk image to zero for next drawing clk = back_up.clone(); // Clone the previously drawned markings from back-up image char c = waitKey(999); // 这里如果参数为10,则看到的是秒针连续地转动;如果是1000,则效果是秒针一秒一秒地跳动 if (c == 27)break; } return 0; }