第三弹 不同方向旋转的三角形和矩形

 1 /*************************************************************************
 2     > File Name: frame.cpp
 3     > Author: 
 4     > Mail: 
 5     > Created Time: 2015年11月21日 星期六 15时28分01秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<GL/glut.h>
10 using namespace std;
11 static GLfloat spin=0.0;
12 void init(void)
13 {
14     glClearColor(0.0,0.0,0.0,0.0);
15     glShadeModel(GL_SMOOTH);
16 }
17 
18 void display(void)
19 {
20     glClear(GL_COLOR_BUFFER_BIT);
21     glPushMatrix();
22     glColor3f(1.0,1.0,1.0);
23 
24     glRotatef(spin,0.0f,0.0f,1.0f);
25     glBegin(GL_TRIANGLES);
26         glColor3f(1.0f,0.0f,0.0f);
27         glVertex3f(0.0f,0.0f,0.0f);
28         glColor3f(0.0f,1.0f,0.0f);
29         glVertex3f(30.0f,0.0f,0.0f);
30         glColor3f(0.0f,0.0f,1.0f);
31         glVertex3f(15.0f,50.0f,0.0f);
32     glEnd();
33 
34     glLoadIdentity();
35     glRotatef(spin,1.0f,0.0f,0.0f);
36     glBegin(GL_QUADS);
37         glVertex3f(0.0f,0.0f,0.0f);
38         glVertex3f(-30.0f,0.0f,0.0f);
39         glVertex3f(-30.0f,30.0f,0.0f);
40         glVertex3f(0.0f,30.0f,0.0f);
41     glEnd();
42     glPopMatrix();
43     glutSwapBuffers();
44 }
45 
46 void spinDisplay(void)
47 {
48     spin=spin+2.0;
49     if(spin>360)
50         spin=spin-360.0;
51     glutPostRedisplay();
52 }
53 
54 void reshape(int w,int h)
55 {
56     glViewport(0,0,(GLsizei)w,(GLsizei)h);
57     glMatrixMode(GL_PROJECTION);
58     glLoadIdentity();
59     glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);
60     glMatrixMode(GL_MODELVIEW);
61     glLoadIdentity();
62 }
63 
64 void mouse(int button,int state,int x,int y)
65 {
66     switch(button)
67     {
68         case GLUT_LEFT_BUTTON:
69             if(state == GLUT_DOWN)
70                 glutIdleFunc(spinDisplay);
71             break;
72         case GLUT_MIDDLE_BUTTON:
73             if(state == GLUT_DOWN)
74                 glutIdleFunc(NULL);
75             break;
76         default:
77             break;
78     }
79 }
80 
81 int main(int argc,char ** argv)
82 {
83     glutInit(&argc,argv);
84     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
85     glutInitWindowSize(400,400);
86     glutInitWindowPosition(100,100);
87     glutCreateWindow("OpenGL test");
88     init();
89 
90     glutDisplayFunc(display);
91     glutReshapeFunc(reshape);
92     glutMouseFunc(mouse);
93 
94     glutMainLoop();
95     return 0;
96 }

 

效果如图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个实现圆形、三角形矩形识别的 OpenCV 代码示例: ```python import cv2 # 定义识别函数 def detect_shape(c): shape = "unknown" peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) # 当逼近形状的顶点数为 3 时,判定为三角形 if len(approx) == 3: shape = "triangle" # 当逼近形状的顶点数为 4 时,计算宽高比来判定为矩形 elif len(approx) == 4: (x, y, w, h) = cv2.boundingRect(approx) ar = w / float(h) shape = "rectangle" if ar >= 0.95 and ar <= 1.05 else "square" # 当逼近形状的顶点数为大于 4 时,判定为圆形 else: shape = "circle" return shape # 读取摄像头视频流 cap = cv2.VideoCapture(0) while True: # 读取视频帧 ret, frame = cap.read() # 转换为灰度图像并进行边缘检测 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(blurred, 50, 150) # 寻找轮廓并进行形状识别 cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in cnts: # 忽略过小的轮廓 if cv2.contourArea(c) < 100: continue # 计算轮廓重心并在图像上标注识别结果 M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) shape = detect_shape(c) cv2.drawContours(frame, [c], -1, (0, 255, 0), 2) cv2.putText(frame, shape, center, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) # 显示图像 cv2.imshow("Frame", frame) # 按 'q' 退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() cv2.destroyAllWindows() ``` 该代码通过边缘检测和轮廓找到图像中的形状,再根据形状的顶点数和宽高比来判断其为圆形、三角形矩形。在运行代码前,需要确保已安装 OpenCV 库并连接了可用的摄像头设备。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值