实例介绍
【实例简介】openmv目标检测代码
【实例截图】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | class GeometryFeature: def __init__( self , img): self .img = img @staticmethod def trans_line_format(line): ''' 将原来由两点坐标确定的直线,转换为 y = ax b 的格式 ''' x1 = line.x1() y1 = line.y1() x2 = line.x2() y2 = line.y2() if x1 = = x2: # 避免完全垂直,x坐标相等的情况 x1 = 0.1 # 计算斜率 a a = (y2 - y1) / (x2 - x1) # 计算常数项 b # y = a*x b -> b = y - a*x b = y1 - a * x1 return a,b @staticmethod def calculate_angle(line1, line2): ''' 利用四边形的角公式, 计算出直线夹角 ''' angle = ( 180 - abs (line1.theta() - line2.theta())) if angle > 90 : angle = 180 - angle return angle @staticmethod def find_verticle_lines(lines, angle_threshold = ( 70 , 90 )): ''' 寻找相互垂直的两条线 ''' return GeometryFeature.find_interserct_lines(lines, angle_threshold = angle_threshold) @staticmethod def find_interserct_lines(lines, angle_threshold = ( 10 , 90 ), window_size = None ): ''' 根据夹角阈值寻找两个相互交叉的直线, 且交点需要存在于画面中 ''' line_num = len (lines) for i in range (line_num - 1 ): for j in range (i, line_num): # 判断两个直线之间的夹角是否为直角 angle = GeometryFeature.calculate_angle(lines[i], lines[j]) # 判断角度是否在阈值范围内 if not (angle > = angle_threshold[ 0 ] and angle < = angle_threshold[ 1 ]): continue # 判断交点是否在画面内 if window_size is not None : # 获取串口的尺寸 宽度跟高度 win_width, win_height = window_size # 获取直线交点 intersect_pt = GeometryFeature.calculate_intersection(lines[i], lines[j]) if intersect_pt is None : # 没有交点 continue x, y = intersect_pt if not (x > = 0 and x < win_width and y > = 0 and y < win_height): # 交点如果没有在画面中 continue return (lines[i], lines[j]) return None @staticmethod def calculate_intersection(line1, line2): ''' 计算两条线的交点 ''' a1 = line1.y2() - line1.y1() b1 = line1.x1() - line1.x2() c1 = line1.x2() * line1.y1() - line1.x1() * line1.y2() a2 = line2.y2() - line2.y1() b2 = line2.x1() - line2.x2() c2 = line2.x2() * line2.y1() - line2.x1() * line2.y2() if (a1 * b2 - a2 * b1) ! = 0 and (a2 * b1 - a1 * b2) ! = 0 : cross_x = int ((b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1)) cross_y = int ((c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1)) return (cross_x, cross_y) return None |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论