Bird
Raised Fist0
Drone Programmingprogramming~20 mins

Color-based tracking in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Color Tracking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of color detection thresholding
What is the output of the following code snippet that processes an image to detect a red object using HSV color space thresholding?
Drone Programming
import cv2
import numpy as np

# Create a 3x3 image with one red pixel in HSV
image = np.zeros((3,3,3), dtype=np.uint8)
image[1,1] = [0, 255, 255]  # HSV red

# Define red color range in HSV
lower_red = np.array([0, 100, 100])
upper_red = np.array([10, 255, 255])

# Threshold the image
mask = cv2.inRange(image, lower_red, upper_red)

print(mask)
A
[[  0   0   0]
 [  0 255   0]
 [  0   0   0]]
B
[[255 255 255]
 [255 255 255]
 [255 255 255]]
C
[[0 0 0]
 [0 0 0]
 [0 0 0]]
D
[[0 0 0]
 [255 0 255]
 [0 0 0]]
Attempts:
2 left
💡 Hint
Remember that cv2.inRange returns 255 for pixels within the range and 0 otherwise.
🧠 Conceptual
intermediate
1:30remaining
Understanding color space conversion
Why do drone programs often convert camera images from BGR to HSV color space for color-based tracking?
ABecause HSV images have fewer pixels, improving performance.
BBecause HSV separates color information from brightness, making color detection more robust to lighting changes.
CBecause BGR images are always grayscale and cannot detect colors.
DBecause BGR color space is not supported by drone cameras.
Attempts:
2 left
💡 Hint
Think about how lighting affects color detection.
🔧 Debug
advanced
2:00remaining
Identify the error in color tracking code
What error will this drone tracking code raise when run?
Drone Programming
import cv2
import numpy as np

frame = np.zeros((5,5,3), dtype=np.uint8)
frame[2,2] = [0, 0, 255]  # Red in BGR

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])

mask = cv2.inRange(hsv, lower_blue, upper_blue)

contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

print(contours[0])
ATypeError: 'NoneType' object is not iterable
BSyntaxError: invalid syntax
CNo error, prints contour points
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check what happens if no blue pixels are detected.
📝 Syntax
advanced
1:30remaining
Find the syntax error in color mask creation
Which option contains the correct syntax to create a mask for green color in HSV using cv2.inRange?
Amask = cv2.inRange(hsv, (40, 40, 40), (70, 255, 255))
Bmask = cv2.inRange(hsv, {40, 40, 40}, {70, 255, 255})
Cmask = cv2.inRange(hsv, np.array([40, 40, 40]), np.array([70, 255, 255]))
Dmask = cv2.inRange(hsv, [40, 40, 40], [70, 255, 255])
Attempts:
2 left
💡 Hint
cv2.inRange expects numpy arrays for the color bounds.
🚀 Application
expert
2:30remaining
Calculate the number of detected color blobs
Given the following code that detects yellow blobs in a drone camera frame, how many blobs will be detected?
Drone Programming
import cv2
import numpy as np

frame = np.zeros((6,6,3), dtype=np.uint8)
frame[1,1] = [0, 255, 255]  # Yellow in BGR
frame[4,4] = [0, 255, 255]  # Yellow in BGR
frame[1,4] = [0, 255, 255]  # Yellow in BGR

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_yellow = np.array([20, 100, 100])
upper_yellow = np.array([30, 255, 255])

mask = cv2.inRange(hsv, lower_yellow, upper_yellow)

contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

print(len(contours))
A3
B1
C0
D2
Attempts:
2 left
💡 Hint
Each yellow pixel is isolated; contours detect connected regions.

Practice

(1/5)
1. What is the main purpose of color-based tracking in drone programming?
easy
A. To make the drone follow an object by detecting its color
B. To control the drone's altitude using GPS
C. To improve the drone's battery life
D. To send commands to the drone via remote control

Solution

  1. Step 1: Understand color-based tracking concept

    Color-based tracking means the drone uses color detection to follow an object.
  2. Step 2: Identify the main goal

    The goal is to detect and follow an object by its color, not altitude or battery.
  3. Final Answer:

    To make the drone follow an object by detecting its color -> Option A
  4. Quick Check:

    Color-based tracking = follow object by color [OK]
