Bird
Raised Fist0
Drone Programmingprogramming~20 mins

Optical flow for indoor positioning in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Optical Flow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Calculate displacement from optical flow data
Given the optical flow vectors detected by a drone's downward camera, what is the resulting displacement in meters after processing the flow data for 1 second?

Assume the drone's altitude is 2 meters, and the optical flow sensor reports pixel shifts that translate to velocity components as shown in the code.
Drone Programming
altitude = 2.0  # meters
flow_x = 0.1  # meters/second
flow_y = -0.05  # meters/second
time_seconds = 1

displacement_x = flow_x * time_seconds
displacement_y = flow_y * time_seconds

print(f"Displacement X: {displacement_x} m")
print(f"Displacement Y: {displacement_y} m")
ADisplacement X: 0.0 m\nDisplacement Y: 0.0 m
BDisplacement X: 2.0 m\nDisplacement Y: -1.0 m
CDisplacement X: 0.2 m\nDisplacement Y: -0.1 m
DDisplacement X: 0.1 m\nDisplacement Y: -0.05 m
Attempts:
2 left
💡 Hint
Multiply the velocity components by the time to get displacement.
🧠 Conceptual
intermediate
1:30remaining
Understanding optical flow limitations indoors
Which of the following is the main challenge when using optical flow sensors for indoor positioning of drones?
AStrong GPS signals interfering with optical sensors
BLack of distinct visual features on uniform surfaces
CExcessive sunlight causing sensor overheating
DHigh altitude causing blurred images
Attempts:
2 left
💡 Hint
Think about what optical flow needs to detect movement.
🔧 Debug
advanced
2:30remaining
Fix the error in optical flow velocity calculation
The following code attempts to calculate the velocity from optical flow pixel shifts and altitude, but it raises an error. Identify the cause and select the correct fixed code.
Drone Programming
pixel_shift_x = 15
pixel_shift_y = 10
altitude = 3.0
focal_length = 2.5
velocity_x = pixel_shift_x * altitude / focal_length
velocity_y = pixel_shift_y * altitude / focal_length

print(f"Velocity X: {velocity_x}")
print(f"Velocity Y: {velocity_y}")
A
velocity_x = pixel_shift_x * altitude / focal_length
velocity_y = pixel_shift_y * altitude / focal_length
B
velocity_x = pixel_shift_x * altitude // focal_length
velocity_y = pixel_shift_y * altitude // focal_length
C
velocity_x = pixel_shift_x / altitude / focal_length
velocity_y = pixel_shift_y / altitude / focal_length
D
velocity_x = pixel_shift_x * altitude * focal_length
velocity_y = pixel_shift_y * altitude * focal_length
Attempts:
2 left
💡 Hint
Check the operators used for division and multiplication.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in optical flow data processing
Which option contains a syntax error when trying to create a dictionary of pixel shifts mapped to their velocities?
Drone Programming
pixel_shifts = {'x': 12, 'y': 8}
altitude = 2.5
focal_length = 3.0
velocities = {axis: pixel_shifts[axis] * altitude / focal_length for axis in pixel_shifts}
print(velocities)
Avelocities = {axis: pixel_shifts[axis] * altitude / focal_length for axis in pixel_shifts}
Bvelocities = {axis: pixel_shifts[axis] * altitude / focal_length for axis in pixel_shifts.keys()}
Cvelocities = {axis: pixel_shifts[axis] * altitude / focal_length for axis pixel_shifts}
Dvelocities = {axis: pixel_shifts[axis] * altitude / focal_length for axis in pixel_shifts.items()}
Attempts:
2 left
💡 Hint
Check the syntax of the for loop in the dictionary comprehension.
🚀 Application
expert
3:00remaining
Calculate total distance traveled using optical flow data
A drone collects optical flow velocity data every second for 5 seconds as follows (in meters/second): [0.1, 0.15, 0.12, 0.13, 0.20]. What is the total distance traveled in meters after 5 seconds?
A0.70
B0.75
C0.65
D0.60
Attempts:
2 left
💡 Hint
Sum all velocities and multiply by the time interval (1 second each).

Practice

(1/5)
1. What is the main purpose of using optical flow in indoor drone positioning?
easy
A. To track the drone's movement by analyzing camera images
B. To connect the drone to GPS satellites
C. To control the drone's battery usage
D. To measure the drone's altitude using sound waves

Solution

  1. Step 1: Understand optical flow concept

    Optical flow uses camera images to detect movement by comparing changes between frames.
  2. Step 2: Identify its use in indoor positioning

    Since GPS doesn't work well indoors, optical flow helps track position by visual movement detection.
  3. Final Answer:

    To track the drone's movement by analyzing camera images -> Option A
  4. Quick Check:

    Optical flow = movement tracking indoors [OK]
