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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import sensor, image, time import lcd
sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.set_hmirror(True) sensor.set_vflip(True)
sensor.skip_frames(time = 2000)
clock = time.clock() lcd.init() detect_status = False
""" 函数名:rect_convert 函数作用:由于通常识别的矩形为外框,对识别到的矩形进行一定的缩放,有助于正确标记顶点位置 入口参数:需要处理的图形img、原识别到的矩形顶点 返回:转换后的四个顶点坐标 """
def rect_convert(img,corners): point1 = (corners[0][0], corners[0][1]) point2 = (corners[1][0], corners[1][1]) point3 = (corners[2][0], corners[2][1]) point4 = (corners[3][0], corners[3][1])
scale = 0.98
center = ((point1[0] + point2[0] + point3[0] + point4[0]) / 4, (point1[1] + point2[1] + point3[1] + point4[1]) / 4)
new_point1 = (point1[0] - center[0], point1[1] - center[1]) new_point2 = (point2[0] - center[0], point2[1] - center[1]) new_point3 = (point3[0] - center[0], point3[1] - center[1]) new_point4 = (point4[0] - center[0], point4[1] - center[1])
new_point1 = (new_point1[0] * scale, new_point1[1] * scale) new_point2 = (new_point2[0] * scale, new_point2[1] * scale) new_point3 = (new_point3[0] * scale, new_point3[1] * scale) new_point4 = (new_point4[0] * scale, new_point4[1] * scale)
new_point1 = (int(new_point1[0] + center[0]), int(new_point1[1] + center[1])) new_point2 = (int(new_point2[0] + center[0]), int(new_point2[1] + center[1])) new_point3 = (int(new_point3[0] + center[0]), int(new_point3[1] + center[1])) new_point4 = (int(new_point4[0] + center[0]), int(new_point4[1] + center[1]))
img.draw_string(int(center[0])-20, int(center[1]), "Part", color = (255, 0, 0), scale = 2)
img.draw_circle(new_point1[0], new_point1[1], 8, color = (0, 255, 255)) img.draw_circle(new_point2[0], new_point2[1], 8, color = (0, 255, 255)) img.draw_circle(new_point3[0], new_point3[1], 8, color = (0, 255, 255)) img.draw_circle(new_point4[0], new_point4[1], 8, color = (0, 255, 255))
img.draw_line(new_point1[0], new_point1[1], new_point2[0], new_point2[1], thickness = 2, color = (255, 0, 0)) img.draw_line(new_point2[0], new_point2[1], new_point3[0], new_point3[1], thickness = 2, color = (255, 0, 0)) img.draw_line(new_point3[0], new_point3[1], new_point4[0], new_point4[1], thickness = 2, color = (255, 0, 0)) img.draw_line(new_point4[0], new_point4[1], new_point1[0], new_point1[1], thickness = 2, color = (255, 0, 0))
after_convert = [new_point1, new_point2, new_point3, new_point4] return after_convert
""" 主函数 """
while(True): clock.tick() img = sensor.snapshot()
img = img.erode(2, threshold = 3)
for r in img.find_rects(threshold = 25000,roi=(20,0,130,120)): corners = rect_convert(img,r.corners()) detect_status = True img.draw_rectangle(20,0,130,120, color = (255, 0, 0), thickness = 2, fill = False) img.draw_string(40, 0, "%s" % "Detect Area", color = (255, 0, 0), scale = 1.5) if (detect_status == True): img.draw_string(10, 50, "%s" % "Detect successfully", color = (0, 255, 0), scale = 1.4) img.draw_string(1, 1, "%.1f" % clock.fps(), color = (255, 0, 0), scale = 1.5) lcd.display(img)
|