Bird
Raised Fist0
Computer Visionml~10 mins

Optical flow concept in Computer Vision - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the optical flow using Lucas-Kanade method.

Computer Vision
flow = cv2.calcOpticalFlow[1](prev_gray, next_gray, None, lk_params['winSize'], lk_params['maxLevel'], lk_params['criteria'])
Drag options to blanks, or click blank then click option'
APyrLK
BFarneback
CSparse
DDense
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Farneback' instead of 'PyrLK' for sparse optical flow.
Confusing dense and sparse optical flow methods.
2fill in blank
medium

Complete the code to convert a video frame to grayscale for optical flow calculation.

Computer Vision
gray_frame = cv2.cvtColor(frame, [1])
Drag options to blanks, or click blank then click option'
Acv2.COLOR_RGB2GRAY
Bcv2.COLOR_BGR2RGB
Ccv2.COLOR_BGR2GRAY
Dcv2.COLOR_GRAY2BGR
Attempts:
3 left
💡 Hint
Common Mistakes
Using RGB conversion instead of grayscale.
Not converting the frame to grayscale before optical flow.
3fill in blank
hard

Fix the error in the code to calculate the magnitude and angle of optical flow vectors.

Computer Vision
magnitude, angle = cv2.cartToPolar(flow[..., 0], flow[..., [1]], angleInDegrees=True)
Drag options to blanks, or click blank then click option'
A1
B3
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 2 or 3 which are out of bounds.
Using the same index 0 for both components.
4fill in blank
hard

Fill both blanks to create a mask image that visualizes optical flow direction and magnitude.

Computer Vision
mask = np.zeros_like(frame)
mask[..., [1]] = angle / 2
mask[..., [2]] = 255
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing channel indices for HSV color space.
Setting saturation to zero instead of 255.
5fill in blank
hard

Fill all three blanks to convert the mask to BGR color and display it with OpenCV.

Computer Vision
rgb_flow = cv2.cvtColor(mask, [1])
cv2.imshow('Optical Flow', rgb_flow)
cv2.waitKey([2])
cv2.destroyAllWindows()

# Wait time of [3] milliseconds allows frame update
Drag options to blanks, or click blank then click option'
Acv2.COLOR_BGR2HSV
Bcv2.COLOR_HSV2BGR
Ccv2.COLOR_RGB2BGR
Dcv2.COLOR_GRAY2BGR
E0
F1
G30
H100
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong color conversion codes.
Using waitKey(1) or waitKey(100) which may be too fast or slow.

Practice

(1/5)
1. What does optical flow represent in computer vision?
easy
A. The pattern of apparent motion of objects between two images
B. The color distribution in a single image
C. The brightness level of an image
D. The shape of objects detected in an image

Solution

  1. Step 1: Understand the meaning of optical flow

    Optical flow describes how pixels move from one image frame to the next, showing motion.
  2. Step 2: Compare options to definition

    Only The pattern of apparent motion of objects between two images correctly describes motion between images, while others describe unrelated image properties.
  3. Final Answer:

    The pattern of apparent motion of objects between two images -> Option A
  4. Quick Check:

    Optical flow = motion pattern [OK]
Hint: Optical flow = pixel motion between frames [OK]
Common Mistakes:
  • Confusing optical flow with color or brightness
  • Thinking optical flow detects object shapes
  • Assuming optical flow works on single images
2. Which OpenCV function is commonly used to compute dense optical flow using the Farneback method?
easy
A. cv2.calcOpticalFlowPyrLK()
B. cv2.findContours()
C. cv2.threshold()
D. cv2.calcOpticalFlowFarneback()

Solution

  1. Step 1: Recall OpenCV functions for optical flow

    Farneback method is implemented in OpenCV as cv2.calcOpticalFlowFarneback().
  2. Step 2: Eliminate unrelated functions

    Other options relate to tracking points, contours, or thresholding, not Farneback optical flow.
  3. Final Answer:

    cv2.calcOpticalFlowFarneback() -> Option D
  4. Quick Check:

    Farneback method = calcOpticalFlowFarneback [OK]
Hint: Farneback method uses cv2.calcOpticalFlowFarneback() [OK]
Common Mistakes:
  • Confusing Farneback with Lucas-Kanade method
  • Choosing functions unrelated to optical flow
  • Mixing up function names with similar prefixes
3. Given the following Python code using OpenCV, what is the shape of the output variable flow?
import cv2
prev_img = cv2.imread('frame1.png', 0)
next_img = cv2.imread('frame2.png', 0)
flow = cv2.calcOpticalFlowFarneback(prev_img, next_img, None, 0.5, 3, 15, 3, 5, 1.2, 0)
medium
A. A 3D array with shape (height, width, 2)
B. A 2D array with shape (height, width)
C. A 1D array with length equal to number of pixels
D. A scalar value representing average flow

Solution

  1. Step 1: Understand output of Farneback optical flow

    The function returns a 3D array where each pixel has a 2D flow vector (x and y components).
  2. Step 2: Match output shape to options

    This means the output shape is (height, width, 2), representing flow vectors per pixel.
  3. Final Answer:

    A 3D array with shape (height, width, 2) -> Option A
  4. Quick Check:

    Flow shape = (H, W, 2) [OK]
Hint: Optical flow output has two channels for x and y motion [OK]
Common Mistakes:
  • Assuming output is 2D grayscale image
  • Thinking output is a single scalar
  • Confusing flow vector dimensions
4. You wrote this code to compute optical flow but get an error:
flow = cv2.calcOpticalFlowFarneback(prev_img, next_img, 0, 0.5, 3, 15, 3, 5, 1.2, 0)
What is the likely cause of the error?
medium
A. The window size parameter is missing
B. The images must be color, not grayscale
C. The third argument must be None, not 0
D. The pyramid scale parameter is too high

Solution

  1. Step 1: Check the third argument of calcOpticalFlowFarneback

    The third argument is the output flow array or None to create it internally; passing 0 causes an error.
  2. Step 2: Verify correct usage

    Passing None lets OpenCV allocate the output; 0 is invalid and causes a type error.
  3. Final Answer:

    The third argument must be None, not 0 -> Option C
  4. Quick Check:

    Third argument = None [OK]
Hint: Use None as third argument to allocate flow output [OK]
Common Mistakes:
  • Passing 0 instead of None for output flow
  • Using color images instead of grayscale
  • Misunderstanding parameter order
5. You want to track a fast-moving object in a video using optical flow. Which approach helps improve accuracy?
hard
A. Use a larger window size and fewer pyramid levels
B. Use a smaller window size and increase pyramid levels
C. Convert images to color before computing flow
D. Apply thresholding before computing flow

Solution

  1. Step 1: Understand parameters affecting optical flow accuracy

    Smaller window size captures finer motion details; more pyramid levels help track fast motion at multiple scales.
  2. Step 2: Evaluate options for fast-moving object tracking

    Use a smaller window size and increase pyramid levels correctly suggests smaller window and more pyramid levels to improve tracking accuracy.
  3. Final Answer:

    Use a smaller window size and increase pyramid levels -> Option B
  4. Quick Check:

    Smaller window + more pyramids = better fast motion tracking [OK]
Hint: Smaller window + more pyramids track fast motion better [OK]
Common Mistakes:
  • Using large windows that blur fast motion
  • Ignoring pyramid levels for scale changes
  • Preprocessing images incorrectly
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