识别最大矩形的时候代码出现了问题
-
sensor.reset() sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) sensor.set_auto_whitebal(False) clock = time.clock() class Rec(object): flag = 0 x = 0 y = 0 def Find_Max(rects): max_size=1 if rects: max_rect = 0 for rect in rects: rect_size =rect.w()*rect.h() if ((rect_size > max_size) & (rect_size > 100)) :#& (blob.density()<1.2*math.pi/4) & (blob.density()>0.8*math.pi/4) if ( math.fabs( rect.w() / rect.h() - 1 ) < 2.0 ) : max_rect=rect max_size = rect.w()*rect.h() return max_rect while(True): clock.tick() img = sensor.snapshot() for r in img.find_rects(threshold = 12000): img.draw_rectangle(r.rect(),color = (255, 0, 0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) max_rect = Find_Max(p) img.draw_cross(max_rect.x() + max_rect.w()//2, max_rect.y() + max_rect.h()//2) x1 = max_rect.x() + max_rect.w()//2 y1 = max_rect.y() + max_rect.h()//2 fps=int(clock.fps()) Message.Ctr.T_ms = (int)(1000/fps) img.draw_cross(x1, y1) Rec.flag = 1 Rec.x = x1-80 Rec.y = y1-60 #print(r) print(' x= ',Rec.x,'y= ',Rec.y,'fps',fps,'T_ms',Message.Ctr.T_ms) LED(3).toggle() print("flag= ",Rec.flag) Message.UartSendData(Message.DotDataPack(1,Rec.flag,Rec.x,Rec.y,Message.Ctr.T_ms))
-
第31行,max_rect = Find_Max(p)改为max_rect = Find_Max(r)