0
0
Drone Programmingprogramming~20 mins

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

Choose your learning style9 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.