OpenMV测距
測距
OpenMV采用的是單目攝像頭,想要實現(xiàn)測距,就需要選參照物,利用參照物的大小比例來計算距離。
本節(jié)分享一下第二種方法,如何通過攝像頭里乒乓球的大小,計算攝像頭與乒乓球之間的距離。
眾所周知,乒乓球距離攝像頭越遠,攝像頭里乒乓球的大小就越小,那么問題來了?
這個關系到底是什么呢?
(注:此處的數(shù)學幾何問題,僅涉及到高中數(shù)學三角函數(shù)部分,不想看的,直接看結論也可)
為了簡化問題,我們看一下圖:
由左邊的攝像頭里的幾何關系可得知:
所以有(1式)
由右邊的真實環(huán)境里的幾何關系得知:
帶入(1式),可得(結論公式):
上面就是最終我們想知道的關系啦!
這是什么意思呢?
等號左邊的Lm是長度,Bpix是攝像頭中,球所占的像素(直徑的像素)。等號右邊呢,Rm是球真實的半徑,Apix是是固定的像素,a是視角的一半。
所以!所以!所以!所以!所以!所以!所以!
這個公式告訴我們的就是:
實際長度和攝像頭里的像素成反比
簡化就是
距離 = 一個常數(shù)/直徑的像素
好啦,我們已經(jīng)知道關系啦,而且還是這么的優(yōu)雅簡單!
具體操作步驟呢,就是先測出這個常數(shù)的值,怎么測不用說了吧,就是先讓球距離攝像頭10cm,打印出攝像頭里直徑的像素值,然后相乘,就得到了k的值!
然后 距離=這個常數(shù)/攝像頭里像素點,so easy.
我這里貼出OpenMV的代碼:
#Measure the distance
#
#This example shows off how to measure the distance through the size in imgage
#This example in particular looks for yellow pingpong ball.
import sensor, image, time
#For color tracking to work really well you should ideally be in a very, very,
#very, controlled enviroment where the lighting is constant…
yellow_threshold = ( 56, 83, 5, 57, 63, 80)
#You may need to tweak the above settings for tracking green things…
#Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
K=5000#the value should be measured
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
成果是這樣的,通過串口把距離打印出來。
總結
- 上一篇: 【网络】网络基础
- 下一篇: 近十年以来机器学习中最优秀的想法之一~领