码迷,mamicode.com
首页 > 其他好文 > 详细

opencv函数制作的时钟模型

时间:2015-01-06 19:36:54      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

http://www.cnblogs.com/sytu/p/4192652.html

在秒针模型的基础上添加了分针和时针,并且添加了暂停控件和设置时间的功能。

  1 #include"cv.h"
  2 #include"highgui.h"
  3 int run = 1;
  4 void Stop_Run_onclick(int event,int x,int y,int flags,void* param)
  5 {
  6     switch (event)
  7     {
  8     case CV_EVENT_LBUTTONDOWN:{
  9                                   if (x<330 && x>170 && y<110 && y>80)
 10                                   {
 11                                       run = !run;
 12                                     }
 13     }
 14     default:
 15         break;
 16     }
 17 }
 18 void Drwan_button(IplImage* plane)
 19 {
 20     //画按钮
 21     cvRectangle(plane, cvPoint(170, 80), cvPoint(330, 110), cvScalar(100, 100, 100), 2);
 22     cvSetImageROI(plane, cvRect(170, 80, 160, 30));
 23     cvZero(plane);
 24     cvFloodFill(plane,cvPoint(0,0),cvScalarAll(100),cvScalarAll(0));
 25     cvResetImageROI(plane);
 26     if (run)
 27     cvPutText(plane, "StopTime", cvPoint(173, 105), &cvFont(2, 2), cvScalar(100, 0, 100));
 28     else
 29     {
 30         cvPutText(plane, "RunTime", cvPoint(173, 105), &cvFont(2, 2), cvScalar(100, 0, 100));
 31     }
 32 }
 33 double PIZHI(int count)
 34 {
 35     return 2*(1.0*count / 60.0 * 3.1415926);
 36 }
 37 int main()
 38 {
 39     IplImage* Plane = cvCreateImage(cvSize(500,500),8,3);
 40     cvAddS(Plane,cvScalar(100,0,200),Plane);
 41     cvCircle(Plane,cvPoint(250,250),100,cvScalar(0,180,0),2);
 42     cvPutText(Plane,"12",cvPoint(236,145),&cvFont(1,1),cvScalar(50,150,10));
 43     cvPutText(Plane, "3", cvPoint(355, 255), &cvFont(1, 1), cvScalar(50, 150, 10));
 44     cvPutText(Plane, "4", cvPoint(340, 305), &cvFont(1, 1), cvScalar(50, 150, 10));
 45     cvPutText(Plane, "5", cvPoint(312, 345), &cvFont(1, 1), cvScalar(50, 150, 10));
 46     cvPutText(Plane, "6", cvPoint(250, 365), &cvFont(1, 1), cvScalar(50, 150, 10));
 47     cvPutText(Plane, "7", cvPoint(188, 347), &cvFont(1, 1), cvScalar(50, 150, 10));
 48     cvPutText(Plane, "8", cvPoint(153, 310), &cvFont(1, 1), cvScalar(50, 150, 10));
 49     
 50     cvPutText(Plane, "9", cvPoint(135, 255), &cvFont(1, 1), cvScalar(50, 150, 10));
 51     cvPutText(Plane, "10", cvPoint(140, 200), &cvFont(1, 1), cvScalar(50, 150, 10));
 52     cvPutText(Plane, "11", cvPoint(175, 165), &cvFont(1, 1), cvScalar(50, 150, 10));
 53     cvPutText(Plane, "1", cvPoint(300, 160), &cvFont(1, 1), cvScalar(50, 150, 10));
 54     cvPutText(Plane, "2", cvPoint(340, 195), &cvFont(1, 1), cvScalar(50, 150, 10));
 55     cvCircle(Plane,cvPoint(250,250),3,cvScalarAll(50),4);
 56     cvNamedWindow("Clock");
 57     cvSetMouseCallback("Clock",Stop_Run_onclick,0);
 58     Drwan_button(Plane);
 59     cvShowImage("Clock",Plane);
 60     IplImage* img = cvCreateImage(cvGetSize(Plane),8,3);
 61     double dangle = 0.1036;
 62     int r = 100;
 63     int x = 250, y = 250;//圆心
 64     int count_s = 0;
 65     int count_m = 0;
 66     int x1, x2;
 67     //for seconds
 68     double angle = 0;
 69     int dx = 0;
 70     int dy = 0;
 71     //for minutes
 72     double angle_m = 0;
 73     int mx = 0;
 74     int my = 0;
 75     //for hours
 76     double angle_h = 0;
 77     int hx=0, hy = 0;
 78     int count_h = 0;
 79     //设置时钟输入调节
 80     //angle;
 81     //angle_m;
 82     //angle_h;
 83     //count_m;
 84     //count_s;
 85     printf("\t\t\t选项\n\t\t\t从零点开始走针(1)\n\t\t\t从历史记录的时间走针(2)\n\t\t\t输入选项:");
 86     int judge;
 87     scanf("%d", &judge);
 88     if (judge == 2){
 89         FILE *file = fopen("TimeRecord.dat", "r");
 90         fscanf(file, "%lf %lf %lf %d %d %d", &angle_h, &angle_m, &angle, &count_h, &count_m, &count_s);
 91         fclose(file);
 92     }
 93     system("cls");
 94     printf("按下ESC键可以退出!\n");
 95     printf("Enter键或者屏幕按键可以暂停钟表转动\n");
 96     printf("按下空格键可以设置时间:(输入格式为00:00:00)\n");
 97     
 98     while (true)
 99     {
100         if (run){
101             cvCopy(Plane, img);
102             dy = r * cos(angle);
103             dx = r * sin(angle);
104             //for seconds
105             my = r * cos(angle_m);
106             mx = r * sin(angle_m);
107             //for hours
108             hx = r * sin(angle_h);
109             hy = r * cos(angle_h);
110             count_s++;
111             angle=PIZHI(count_s);
112             cvLine(img, cvPoint(x, y), cvPoint(x + hx / 2, x - hy / 2), cvScalar(100, 0, 250), 3);
113             cvLine(img, cvPoint(x, y), cvPoint(x + mx / 1.5, y - my / 1.5), cvScalar(10, 90, 50), 2);
114             cvLine(img, cvPoint(x, y), cvPoint(x + dx, y - dy), cvScalar(100, 90, 0), 1);
115             
116             if (count_s == 60||judge==10)
117             {
118                 if (judge!=10)
119                 count_s = 0;
120                 count_m++;
121                 angle = 0;
122                 angle_m=PIZHI(count_m);
123                 if (count_m % 12 == 0||judge==10){
124                     count_h++;
125                     angle_h = PIZHI(count_h);
126                     if (count_h == 60)count_h = 0;
127                 }
128                 if (count_m == 60)count_m = 0;
129                 judge = 0;
130             }
131         }
132         Drwan_button(img);
133         cvShowImage("Clock", img);
134         
135         char ch = cvWaitKey(1000);
136         if (ch == 27)
137         {
138             FILE* write = fopen("TimeRecord.dat","w");
139             fprintf(write, "%lf %lf %lf %d %d %d", angle_h, angle_m, angle, count_h, count_m, count_s);
140             fclose(write);
141             break;
142         }
143         else if (ch==13)
144         {
145             run = !run;
146         }
147         else if (ch == 32)
148         {
149             scanf("%d:%d:%d",&count_h,&count_m,&count_s);
150             count_h=count_h * 5 + 1.0*count_m / 12.0;
151             judge = 10;
152         }
153     }
154     cvWaitKey(0);
155     return 0;
156 }

做着挺好玩的,就当练练手吧。

 

opencv函数制作的时钟模型

标签:

原文地址:http://www.cnblogs.com/sytu/p/4206603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!