logo

为什么图像的像素点少于某个值(且不为零)程序就会崩溃

2019-07-20 19:38
1
stzo
34cd72cb-f283-4456-b09f-b9a7bf5c68dd.png
import sensor, image, time
from pyb import LED
from pyb import UART,Timer
import Message
from pid import PID
rho_pid = PID(p=5.0, i=0.1, d=0.3)   #控制rol
theta_pid = PID(p=3.0, i=0.2)#控制yaw

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQQVGA)
sensor.set_vflip(False)
sensor.set_hmirror(False)
sensor.skip_frames(time = 2000) # let auto-gain/auto-white-balance run before using images

yanse=(0,100)

class zhixian():
    angle=0
    yaw=0
    pitch=0
    magnitude=0

a1=0
a2=0
b1=0
b2=0
c1=0
c2=0
last_x=0
last_y=0

rho=0
angle=0
yaw=0

rho_err=0
angle_err=0
yaw_err=0

clock = time.clock()
lx=zhixian()
i=0
while True:
    clock.tick()
    img = sensor.snapshot().binary([yanse])#二值化

    i=0
    last_x=0
    last_y=0
    rho=0
    angle=0
    x=0
    y=0
    maxbo=0
    #blobs = img.find_blobs([(250,255)],merge=True)
    #for b in blobs :
        #if maxbo<b.pixels():
            #maxbo=b.pixels()
    #if maxbo>20 :

    for l in img.find_lines(threshold = 1000, theta_margin = 50, rho_margin = 50) :
        i=i+1
        #if(min_degree <= l.theta()) and (l.theta() <= max_degree):
        if i==1:
            #img.draw_line(l.line(), color = (255, 0, 0))
            a1=l[3]-l[1];
            b1=l[0]-l[2];
            c1=l[2]*l[1]-l[0]*l[3];
        elif i==2 :
            a2=l[3]-l[1];
            b2=l[0]-l[2];
            c2=l[2]*l[1]-l[0]*l[3];
        else:
            img.draw_line(l.line(), color = (255, 0, 0));

    if i>1 :
        if((a1*b2-a2*b1)!=0 and (a1*b2-a2*b1)!=0):
            img.draw_cross(int((b1*c2-b2*c1)/(a1*b2-a2*b1)),int((c1*a2-c2*a1)/(a1*b2-a2*b1)),10,color=[255,0,0]);
            last_x=int((b1*c2-b2*c1)/(a1*b2-a2*b1));
            last_y=int((c1*a2-c2*a1)/(a1*b2-a2*b1));
            Message.UartSendData(Message.DotDataPack(last_y,last_x,0))
            print(2,   last_x,   last_y  )
    elif i==1 :
        lines = img.get_regression([(255,255)],robust = True)

           # print(lines.line())
            #lx.yaw=lines.theta()
            #lx.pitch=lines.rho()
            #lx.magnitude=lines.magnitude()

            #rho_err = abs(lines.rho())-img.width()/2
            #if lines.theta()>90:
                #angle_err = lines.theta()-180
            #else:
                #angle_err = lines.theta()
            #if lines.magnitude()>8:
                ##if -40<b_err<40 and -30<t_err<30:
                #rho = rho_pid.get_pid(lines.rho(),1)         #正值->右移  rol
                #angle = theta_pid.get_pid(angle_err,1)   #正值->右转  yaw
            ##for ls in lines:
        rho=lines.rho()
        angle=lines.theta()
        Message.UartSendData(Message.DotDataPack(20,int(rho),int(angle)))
            #print(1,    rho,   angle  )
    print(clock.fps())
2019-07-22 12:18
I
网红小智智 星瞳科技

代码不对啊。

如果get_regression没有找到,你的代码里的lines就是空的。空的自然没有rho和theta。

另外,get_regression返回的是一条线,而不是很多线,所以应该是line而不是lines。

参考代码:

https://book.openmv.cc/project/follow-lines.html#%E8%B0%83%E6%95%B4%E5%8F%82%E6%95%B0%EF%BC%8C%E5%AE%9E%E7%8E%B0%E8%B7%9F%E9%9A%8F