一、原理(以小球为例)
测距: 相同尺寸,距离越近,像素点越多,所以距离与直径像素点个数成反比:K = 距离*直径的像素
测尺寸: 相同距离,尺寸越大,直径像素点越多,所以物体尺寸与直径像素点个数成正比:实际大小 = K1*直径的像素
因此:
需要测距的时候,只需要用同一小球,先修改物体的颜色阈值,让OpenMv能够框出小球,再在一个已知距离点打印物体的像素点长度,就可以用K = 距离*直径的像素
求出关系系数K,然后再通过公式打印出距离。文章来源:https://www.toymoban.com/news/detail-614595.html
需要测量大小的时候,先测出小球的直径,然后在一固定位置(测量任何尺寸必须固定在同一位置)打印小球的直径像素点,再通过实际大小 = K1*直径的像素
公式求出比例系数K1,然后通过公式打印出尺寸大小。文章来源地址https://www.toymoban.com/news/detail-614595.html
二、代码
# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
# 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 = (12, 100, -69, 67, 119, 16)
# 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.
#the value should be measured
#K = 距离*直径的像素
K=544
#实际大小 = K1*直径的像素
K1=0.038
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([yellow_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
size = K1*Lm
h = K1*b[3]
w = K1*b[2]
#print(length)
#print(Lm)
#print(size)
print("高:%s cm,宽:%s",h,w)
#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.
到了这里,关于【OpenMv】测距测尺寸的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!