import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
def find_center(img):
my_place=[120,160]
heng=[]
shu=[]
center = []
min_degree = 80
max_degree = 100
count=[]
###################找最靠近飞机坐标点的两条横线从而得出方框中心点纵坐标
for l in img.find_lines(roi=(50,30,220,180),threshold = 900, theta_margin = 25, rho_margin = 25):
if (min_degree <= l.theta()) and (l.theta() <= max_degree):
img.draw_line(l.line(), color = (255, 0, 0))
heng.append(l)
# print("横线p值"heng.rho())
if len(heng)>=2:
heng1 = heng[0]
for l in heng:
if abs(l.rho()-my_place[0])<abs(heng1.rho()-my_place[0]):
heng1 = l
heng.pop(heng.index(heng1))
#print(heng)
heng2 = heng[0]
for l in heng:
if abs(l.rho()-my_place[0])<abs(heng2.rho()-my_place[0]):
heng2 = l
count.append(1)
#center_y = (heng1.rho()+heng2.rho())/2
#center.append(center_y)
#print(heng1,heng2,center_y)
##########################找中心横坐标
for l in img.find_lines(roi=(50,30,220,180),threshold = 1000,theta_margin = 35, rho_margin = 25):
if (30 >= l.theta()) or (l.theta() >= 150):
img.draw_line(l.line(), color = (255, 0, 0))
shu.append(l)
#print("竖线坐标"shu)
if len(shu)>=2:
shu1 = shu[0]
for l in shu:
if abs(abs(l.rho())-my_place[1])<abs(abs(shu1.rho())-my_place[1]):
shu1 = l
shu.pop(shu.index(shu1))
# print(shu)
shu2 = shu[0]
for l in shu:
if abs(abs(l.rho())-my_place[1])<abs(abs(shu2.rho())-my_place[1]):
shu2 = l
count.append(1)
if len(count)==2:
center_y_1=(math.cos(shu1.theta()/180*3.14159)*heng1.rho()-math.cos(heng1.theta()/180*3.14159)*shu1.rho())/(math.sin(heng1.theta()/180*3.14159)*math.cos(shu1.theta()/180*3.14159)-math.sin(shu1.theta()/180*3.14159)*math.cos(heng1.theta()/180*3.14159))
center_x_1=(math.sin(shu1.theta()/180*3.14159)*heng1.rho()-math.sin(heng1.theta()/180*3.14159)*shu1.rho())/(math.cos(heng1.theta()/180*3.14159)*math.sin(shu1.theta()/180*3.14159)-math.cos(shu1.theta()/180*3.14159)*math.sin(heng1.theta()/180*3.14159))
center_y_2=(math.cos(shu2.theta()/180*3.14159)*heng2.rho()-math.cos(heng2.theta()/180*3.14159)*shu2.rho())/(math.sin(heng2.theta()/180*3.14159)*math.cos(shu2.theta()/180*3.14159)-math.sin(shu2.theta()/180*3.14159)*math.cos(heng2.theta()/180*3.14159))
center_x_2=(math.sin(shu2.theta()/180*3.14159)*heng2.rho()-math.sin(heng2.theta()/180*3.14159)*shu2.rho())/(math.cos(heng2.theta()/180*3.14159)*math.sin(shu2.theta()/180*3.14159)-math.cos(shu2.theta()/180*3.14159)*math.sin(heng2.theta()/180*3.14159))
center_x=(abs(center_x_1)+abs(center_x_2))/2
center_y=(abs(center_y_1)+abs(center_y_2))/2
print("中心x",center_x_1,center_x_2)
print("中心y",center_y_1,center_y_2)
return int(center_x), int(center_y)
img.draw_cross(my_place[1], my_place[0], size=3,color=(0))
img.draw_cross(int(center_x), int(center_y), size=5,color=(0))
img.draw_arrow(my_place[1], my_place[0], int(center_x), int(center_y), color = (0), size = 10, thickness = 2)
想要进行识别方块,中心坐标 在运行后屏幕卡住,并弹出屏幕截图的显示。