How to use OpenMV for distance measurement
Video tutorial 10 - Distance measurement and object size measurement: https://singtown.com/learn/50001/
Video Tutorial 33 - ToF Optical Distance Measurement: https://singtown.com/learn/50539/
The first method:\ Using apriltag, Apriltag can perform 3D positioning. For specific implementation, please refer to our tutorial:\ http://book.openmv.cc/image/apriltag.html
The second method:\ OpenMV uses a monocular camera. If you want to achieve distance measurement, you need to select a reference object and use the size ratio of the reference object to calculate the distance.
This section shares the second method, how to calculate the distance between the camera and the ping-pong ball by the size of the ping-pong ball in the camera.
As we all know, the farther the ping-pong ball is from the camera, the smaller the size of the ping-pong ball in the camera, so the question is?\ What is this relationship?\ (Note: The mathematical geometry problem here only involves the trigonometric function part of high school mathematics. If you don’t want to read it, you can just look at the conclusion directly)
To simplify the problem, let’s look at the picture:\
From the geometric relationship in the camera on the left, we can know:
So there is (Formula 1)
From the geometric relationship in the real environment on the right, we can know:
Substitute (Formula 1) and get (Conclusion Formula):
The above is the relationship we want to know in the end!\ What does this mean?\ Lm on the left side of the equal sign is the length, and Bpix is the pixels occupied by the ball in the camera (pixels of diameter). On the right side of the equal sign, Rm is the actual radius of the ball, Apix is a fixed pixel, and a is half of the viewing angle.\ So! So! So! So! So! So! So!\ This formula tells us:
实际长度和摄像头里的像素成反比
Simplified
距离 = 一个常数/直径的像素
Okay, we already know the relationship, and it is so elegant and simple!\ The specific operation steps are to first measure the value of this constant. Needless to say, first let the ball be 10cm away from the camera, print out the pixel value of the diameter in the camera, and then multiply it to get the value of k!
Then distance = this constant / pixel in the camera, so easy.
Here is the code of 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.
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
print(length)
#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.
The result is this, the distance is printed out through the serial port.