

import sensor, image, time, display, math
from pyb import UART
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_whitebal(False)
sensor.set_auto_gain(False)
#sensor.set_windowing((160, 120))
sensor.skip_frames(time = 2000)
clock = time.clock()
uart = UART(3, 9600)
#阈值设置
color = (0, 43, -128, 127, -128, 127)
mode = 0
rois = []
#------------------------------定义缩小四边形函数
def shrink_rect(corners, shrink_by):
# 计算中心点
cx = int(sum(p[0] for p in corners) / len(corners))
cy = int(sum(p[1] for p in corners) / len(corners))
# 缩小每个顶点
new_corners = []
for p in corners:
dx = p[0] - cx
dy = p[1] - cy
distance = math.sqrt(dx * dx + dy * dy)
scale = (distance - shrink_by) / distance if distance > 0 else 0
new_x = int(cx + dx * scale)
new_y = int(cy + dy * scale)
new_corners.append((new_x, new_y))
return new_corners, cx, cy
#------------------------------求两点中点坐标
def Make_dian(new_xy):
mid2x = abs((new_xy[2][0] + new_xy[3][0]) / 2)
mid4x = abs((new_xy[3][0] + new_xy[0][0]) / 2)
mid6x = abs((new_xy[2][0] + new_xy[1][0]) / 2)
mid8x = abs((new_xy[1][0] + new_xy[0][0]) / 2)
mid2y = abs((new_xy[2][1] + new_xy[3][1]) / 2)
mid4y = abs((new_xy[3][1] + new_xy[0][1]) / 2)
mid6y = abs((new_xy[2][1] + new_xy[1][1]) / 2)
mid8y = abs((new_xy[1][1] + new_xy[0][1]) / 2)
newmid = [int(mid2x), int(mid2y),int(mid4x), int(mid4y), int(mid6x), int(mid6y), int(mid8x), int(mid8y)]
# print(newmid)
return newmid
#def find_rois(max_rect):
#def color_blob(color, roi_r):
# for blobs in img.find_blobs([color], pixels_threshold=100, area_threshold=100):
# if blobs:
# cx = blobs.cx()
# cy = blobs.cy()
# img.draw_cross(cx,cy,color= (255,255,255))
# return cx,cy
#-----------------------------通过面积进行识别最大的矩形
def find_max_rect():
max_rect = None #初始化返回的最大矩形
rect_max_w = 0 #通过比较矩形的面积来判断矩形大小
rect_max_h = 0
for black_rect in img.find_rects(threshold = 25000):
if (black_rect.w()*black_rect.h()) > (rect_max_w*rect_max_h):
rect_max_w = black_rect.w()
rect_max_h = black_rect.h()
max_rect = black_rect
# print(max_rect)
return max_rect
#------------------------------------画矩形---------------------------------#
def four_corners_draw(a):
img.draw_line(a[0][0], a[0][1], a[1][0], a[1][1], (255, 0, 0), 1)
img.draw_line(a[1][0], a[1][1], a[2][0], a[2][1], (255, 0, 0), 1)
img.draw_line(a[2][0], a[2][1], a[3][0], a[3][1], (255, 0, 0), 1)
img.draw_line(a[3][0], a[3][1], a[0][0], a[0][1], (255, 0, 0), 1)
# print(a)
while(True):
clock.tick()
img = sensor.snapshot()
max1_rect = find_max_rect()
if max1_rect:
mode = 1
img.draw_rectangle(max1_rect.rect(), color=(255, 0, 0))
a = max1_rect.corners()
four_corners_draw(a) #画矩形
# print(a)
new_xy, cx, cy = shrink_rect(max1_rect.corners(), 10) #计算等比例矩形
new0_xy =new_xy #打印四角点7 9 3 1
# print(new0_xy)
newmid = Make_dian(new_xy)
# print(newmid) #打印2,4,6,8 点
# for w in range(0,7,2):
# img.draw_cross(newmid[w],newmid[w+1],5,color=(255,0,0))
# img.draw_cross(cx, cy, 5, color=(255, 0, 0)) #画出中心点
# for new1 in new_xy:
# img.draw_cross(new1[0], new1[1], 5, color=(255, 0, 0)) #画出缩小后矩形的点(十字)
# print(new1)
#------------------------棋盘坐标9点
if mode == 1:
#------棋盘1---------------------------棋盘2------------------棋盘3--------------------------棋盘4
data = [new0_xy[3][0], new0_xy[3][1], newmid[0], newmid[1], new0_xy[2][0], new0_xy[2][1], newmid[2], newmid[3],
#棋盘5---棋盘6------------------棋盘7--------------------------棋盘8-----------------棋盘9
cx, cy, newmid[4], newmid[5], new0_xy[0][0], new0_xy[0][1], newmid[6], newmid[7], new0_xy[1][0], new0_xy[1][1]]
print(data)
rois = [(data[0]-2,data[1]-2,5,5),
(data[2]-2,data[3]-2,5,5),
(data[4]-2,data[5]-2,5,5),
(data[6]-2,data[7]-2,5,5),
(data[8]-2,data[9]-2,5,5),
(data[10]-2,data[11]-2,5,5),
(data[12]-2,data[13]-2,5,5),
(data[14]-2,data[15]-2,5,5),
(data[16]-2,data[17]-2,5,5),]
for r in rois :
img.draw_rectangle(r,color = (255,255,255))
for blobs in img.find_blobs([(10, 0, -76, 114, -118, 99)],roi = r):
img.draw_cross(blobs.cx(),blobs.cy())
mode = 2
##-----------------------更新棋盘上的落子---------------
# for t in range(0,8):
# for blobs in img.find_blobs([(10, 0, -76, 114, -118, 99)], roi=rois[t]):
# img.draw_cross(blobs.cx(), blobs.cy(),color = (0,255,0))
# print(blobs.cx(), blobs.cy())
# if mode == 2:
# for t in range(0,8):
# for blobs in img.find_blobs([(10, 0, -76, 114, -118, 99)], roi=rois[t]):
# img.draw_cross(blobs.cx(), blobs.cy(),color = (0,255,0))
# print(blobs.cx(), blobs.cy())你把r打印出来,就能看到r的值不对。超出了图片的范围。
没有,roi能打印在图现上,我怀疑roi的11值11不稳定,导致报错,如果是超出,是不是因为我设置了窗口的大小sensor.set_windowing((160, 120))