mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge pull request #174 from kwagyeman/color_tracking
Update color tracking scripts.
This commit is contained in:
commit
25005eb80e
@ -1,7 +1,7 @@
|
||||
# Line Following Example
|
||||
# Black Grayscale Line Following Example
|
||||
#
|
||||
# Making a line following robot requires a lot of effort. This example script
|
||||
# shows how to do the computer vision part of the line following robot. You
|
||||
# shows how to do the machine vision part of the line following robot. You
|
||||
# can use the output from this script to drive a differential drive robot to
|
||||
# follow a line. This script just generates a single turn value that tells
|
||||
# your robot to go left or right.
|
||||
@ -12,8 +12,8 @@
|
||||
|
||||
import sensor, image, time, math
|
||||
|
||||
# Tracks a white line. Use [(0, 64)] for a tracking a black line.
|
||||
GRAYSCALE_THRESHOLD = [(128, 255)]
|
||||
# Tracks a black line. Use [(128, 255)] for a tracking a white line.
|
||||
GRAYSCALE_THRESHOLD = [(0, 64)]
|
||||
|
||||
# Each roi is (x, y, w, h). The line detection algorithm will try to find the
|
||||
# centroid of the largest blob in each roi. The x position of the centroids
|
||||
@ -25,16 +25,17 @@ ROIS = [ # [ROI, weight]
|
||||
(0, 000, 160, 20, 0.1)
|
||||
]
|
||||
|
||||
# Compute the weight divisor
|
||||
# Compute the weight divisor (we're computing this so you don't have to make weights add to 1).
|
||||
weight_sum = 0
|
||||
for r in ROIS: weight_sum += r[4]
|
||||
for r in ROIS: weight_sum += r[4] # r[4] is the roi weight.
|
||||
|
||||
# Camera setup...
|
||||
sensor.reset() # Initialize the camera sensor.
|
||||
sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
|
||||
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.
|
||||
sensor.skip_frames(30) # Let new settings take affect.
|
||||
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() # Tracks FPS.
|
||||
|
||||
while(True):
|
||||
@ -43,23 +44,22 @@ while(True):
|
||||
|
||||
centroid_sum = 0
|
||||
for r in ROIS:
|
||||
merged_blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=r[0:4], merge=True) # r[0:4] is roi tuple.
|
||||
if merged_blobs:
|
||||
blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=r[0:4], merge=True) # r[0:4] is roi tuple.
|
||||
if blobs:
|
||||
# Find the index of the blob with the most pixels.
|
||||
most_pixels = 0
|
||||
largest_blob = 0
|
||||
for i in range(len(merged_blobs)):
|
||||
if merged_blobs[i][4] > most_pixels:
|
||||
most_pixels = merged_blobs[i][4] # [4] is pixels.
|
||||
for i in range(len(blobs)):
|
||||
if blobs[i].pixels() > most_pixels:
|
||||
most_pixels = blobs[i].pixels()
|
||||
largest_blob = i
|
||||
|
||||
# Draw a rect around the blob.
|
||||
img.draw_rectangle(merged_blobs[largest_blob][0:4]) # rect
|
||||
img.draw_cross(merged_blobs[largest_blob][5], # cx
|
||||
merged_blobs[largest_blob][6]) # cy
|
||||
img.draw_rectangle(blobs[largest_blob].rect())
|
||||
img.draw_cross(blobs[largest_blob].cx(),
|
||||
blobs[largest_blob].cy())
|
||||
|
||||
# [5] of the blob is the x centroid - r[4] is the weight.
|
||||
centroid_sum += merged_blobs[largest_blob][5] * r[4]
|
||||
centroid_sum += blobs[largest_blob].cx() * r[4] # r[4] is the roi weight.
|
||||
|
||||
center_pos = (centroid_sum / weight_sum) # Determine center of line.
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
# Blob Detection Example
|
||||
#
|
||||
# This example shows off how to use the find_blobs function to find color
|
||||
# blobs in the image. This example in particular looks for dark green objects.
|
||||
|
||||
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...
|
||||
green_threshold = ( 0, 80, -70, -10, -0, 30)
|
||||
# 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.
|
||||
|
||||
while(True):
|
||||
clock.tick() # Track elapsed milliseconds between snapshots().
|
||||
img = sensor.snapshot() # Take a picture and return the image.
|
||||
|
||||
for b in img.find_blobs([green_threshold], area_threshold=30, pixels_threshold=30, merge=True):
|
||||
# Draw a rect around the blob.
|
||||
img.draw_rectangle(b.rect()) # rect
|
||||
img.draw_cross(b.cx(), b.cy()) # cx, cy
|
||||
|
||||
print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
|
||||
# connected to your computer. The FPS should increase once disconnected.
|
||||
@ -0,0 +1,30 @@
|
||||
# IR Beacon Grayscale Tracking Example
|
||||
#
|
||||
# This example shows off IR beacon Grayscale tracking using the OpenMV Cam.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
thresholds = (255, 255) # thresholds for bright white light from IR.
|
||||
|
||||
sensor.reset()
|
||||
sensor.set_pixformat(sensor.GRAYSCALE)
|
||||
sensor.set_framesize(sensor.VGA)
|
||||
sensor.set_windowing((240, 240)) # 240x240 center pixels of VGA
|
||||
sensor.skip_frames(30)
|
||||
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], pixels_threshold=200, area_threshold=200, merge=True):
|
||||
ratio = blob.w() / blob.h()
|
||||
if (ratio >= 0.5) and (ratio <= 1.5): # filter out non-squarish blobs
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
print(clock.fps())
|
||||
30
usr/examples/10-Color-Tracking/ir_beacon_rgb565_tracking.py
Normal file
30
usr/examples/10-Color-Tracking/ir_beacon_rgb565_tracking.py
Normal file
@ -0,0 +1,30 @@
|
||||
# IR Beacon RGB565 Tracking Example
|
||||
#
|
||||
# This example shows off IR beacon RGB565 tracking using the OpenMV Cam.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
thresholds = (100, 100, 0, 0, 0, 0) # thresholds for bright white light from IR.
|
||||
|
||||
sensor.reset()
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
sensor.set_framesize(sensor.VGA)
|
||||
sensor.set_windowing((240, 240)) # 240x240 center pixels of VGA
|
||||
sensor.skip_frames(30)
|
||||
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], pixels_threshold=200, area_threshold=200, merge=True):
|
||||
ratio = blob.w() / blob.h()
|
||||
if (ratio >= 0.5) and (ratio <= 1.5): # filter out non-squarish blobs
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
print(clock.fps())
|
||||
@ -1,45 +0,0 @@
|
||||
# Marker Tracking Example
|
||||
#
|
||||
# This example shows how to use the find_markers function to merge blobs for
|
||||
# different colors into one blob that represents a marker.
|
||||
#
|
||||
# Each blob that find_blobs returns has a bit in a bitmask set for the color
|
||||
# that blob was produced by which was passed to find_blobs. E.g. if you pass
|
||||
# find blobs 3 colors then you'll get blobs with possibly a color value of
|
||||
# (2^0), (2^1), or (2^2). These color values can be or'ed togheter because
|
||||
# they are a single bit each to represent a mutli-colored blob which you
|
||||
# can then classify as a marker.
|
||||
|
||||
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. Additionally, if
|
||||
# you want to track more than 2 colors you need to set the boundaries for them
|
||||
# very narrowly. If you try to track... generally red, green, and blue then
|
||||
# you will end up just tracking everything which you don't want.
|
||||
red_threshold = ( 40, 60, 60, 90, 50, 70)
|
||||
blue_threshold = ( 0, 20, -10, 30, -60, 10)
|
||||
# You may need to tweak the above settings for tracking red and blue 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.
|
||||
|
||||
while(True):
|
||||
clock.tick() # Track elapsed milliseconds between snapshots().
|
||||
img = sensor.snapshot() # Take a picture and return the image.
|
||||
|
||||
# margin=2 means blobs can be 2 pixels away from each other to merge
|
||||
for b in img.find_blobs([red_threshold, blue_threshold], area_threshold=30, pixels_threhsold=30, merge=True, margin=2):
|
||||
# Draw a rect around the blob.
|
||||
img.draw_rectangle(b[0:4]) # rect
|
||||
img.draw_cross(b[5], b[6]) # cx, cy
|
||||
# Draw the color label. b[8] is the color label.
|
||||
img.draw_string(b[0]+2, b[1]+2, "%d" % b[8])
|
||||
|
||||
print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
|
||||
# connected to your computer. The FPS should increase once disconnected.
|
||||
33
usr/examples/10-Color-Tracking/multi_color_blob_tracking.py
Normal file
33
usr/examples/10-Color-Tracking/multi_color_blob_tracking.py
Normal file
@ -0,0 +1,33 @@
|
||||
# Multi Color Blob Tracking Example
|
||||
#
|
||||
# This example shows off multi color blob tracking using the OpenMV Cam.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
|
||||
# The below thresholds track in general red/green 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, 15, 0, 40, -80, -20)] # 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.RGB565)
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
sensor.skip_frames(30)
|
||||
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. Don't set "merge=True" becuase that will merge blobs which we don't want here.
|
||||
|
||||
while(True):
|
||||
clock.tick()
|
||||
img = sensor.snapshot()
|
||||
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
print(clock.fps())
|
||||
49
usr/examples/10-Color-Tracking/multi_color_code_tracking.py
Normal file
49
usr/examples/10-Color-Tracking/multi_color_code_tracking.py
Normal file
@ -0,0 +1,49 @@
|
||||
# Multi Color Code Tracking Example
|
||||
#
|
||||
# This example shows off multi color code tracking using the OpenMV Cam.
|
||||
#
|
||||
# A color code is a blob composed of two or more colors. The example below will
|
||||
# only track colored objects which have two or more the colors below in them.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
|
||||
# The below thresholds track in general red/green things. You may wish to tune them...
|
||||
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0)
|
||||
(30, 100, -64, -8, -32, 32), # generic_green_thresholds -> index is 1 so code == (1 << 1)
|
||||
(0, 15, 0, 40, -80, -20)] # generic_blue_thresholds -> index is 2 so code == (1 << 2)
|
||||
# Codes are or'ed together when "merge=True" for "find_blobs".
|
||||
|
||||
sensor.reset()
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
sensor.skip_frames(30)
|
||||
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" must be set to merge overlapping color blobs for color codes.
|
||||
|
||||
while(True):
|
||||
clock.tick()
|
||||
img = sensor.snapshot()
|
||||
for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True):
|
||||
if blob.code() == 3: # r/g code
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
img.draw_string(blob.x() + 2, blob.y() + 2, "r/g")
|
||||
if blob.code() == 5: # r/b code
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
img.draw_string(blob.x() + 2, blob.y() + 2, "r/b")
|
||||
if blob.code() == 6: # g/b code
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
img.draw_string(blob.x() + 2, blob.y() + 2, "g/b")
|
||||
if blob.code() == 7: # r/g/b code
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
img.draw_string(blob.x() + 2, blob.y() + 2, "r/g/b")
|
||||
print(clock.fps())
|
||||
35
usr/examples/10-Color-Tracking/single_color_code_tracking.py
Normal file
35
usr/examples/10-Color-Tracking/single_color_code_tracking.py
Normal file
@ -0,0 +1,35 @@
|
||||
# Single Color Code Tracking Example
|
||||
#
|
||||
# This example shows off single color code tracking using the OpenMV Cam.
|
||||
#
|
||||
# A color code is a blob composed of two or more colors. The example below will
|
||||
# only track colored objects which have both the colors below in them.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
|
||||
# The below thresholds track in general red/green things. You may wish to tune them...
|
||||
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0)
|
||||
(30, 100, -64, -8, -32, 32)] # generic_green_thresholds -> index is 1 so code == (1 << 1)
|
||||
# Codes are or'ed together when "merge=True" for "find_blobs".
|
||||
|
||||
sensor.reset()
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
sensor.skip_frames(30)
|
||||
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" must be set to merge overlapping color blobs for color codes.
|
||||
|
||||
while(True):
|
||||
clock.tick()
|
||||
img = sensor.snapshot()
|
||||
for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True):
|
||||
if blob.code() == 3: # r/g code == (1 << 1) | (1 << 0)
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
print(clock.fps())
|
||||
@ -0,0 +1,29 @@
|
||||
# Single Color Grayscale Blob Tracking Example
|
||||
#
|
||||
# This example shows off single color grayscale tracking using the OpenMV Cam.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
# Color Tracking Thresholds (Grayscale Min, Grayscale Max)
|
||||
# The below grayscale threshold is set to only find extremely bright white areas.
|
||||
thresholds = (245, 255)
|
||||
|
||||
sensor.reset()
|
||||
sensor.set_pixformat(sensor.GRAYSCALE)
|
||||
sensor.set_framesize(sensor.VGA)
|
||||
sensor.skip_frames(30)
|
||||
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], pixels_threshold=100, area_threshold=100, merge=True):
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
print(clock.fps())
|
||||
@ -0,0 +1,33 @@
|
||||
# Single Color RGB565 Blob Tracking Example
|
||||
#
|
||||
# This example shows off single color RGB565 tracking using the OpenMV Cam.
|
||||
|
||||
import sensor, image, time
|
||||
|
||||
threshold_index = 0 # 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(30)
|
||||
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):
|
||||
img.draw_rectangle(blob.rect())
|
||||
img.draw_cross(blob.cx(), blob.cy())
|
||||
print(clock.fps())
|
||||
Loading…
Reference in New Issue
Block a user