Multi Color Blob Tracking Example
This example shows off multi color blob tracking using the OpenMV Cam.
Python snapshot Examples, sensor.snapshot Python Examples - HotExamples
import sensor, image, time, math
Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
L is lightness (0:100) A is Red (+)/Green (-) B is Yellow (+)/Blue (-)
Range varies L -150:150 A or B -127:127 or just +/- 100
The below thresholds track in general red/green things. You may wish to tune them…
thresholds = [(10, 48, -10, 10, -10, 10), # generic_red_thresholds
(10, 48, -10, 10, -10, 10), # generic_green_thresholds
(10, 48, -10, 10, -10, 10)] # generic_blue_thresholds
You may pass up to 16 thresholds above. However, it’s not really possible to segment any
scene with 16 thresholds before color thresholds start to overlap heavily.
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # modulates min blob size
sensor.set_windowing((240, 240)) # Set 240x240 window can apply offsets too
sensor.set_brightness(0)
sensor.set_saturation(0)
sensor.set_gainceiling(8)
sensor.set_contrast(2)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking no grayscale use
exposure time is 0 to 10000uS
sensor.set_auto_exposure(False, exposure_us = int(300)) # exposure_us = int(current_exposure_time_in_microseconds * EXPOSURE_TIME_SCALE))
gain can be limited by this setting values 2/4/8/16/32/64/128
see NXP AN13243 this may be a ‘clone’ but a very good one !
sensor.set_gainceiling(8)
The gain db ceiling maxes out at about 24 db for the OV7725 sensor.
values 2/4/8/16/32/64/128
sensor.set_auto_gain(False, 4) # gain_db_ceiling = 16.0) # Default gain.
Change this to False to undo the flip.
sensor.set_vflip(False)
Change this to False to undo the mirror.
sensor.set_hmirror(False)
sensor.set_brightness(3) # range is -3 : +3
sensor.set_colorbar(0) # Colorbar display in FrameBuffer
sensor.skip_frames(time = 300) # Use this after every register change 10 frames 300mS default
clock = time.clock()
print some register & configuration settings
print(“New exposure == %d” % sensor.get_exposure_us())
print(“Sensor ID == %d” % sensor.get_id())
print(“Sensor Height == %d” % sensor.height())
print(“Sensor Width == %d” % sensor.width())
returns a tuple (float,float,float) does not work with MT9M114 sensor not on H7 R2
set with same values is how white balance is maintained
What does "sensor.get_rgb_gain_db()" do exactly?
print(sensor.get_rgb_gain_db())
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. Don’t set “merge=True” becuase that will merge blobs which we don’t want here.
while(True):
clock.tick()
event_count = 0 # actually a blob counter
img = sensor.snapshot()
for blob in img.find_blobs(thresholds, pixels_threshold=10, area_threshold=70):
event_count = event_count + 1
# These values depend on the blob not being circular - otherwise they will be shaky.
if blob.elongation() > 0.5:
img.draw_edges(blob.min_corners(), color=(55,255,10))
# img.draw_line(blob.major_axis_line(), color=(255,255,0))
# img.draw_line(blob.minor_axis_line(), color=(255,255,0))
# These values are stable all the time.
# img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
# Note - the blob rotation is unique to 0-180 only.
# img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=10)
print(“FPS == %f” % clock.fps()) # looks like %d is for integers
print(blob.cx(), blob.cy())
print(“BLOBs == %d” % event_count) # Use %f for floats
# print(event_count)