Hi, um, this whole forum post is about how to do this. Rather not explain it again. If you have a very specific question on an issue you are having I’m happy to answer.
I used this code
Please, can you check this?
Maybe I have any little mistakes at the code
# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.
import sensor, image, time, math
from pyb import Servo
x_pos = 1000 # default
y_pos = 1000 # default
x_min = 600
x_max = 2800
y_max = 1800
y_min = 900
x_gain = +1.00 # You have to tweak this value to stablize the control loop.
# You also may need to invert the value if the system goes
# in the wrong direction.
y_gain = +1.00 # You have to tweak this value to stablize the control loop.
# You also may need to invert the value if the system goes
# in the wrong direction.
xServo = Servo(1)
yServo = Servo(2)
xServo.pulse_width(x_pos)
yServo.pulse_width(y_pos)
threshold_index = 1 # 0 for red, 1 for green, 2 for blue
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
(30, 100, -64, -8, -32, 32), # generic_green_thresholds
(0, 30, 0, 64, -128, 0)] # generic_blue_thresholds
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()
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
x = blob.cx()
y = blob.cy()
x_error = x - (img.width()/2)
y_error = y - (img.height()/2)
x_pos += x_error * x_gain
y_pos += y_error * y_gain
# Clamp output between min and max
if (x_pos > x_max):
x_pos = x_max
if (x_pos < x_min):
x_pos = x_min
# Clamp output between min and max
if (y_pos > y_max):
y_pos = y_max
if (y_pos < y_min):
y_pos = y_min
xServo.pulse_width(int(x_pos))
yServo.pulse_width(int(y_pos))
# Note - the blob rotation is unique to 0-180 only.
img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
print(clock.fps())
Yeah, that’s the basic idea. It looks like you are doing exactly the right thing.
Thank you very much! Glad your reply
How can improve object tracking?
In my case, the tracking is rough, not smoothed
Can you please tell me what and how to fix it?
Glad to try any ideas!
Hi dos, I don’t know how to do everything.
If you want it to be smoother you should filter the input blob location through an average filter and then reduce the x_gain and y_gain so the system tracks more slowly.