Hint: Optical flow tracks movement visually, not with GPS indoors [OK]
Common Mistakes:
  • Confusing optical flow with GPS tracking
  • Thinking optical flow measures altitude
  • Assuming optical flow controls battery
2. Which of the following is the correct Python syntax to calculate optical flow using OpenCV's calcOpticalFlowPyrLK function?
easy
A. flow = cv2.calcOpticalFlowPyrLK(prev_img, next_img, prev_pts, None)
B. flow = cv2.calcOpticalFlowPyrLK(prev_pts, prev_img, next_img, None)
C. flow = cv2.calcOpticalFlowPyrLK(next_img, prev_img, None, prev_pts)
D. flow = cv2.calcOpticalFlowPyrLK(None, prev_img, prev_pts, next_img)

Solution

  1. Step 1: Recall function parameter order

    The function calcOpticalFlowPyrLK expects parameters in order: previous image, next image, previous points, and next points (usually None to compute).
  2. Step 2: Match parameters to options

    flow = cv2.calcOpticalFlowPyrLK(prev_img, next_img, prev_pts, None) matches this order correctly; others mix parameter positions incorrectly.
  3. Final Answer:

    flow = cv2.calcOpticalFlowPyrLK(prev_img, next_img, prev_pts, None) -> Option A
  4. Quick Check:

    Correct parameter order = flow = cv2.calcOpticalFlowPyrLK(prev_img, next_img, prev_pts, None) [OK]
Hint: Remember: prev_img, next_img, prev_pts, None order [OK]
Common Mistakes:
  • Swapping image and points parameters
  • Passing None in wrong position
  • Confusing previous and next images
3. Given the following Python snippet for updating drone position using optical flow displacement:
dx, dy = 5, -3
position = [10, 10]
position[0] += dx
position[1] += dy
print(position)

What will be the output?
medium
A. [15, -3]
B. [5, 13]
C. [10, 10]
D. [15, 7]

Solution

  1. Step 1: Understand position update

    Initial position is [10, 10]. Adding dx=5 to x gives 15. Adding dy=-3 to y gives 7.
  2. Step 2: Calculate final position

    Updated position is [15, 7].
  3. Final Answer:

    [15, 7] -> Option D
  4. Quick Check:

    10+5=15 and 10-3=7 [OK]
Hint: Add dx to x and dy to y for new position [OK]
Common Mistakes:
  • Mixing x and y updates
  • Forgetting to add dy as negative
  • Printing initial position instead of updated
4. Identify the error in this Python code snippet for calculating optical flow displacement:
prev_pts = np.array([[100, 150]], dtype=np.float32)
next_pts = np.array([[105, 145]], dtype=np.float32)
dx = next_pts[0][0] - prev_pts[0]
dy = next_pts[0][1] - prev_pts[1]
medium
A. Wrong data type for numpy arrays
B. Missing import statement for numpy
C. Incorrect indexing of prev_pts causing IndexError
D. Using subtraction instead of addition

Solution

  1. Step 1: Check indexing of prev_pts

    prev_pts is a 2D array; prev_pts[0] is [100, 150], but prev_pts[1] does not exist (index error).
  2. Step 2: Identify cause of error

    Accessing prev_pts[1] causes an IndexError; correct indexing should be prev_pts[0][1].
  3. Final Answer:

    Incorrect indexing of prev_pts causing IndexError -> Option C
  4. Quick Check:

    Indexing 2D arrays needs two indices [OK]
Hint: Use two indices for 2D arrays like prev_pts[0][1] [OK]
Common Mistakes:
  • Using single index on 2D numpy arrays
  • Confusing dx and dy calculations
  • Ignoring numpy import errors
5. You want to improve indoor drone positioning by combining optical flow displacement with altitude data from a barometer. Which approach best integrates these two data sources in Python?
hard
A. Store altitude as a separate variable and never update position
B. Create a dictionary with keys 'x', 'y', 'altitude' updated each frame
C. Use optical flow data only and ignore altitude for simplicity
D. Replace optical flow with barometer readings entirely

Solution

  1. Step 1: Understand data integration needs

    Combining horizontal movement (optical flow) and vertical position (altitude) requires a unified data structure.
  2. Step 2: Choose appropriate data structure

    A dictionary with keys 'x', 'y', and 'altitude' allows updating all position components together each frame.
  3. Final Answer:

    Create a dictionary with keys 'x', 'y', 'altitude' updated each frame -> Option B
  4. Quick Check:

    Combine data in one structure for full 3D position [OK]
Hint: Use one dictionary to track x, y, and altitude together [OK]
Common Mistakes:
  • Ignoring altitude data
  • Keeping altitude separate without integration
  • Replacing optical flow instead of combining