23年电赛e题-->自动追踪系统openmv代码(讲解+视觉完整代码)_23电赛e题
openmv很常用了,会有几个很明显的问题,看不见,找不到红色激光。
第四问找到的矩形,的四个点顺序不对,应该怎么描点(细分多少份好一点)。
这个就是问题了。
现在一个一个解决。
1.关于看不见的问题
sensor.set_brightness(-3)sensor.set_contrast(-3)
调试这两个函数。
2.关于找到四个矩形框,四个点不是顺时针问题。
先是找矩形框:
for i in range(10): img = sensor.snapshot().lens_corr(strength = 1.5, zoom = 1) #img.binary(heikuang)#.erode(1) for r in img.find_rects(threshold = 10000,roi=(75,5,191,203)): #img.draw_rectangle(r.rect(), color = (255, 0, 0)) for p in r.corners(): a+=1 img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) #print(p) if a<=4: dian.add(p)
我找10次就好了,然后画出来。
看找的对不对。
这块的代码就是对点进行排序。(思路是,x最小为第一个点,x最大为第三个点,y最大为第四个,y最小为第二个。)
完美解决!!!
3.细分多少份。
我这里有绝妙的方法,我细分了无数份。---》我上面求出了矩形的四个点,并且·对他进行排序了。然后我就可以求出这,四条边的直线了,然后让直线·向内收缩,收缩到中心位置。
我的细分点为,目标点,红色点为当前值。我用当前值+1个像素,作为目标点的x,再把x代直线求y。就是完美的细分。
具体代码:
1. 给点求直线。
2.求出直线并向内收缩
(我这里等分了,这个不用管,我在下面将这几个点画出来,看我找的对不对!!!)
3.细分(我这里求目标点和现在点的差值,来进行换点!!!)
其实具体代码思路就那样,具体代码要靠自己去写。
完整代码:
# Untitled - By: 24882 - Tue Jun 25 2024heikuang =[(0, 107)]roi1=[(79,32,165,163),(81,12,401,404)]bai_se=0red = [(0, 60, 2, 68, 53, -24)]green=[(59, 85, -78, 23, -11, 60)]import sensor, image, time,pybfrom pyb import Pin, ExtIntfrom pyb import LEDfrom pyb import UARTfrom pid import PIDfrom machine import UARTsensor.reset()sensor.set_pixformat(sensor.RGB565)sensor.set_framesize(sensor.QVGA)sensor.skip_frames(time = 20)sensor.set_hmirror(True)sensor.set_vflip(True)clock = time.clock()uart = UART(3, 115200)uart1 = UART(1, 115200)dian=set()a=0b=()c=()d=()e=0dian1=[]dian2=[]dian3=[]dian4=[]zuobiao=[]zhongjian=()def sending_data3(a1,a2,a3,a4,a5,a6,a7,a8): FH= bytearray([0x01,a1,a2,a3,a4,a5,a6,a7,a8,0x64]) uart.write(FH)def sending_data1(a1,a2,a3,a4,a5,a6,a7,a8): FH= bytearray([0x01,a1,a2,a3,a4,a5,a6,a7,a8,0x64]) uart1.write(FH)for i in range(10): img = sensor.snapshot().lens_corr(strength = 1.5, zoom = 1) #img.binary(heikuang)#.erode(1) for r in img.find_rects(threshold = 10000,roi=(75,5,191,203)): #img.draw_rectangle(r.rect(), color = (255, 0, 0)) for p in r.corners(): a+=1 img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) #print(p) if amax_size: max_blob = blob max_size = blob[4] return max_blobi=0flaghuan=0countflag=0dian1x=0dian1y=0dian2x=0dian2y=0sensor.set_pixformat(sensor.RGB565)sensor.set_brightness(-3)sensor.set_contrast(-3)mode=0while(True): mode=2 dian1=list(dian) dian1.sort(key=takeSecond) #print(dian1) img = sensor.snapshot().lens_corr(strength = 1.5, zoom = 1)* sorted_indices = sorted(enumerate(dian1), key=lambda x: x[1][0]) sorted_matrix = [dian1[idx] for idx, _ in sorted_indices] z1x=sorted_matrix[0][0] z1y=sorted_matrix[0][1] z3x=sorted_matrix[3][0] z3y=sorted_matrix[3][1] sorted_indices2 = sorted(enumerate(dian1), key=lambda y: y[1][1]) sorted_matrix2 = [dian1[idy] for idy, _ in sorted_indices2] z2x=sorted_matrix2[0][0] z2y=sorted_matrix2[0][1] z4x=sorted_matrix2[3][0] z4y=sorted_matrix2[3][1] zhixianwai1(z1x,z1y,z2x,z2y) dengfen1=abs(z2x-z1x)/3 mubiao1x=z1x+dengfen1 mubiao1y=mubiao1x*k1+b1+5 mubiao2x=z1x+dengfen1*2 mubiao2y=mubiao2x*k1+b1+5 zhixianwai2(z2x,z2y,z3x,z3y) dengfen2=abs(z3x-z2x)/3 mubiao3x=z2x+dengfen2 mubiao3y=mubiao3x*k2+b2+5 mubiao4x=z2x+dengfen2*2 mubiao4y=mubiao4x*k2+b2+5 zhixianwai3(z4x,z4y,z3x,z3y) dengfen3=abs(z2x-z1x)/3 mubiao6x=z4x+dengfen3 mubiao6y=mubiao6x*k3+b3-5 mubiao5x=z4x+dengfen3*2 mubiao5y=mubiao5x*k3+b3-5 zhixianwai4(z1x,z1y,z4x,z4y) dengfen4=abs(z1x-z4x)/3 mubiao8x=z1x+dengfen4 mubiao8y=b4+mubiao8x*k4-5 mubiao7x=z1x+dengfen4*2 mubiao7y=b4+mubiao7x*k4-5 img.draw_cross(int(mubiao1x),int(mubiao1y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao2x),int(mubiao2y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao3x),int(mubiao3y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao4x),int(mubiao4y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao5x),int(mubiao5y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao6x),int(mubiao6y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao7x),int(mubiao7y), size=1, color=(255,0,0)) img.draw_cross(int(mubiao8x),int(mubiao8y), size=1, color=(255,0,0)) #print(i) if (i==0): mubx=z1x muby=z1y are1 = img.find_blobs(green,roi=(0,0,317,237),merge=True) are0 = img.find_blobs(red,roi=(93,4,202,216),merge=True) blobs1 = find_max(are1) blobs0 = find_max(are0) if(blobs0): img.draw_rectangle(blobs0.rect(),color=(255,255,0)) print(flaghuan) sending_data3(blobs0.cx(),blobs0.cy(),abs(int(mubx)),abs(int(muby)),0,0,0,0) if(abs(blobs0.cx()-z3x)<=10): flaghuan=1 if(abs(blobs0.cy()-z4y)<=5): flaghuan=2 if(abs(blobs0.cx()-z1x)<=5): flaghuan=0 if((abs(blobs0.cx()-mubx)<=3) and(abs(blobs0.cy()-muby)z1x)and(blobs0.cx()z2x-5)and(blobs0.cx()<z3x)and(flaghuan==0)): mubx=mubx+1 muby=mubx*k2+b2+5 if((blobs0.cy()=z3y-10)and(flaghuan==1)): mubx=mubx-1 muby=mubx*k3+b3-5 if((blobs0.cy()z1y)and(flaghuan==2)): mubx=mubx-1 muby=mubx*k4+b4-5 dian2x=blobs0.cx() dian2y=blobs0.cy() else : dian2x=0 dian2y=0 if(blobs1): img.draw_rectangle(blobs1.rect(),color=(255,255,0)) dian1x=blobs1.cx() dian1y=blobs1.cy() sending_data1(dian1x,dian1y,dian2x,dian2y,0,0,0,0) print(dian1x,dian1y,dian2x,dian2y,0,0,0,0) else: dian2x=0 dian2y=0