-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pybricks/parameters/pb_type_color: add distance method #217
base: master
Are you sure you want to change the base?
Conversation
exposes the pbio_color_get_bicone_squared_distance function used in the sensor.color() method.
6080999
to
30e4df1
Compare
Not sure if we should call this BTW, I'm keeping this as draft for now since I'm waiting to rebase this once version metadata reflects alpha release, then I'll also add changelog entry. If we want to merge this, I'll also add a PR for the docs. |
Can you add some end-user Python samples? Is this primarily to map(color1, color2) --> cost, or would you eventually like to be able to pass a custom cost function to |
This is simply intended to expose the function The purpose is to allow more specialized ways of deciding what color is measured depending on what the user needs. # calibrate colors here
colors = [Color.GREEN, Color.RED, Color.BLUE]
current_color = colors[0]
new_color_threshold = 0.7 # only switch to new color, if (dist_to_new_col / dist_to_old_col) < 0.7
while True:
hsv = sensor.hsv()
current_dist = hsv.distance(current_color)
best_dist = current_dist
best_color = None
for color in colors:
dist = hsv.distance(color)
if dist < best_dist and dist < new_color_threshold*current_dist:
best_color = color
best_dist = dist
if best_color is not None:
current_color = best_color
print("new color detected!", current_color)
wait(100) |
Exposing the new HCL-bicone color cost function (#104) makes it possible for users to create their own smarter color detection algorithms, for example by adding a hysteresis. This PR adds it as a
color.distance(other_color)
method to aColor
instance. On movehub, this adds 84 bytes. The function nameget_distance_to
would have added 100 bytes.Example use:
Not sure if this would add less firmware size if it were just a module-level function, but to me it makes a bit more sense as a
Color
method.