Seems like something is wrong with robust linear regression. It gets the answer wrong with the horizontal line.
BINARY_VIEW = True
GRAYSCALE_THRESHOLD = (200, 255)
MAG_THRESHOLD = 12
MAG_THRESHOLD_HORIZONTAL = 5
import sensor, image, time, math, pyb
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQQVGA)
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_auto_gain(True)
sensor.set_auto_exposure(True)
sensor.set_auto_whitebal(True)
sensor.skip_frames(time = 2000)
clock = time.clock()
uart = pyb.UART(3, 115200)
previousROIvertical = (0,0,80,60)
previousROIhorizontal = (0,0,80,60)
marginROI = 10
marginROIhorizontal = 3
output = 0
while True:
clock.tick()
print_string = ""
img = image.Image("pre.bmp", copy_to_fb=True).invert().to_grayscale()
img = img.lens_corr(strength = 1.5, zoom = 1.0)
if BINARY_VIEW:
img.binary([GRAYSCALE_THRESHOLD])
copy = img.copy()
#VERTICAL LINE
line = copy.get_regression([(255, 255)], roi=previousROIvertical, robust=True)
if line and (line.magnitude() >= MAG_THRESHOLD):
#Compute ROI for next frame
if line.x1() < line.x2():
previousROIvertical = (line.x1()-int(marginROI/2), 0, marginROI+line.x2()-line.x1(), 60)
else:
previousROIvertical = (line.x2()-int(marginROI/2), 0, marginROI+line.x1()-line.x2(), 60)
#Draw ROI
copy.draw_rectangle((previousROIvertical[0]-3, previousROIvertical[1],previousROIvertical[2]+6, previousROIvertical[3]), color=0, fill=True) #Zero out the ROI and 3 pixels more on each side of the X axis
img.draw_rectangle(previousROIvertical, color=80)
img.draw_line(line.line(), color=127)
#Add info
print_string = "Line Ok: %d - turn %d - line t: %d, r: %d" % (line.magnitude(), output, line.theta(), line.rho())
elif line and (line.magnitude() < MAG_THRESHOLD):
print_string = "Not a good line: %d - turn %d" % (line.magnitude(), output)
previousROIvertical = (0,0,80,60) #Reset V ROI
else:
print_string = "Line Lost - turn %d" % (output)
previousROIvertical = (0,0,80,60) #Reset V ROI
#HORIZONTAL LINE
lineH = copy.get_regression([(255, 255)], roi=previousROIhorizontal, robust=False)
#Draw the ROI
img.draw_rectangle(previousROIhorizontal, color=80)
if lineH and (lineH.magnitude() >= MAG_THRESHOLD_HORIZONTAL):
#Compute ROI for next frame
if lineH.y1() < lineH.y2():
previousROIhorizontal = (0, lineH.y1()-int(marginROIhorizontal/2), 80, lineH.y2()-lineH.y1()+marginROIhorizontal)
else:
previousROIhorizontal = (0, lineH.y2()-int(marginROIhorizontal/2), 80, lineH.y1()-lineH.y2()+marginROIhorizontal)
#Draw ROI
img.draw_line(lineH.line(), color=127)
#Add info
print_string = print_string + " | LineH Ok: %d - line t: %d, r: %d | %s" % (lineH.magnitude(), lineH.theta(), lineH.rho(), lineH.line())
elif lineH and (lineH.magnitude() < MAG_THRESHOLD_HORIZONTAL):
print_string = print_string + " | Not a good lineH: %d " % (lineH.magnitude())
previousROIhorizontal = (0,0,80,60)
else:
previousROIhorizontal = (0,0,80,60)
print("FPS %f, %s" % (clock.fps(), print_string))
So, I switched to the regular linear regression.