通過openmv來進行測距的兩種辦法
第一就是通過ApriTag的3D定位進行測距
第二就是通過利用參照物進行測距
通過上圖可以看出實際距離和物體的像素大小成反比
如何得到常數k就是需要解決的問題
先將物體放在固定的距離x,然后打印出物體的像素大小y,就可以得到常數k
k = x*y
1 # Measure the distance 2 # 3 # This example shows off how to measure the distance through the size in imgage 4 # This example in particular looks for yellow pingpong ball. 5 6 import sensor, image, time 7 8 # For color tracking to work really well you should ideally be in a very, very, 9 # very, controlled enviroment where the lighting is constant... 10 yellow_threshold = ( 56, 83, 5, 57, 63, 80) 11 # You may need to tweak the above settings for tracking green things... 12 # Select an area in the Framebuffer to copy the color settings. 13 14 sensor.reset() # Initialize the camera sensor. 15 sensor.set_pixformat(sensor.RGB565) # use RGB565. 16 sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. 17 sensor.skip_frames(10) # Let new settings take affect. 18 sensor.set_auto_whitebal(False) # turn this off. 19 clock = time.clock() # Tracks FPS. 20 21 K=5000#the value should be measured 22 #k = 物體放置距離 × 打印的Lx 23 24 while(True): 25 clock.tick() # Track elapsed milliseconds between snapshots(). 26 img = sensor.snapshot() # Take a picture and return the image. 27 28 blobs = img.find_blobs([yellow_threshold]) 29 if len(blobs) == 1: 30 # Draw a rect around the blob. 31 b = blobs[0] 32 img.draw_rectangle(b[0:4]) # rect 33 img.draw_cross(b[5], b[6]) # cx, cy 34 Lx = (b[2]+b[3])/2 35 print(Lx) 36 length = K/Lx 37 print(length) 38 39 #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while 40 # connected to your computer. The FPS should increase once disconnected.
根據上面代碼先將物體放置在固定位置,例如10cm,然后打印出該物體在openmv中的像素值Lx
可以得出k =10 * Lx
然后就可以對物體進行測距
對於Lx 的計算是先通過將捕捉的物體顏色范圍的色塊,然后通過b[2]色塊的寬和b[3]色塊的高取的物體的平均半徑
參考文章 https://blog.csdn.net/qq_42518517/article/details/101059853