Hint: Focus on color detection as the tracking method [OK]
Common Mistakes:
  • Confusing color tracking with GPS control
  • Thinking it manages battery or remote commands
  • Mixing color tracking with altitude control
2. Which of the following code snippets correctly initializes a color filter for red in a drone's tracking system?
easy
A. red_filter = {'lower': (0, 255, 255), 'upper': (10, 100, 100)}
B. red_filter = {'lower': [100, 0, 0], 'upper': [255, 10, 10]}
C. red_filter = {'low': (0, 100, 100), 'high': (10, 255, 255)}
D. red_filter = {'lower': (0, 100, 100), 'upper': (10, 255, 255)}

Solution

  1. Step 1: Check correct keys for filter dictionary

    The keys should be 'lower' and 'upper' to define color range.
  2. 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.
  3. Final Answer:

    red_filter = {'lower': (0, 100, 100), 'upper': (10, 255, 255)} -> Option D
  4. Quick Check:

    Correct keys and tuple values = red_filter = {'lower': (0, 100, 100), 'upper': (10, 255, 255)} [OK]
Hint: Look for 'lower' and 'upper' keys with tuple HSV values [OK]
Common Mistakes:
  • Using wrong dictionary keys like 'low' or 'high'
  • Using lists instead of tuples for HSV values
  • Swapping lower and upper HSV ranges
3. Given the following code snippet for detecting a blue object, what will be printed if the detected object's center is at (150, 200)?
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')
medium
A. Blue object detected at (150, 200)
B. SyntaxError
C. No blue object detected
D. RuntimeError

Solution

  1. Step 1: Check the condition on x-coordinate

    Condition checks if 100 <= 150 <= 140, which is false because 150 > 140.
  2. Step 2: Determine which print executes

    Since condition is false, else block runs printing 'No blue object detected'.
  3. Final Answer:

    No blue object detected -> Option C
  4. Quick Check:

    150 not in 100-140 range = No detection [OK]
Hint: Check numeric range conditions carefully [OK]
Common Mistakes:
  • Assuming 150 is within 100 to 140 range
  • Confusing syntax errors with logic errors
  • Ignoring else block output
4. The following code is intended to detect a green object and print its position, but it raises an error. What is the error and how to fix it?
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')
medium
A. SyntaxError due to incorrect dictionary keys; fix keys to 'low' and 'high'
B. TypeError because object_center is None; fix by assigning a tuple to object_center
C. IndexError because green_filter keys are missing; add keys
D. No error; code runs correctly

Solution

  1. Step 1: Identify the error cause

    object_center is None, so accessing object_center[0] causes TypeError.
  2. Step 2: Fix the error

    Assign a tuple like (x, y) to object_center before using it in condition.
  3. Final Answer:

    TypeError because object_center is None; fix by assigning a tuple to object_center -> Option B
  4. Quick Check:

    None used as index causes TypeError [OK]
Hint: Check variable types before indexing [OK]
Common Mistakes:
  • Ignoring NoneType indexing error
  • Changing dictionary keys incorrectly
  • Assuming code runs without error
5. You want the drone to follow a yellow object but only if its detected center's y-coordinate is above 100. Which code snippet correctly implements this condition using color-based tracking?
yellow_filter = {'lower': (20, 100, 100), 'upper': (30, 255, 255)}
object_center = (120, 150)

# Choose the correct condition to check
hard
A. if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100: print('Following yellow object')
B. if yellow_filter['lower'][1] <= object_center[1] <= yellow_filter['upper'][1] and object_center[0] > 100: print('Following yellow object')
C. if yellow_filter['lower'][0] <= object_center[1] <= yellow_filter['upper'][0] and object_center[0] > 100: print('Following yellow object')
D. if yellow_filter['lower'][1] <= object_center[0] <= yellow_filter['upper'][1] and object_center[1] > 100: print('Following yellow object')

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100: print('Following yellow object') -> Option A
  4. Quick 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]
Hint: Match hue with x, check y > 100 separately [OK]
Common Mistakes:
  • Mixing x and y coordinates in conditions
  • Using wrong indices for hue or saturation
  • Checking y-coordinate against hue range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard