根据官网颜色自动追踪实例进行修改写了一段程序但出现了错误,请问是怎么回事?
-
如图所示在串行终端显示“Learning thresholds”的时候摄像头还能对物体进行追踪,但在“Threshold learned...”和“Tracking colors...”出现之后就追踪不到,请问哪里还需要修改。
import sensor, image,time,pyb from pyb import UART uart = UART(3,9600) print("Letting auto algorithms run. Don't put anything in front of the camera!") 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() K=650#the value should be measured 50*13 K2=0.1#实际大小=k*直径的像素 k=5/50 # Capture the color thresholds for whatever was in the center of the image. r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 50x50 center of QVGA. print("Auto algorithms done. Hold the object you want to track in front of the camera in the box.") print("MAKE SURE THE COLOR OF THE OBJECT YOU WANT TO TRACK IS FULLY ENCLOSED BY THE BOX!") for i in range(60): img = sensor.snapshot() img.draw_rectangle(r) print("Learning thresholds...") threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values. for i in range(60): r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 50x50 center of QVGA. img = sensor.snapshot() hist = img.get_histogram(roi=r) lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)! hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)! # Average in percentile values. threshold[0] = (threshold[0] + lo.l_value()) // 2 threshold[1] = (threshold[1] + hi.l_value()) // 2 threshold[2] = (threshold[2] + lo.a_value()) // 2 threshold[3] = (threshold[3] + hi.a_value()) // 2 threshold[4] = (threshold[4] + lo.b_value()) // 2 threshold[5] = (threshold[5] + hi.b_value()) // 2 for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) img.draw_rectangle(r) print("Thresholds learned...") print("Tracking colors...") while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10): blobs = img.find_blobs([threshold]) if len(blobs) == 1: # Draw a rect around the blob. b = blobs[0] img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy Lm = (b[2]+b[3])/2 length = K/Lm print("length:%s cm"%(length)) #print(Lm) size=K2*Lm print(size) h=K2*b[3] w=K2*b[2] print("high:%s cm, width:%s cm"%(h,w)) output_str="[%.1f,%.1f,%.1f]" % (length,h,w) print('you send:',output_str) uart.write(output_str+'\r\n') else: print('not found!') #print(clock.fps())
-
你的代码是错的。
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10): blobs = img.find_blobs([threshold]) if len(blobs) == 1:
为什么for循环里面还要再找色块?
-
@kidswong999 因为要测距啊,那要怎么改
-
你的代码是错的。只需要调用一次find_blobs
blobs = img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10) for blob in blobs: xxxx