特征识别-圆和矩形(Identify circles and rectangles)
来源:互联网 发布:显卡温度监控软件 编辑:程序博客网 时间:2024/06/05 20:42
基于OpenMV平台进行开发
识别原理:
定义矩形度的公式:
Rectangularity = area_real/area_external矩形度 = 图形面积/外接矩形面积
圆和矩形的矩形度值不同
识别结果:
程序代码:
import sensor, image, time, math, pyb
# 全局变量
GRAYSCALE_THRESHOLD = [(0, 100)]
Rectangularity_Threshold = 0.9 # 设定阈值
# 调用硬件
led = pyb.LED(3) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
clock = time.clock() # Tracks FPS.
def senser_init(): # 摄像头传感器初始化函数
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(30) # Let new settings take affect.
sensor.set_auto_gain(True) # must be turned off for color tracking
sensor.set_auto_whitebal(True) # must be turned off for color tracking
return;
def hardware_init():
led.on() #亮灯
time.sleep(100) #延时150ms
led.off() #暗灯
time.sleep(100)
return;
def Maximum_interclass_variance_method(grayFrame): # 最大类间方差法
w = 0
avgValue = 0
t = 0
maxVariance = 0
statistics_data = grayFrame.get_statistics()
u = statistics_data.mean()
histogram_data = grayFrame.get_histogram() # 获取灰度直方图
histogram_bins = histogram_data.bins() # 获取灰度直方图的列表
for i in range(len(histogram_bins)):
w += (histogram_bins[i]+0.0000001)
avgValue += i * (histogram_bins[i]+0.0000001)
t = avgValue/w - u
variance = t * t * w /(1 - w)
if(variance > maxVariance):
maxVariance = variance;
threshold = i;
return threshold
# 主函数
hardware_init();
senser_init();
while(True):
img = sensor.snapshot() # Take a picture and return the image.
h = Maximum_interclass_variance_method(img); # 最大类间方差法计算阈值
GRAYSCALE_THRESHOLD =[(0, h)]
blobs = img.find_blobs(GRAYSCALE_THRESHOLD, merge=True) # 图像分割
if blobs:
for i in range(len(blobs)):
area_real = blobs[i].pixels() # 实际面积
area_external = blobs[i].w() * blobs[i].h() # 外接矩形的面积
Rectangularity = area_real/area_external # 计算矩形度
if Rectangularity > Rectangularity_Threshold: # 大于阈值为矩形
img.draw_rectangle(blobs[i].rect())
elif Rectangularity < Rectangularity_Threshold: # 小于阈值为圆型
img.draw_circle(blobs[i].x()+int(blobs[i].w()/2), blobs[i].y()+int(blobs[i].w()/2),int(blobs[i].w()/2), color=255)
阅读全文
1 1
- 特征识别-圆和矩形(Identify circles and rectangles)
- UVA 478 Points in Figures: Rectangles, Circles, and Triangles
- UVa 477 - Points in Figures: Rectangles and Circles
- Drawing Rectangles(绘制矩形)
- zoj 1608 Two Circles and a Rectangle 判断两个圆是否能放入一个矩形中
- poj 3695 Rectangles(矩形切割)
- HDU 2056 Rectangles(矩形面积交)
- hdoj 2056 Rectangles(矩形) 【几何】
- 矩形个数-Rectangles
- Usaco 1.4.1 铺放矩形块(Packing Rectangles)
- HDOJ 2056 Rectangles(坐标排序、矩形面积并)
- Overlapping Rectangles(线段树,矩形面积并)
- hdu 2056 -- Rectangles (矩形重合)
- HDU2056 Rectangles 【矩形面积交】
- POJ 3695 Rectangles(矩形切割)
- D. Timofey and rectangles
- [arc081f]Flip and Rectangles
- UVa 478 - Points in Figures: Rectangles, Circles, Triangles
- Class.forName 和 ClassLoader的区别
- 单点登录原理与简单实现
- java变量类型
- 以管理员身份删除文件夹
- 解决Uploadify上传控件加载导致的GET 404 Not Found问题
- 特征识别-圆和矩形(Identify circles and rectangles)
- session过期和session同步(springmvc+mybatis+cookie)
- CasServer添加子系统登陆权限验证
- 使用Spring连接数据库数据创建JSON数据 作为接口
- Js 对小数的处理(科学计数法 , 显示精度)
- Unity与VS关联
- 高中逍遥游快速记忆
- 微信分享实践和踩坑实践
- [绍棠_Swift] Swift3.0中Get、Post的同步与异步请求