Color-based tracking in Drone Programming - Time & Space Complexity
When a drone tracks colors, it checks many pixels to find the right one.
We want to know how the time to find colors grows as the image size grows.
Analyze the time complexity of the following code snippet.
for pixel in image_pixels:
if pixel.color == target_color:
drone.move_to(pixel.position)
break
This code looks at each pixel in the image to find the first pixel matching the target color, then moves the drone there.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each pixel's color in the image.
- How many times: Up to once per pixel, until the target color is found.
As the number of pixels grows, the drone may check more pixels to find the color.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 color checks |
| 100 | Up to 100 color checks |
| 1000 | Up to 1000 color checks |
Pattern observation: The number of checks grows roughly in direct proportion to the number of pixels.
Time Complexity: O(n)
This means the time to find the color grows linearly with the number of pixels in the image.
[X] Wrong: "The drone checks only one pixel no matter the image size."
[OK] Correct: The drone may need to check many pixels before finding the target color, so time grows with image size.
Understanding how checking pixels scales helps you explain how your code handles bigger images efficiently.
"What if the drone had to find all pixels of the target color instead of stopping at the first? How would the time complexity change?"
Practice
Solution
Step 1: Understand color-based tracking concept
Color-based tracking means the drone uses color detection to follow an object.Step 2: Identify the main goal
The goal is to detect and follow an object by its color, not altitude or battery.Final Answer:
To make the drone follow an object by detecting its color -> Option AQuick Check:
Color-based tracking = follow object by color [OK]
- Confusing color tracking with GPS control
- Thinking it manages battery or remote commands
- Mixing color tracking with altitude control
Solution
Step 1: Check correct keys for filter dictionary
The keys should be 'lower' and 'upper' to define color range.Step 2: Verify value types and ranges
Values must be tuples with HSV ranges; red_filter = {'lower': (0, 100, 100), 'upper': (10, 255, 255)} uses correct keys and tuples.Final Answer:
red_filter = {'lower': (0, 100, 100), 'upper': (10, 255, 255)} -> Option DQuick Check:
Correct keys and tuple values = red_filter = {'lower': (0, 100, 100), 'upper': (10, 255, 255)} [OK]
- Using wrong dictionary keys like 'low' or 'high'
- Using lists instead of tuples for HSV values
- Swapping lower and upper HSV ranges
blue_filter = {'lower': (100, 150, 0), 'upper': (140, 255, 255)}
object_center = (150, 200)
if 100 <= object_center[0] <= 140:
print('Blue object detected at', object_center)
else:
print('No blue object detected')Solution
Step 1: Check the condition on x-coordinate
Condition checks if 100 <= 150 <= 140, which is false because 150 > 140.Step 2: Determine which print executes
Since condition is false, else block runs printing 'No blue object detected'.Final Answer:
No blue object detected -> Option CQuick Check:
150 not in 100-140 range = No detection [OK]
- Assuming 150 is within 100 to 140 range
- Confusing syntax errors with logic errors
- Ignoring else block output
green_filter = {'lower': (40, 50, 50), 'upper': (80, 255, 255)}
object_center = None
if green_filter['lower'][0] <= object_center[0] <= green_filter['upper'][0]:
print('Green object at', object_center)
else:
print('No green object detected')Solution
Step 1: Identify the error cause
object_center is None, so accessing object_center[0] causes TypeError.Step 2: Fix the error
Assign a tuple like (x, y) to object_center before using it in condition.Final Answer:
TypeError because object_center is None; fix by assigning a tuple to object_center -> Option BQuick Check:
None used as index causes TypeError [OK]
- Ignoring NoneType indexing error
- Changing dictionary keys incorrectly
- Assuming code runs without error
yellow_filter = {'lower': (20, 100, 100), 'upper': (30, 255, 255)}
object_center = (120, 150)
# Choose the correct condition to checkSolution
Step 1: Understand HSV and coordinate roles
yellow_filter keys 'lower' and 'upper' define hue range at index 0; object_center[0] is x, object_center[1] is y.Step 2: Check condition for hue and y-coordinate
Hue check uses object_center[0] between lower and upper hue; y-coordinate must be > 100.Final Answer:
if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100: print('Following yellow object') -> Option AQuick Check:
Hue range on x and y > 100 condition = if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100: print('Following yellow object') [OK]
- Mixing x and y coordinates in conditions
- Using wrong indices for hue or saturation
- Checking y-coordinate against hue range
Ready to prove it?
Pass the test and this pattern moves unknown
