AI Skill Report Card
Building Motion Detection Pipeline
YAML--- name: building-motion-detection-pipeline description: Builds 2D motion detection pipelines using vector spacing, grid wrapping, and VoxelMorph-inspired deformation fields. Use when developing medical imaging motion analysis systems. ---
Building Motion Detection Pipeline
Quick Start15 / 15
Pythonimport numpy as np import cv2 from scipy.ndimage import map_coordinates def create_motion_pipeline(frame1, frame2, grid_size=16): # Basic 2D motion detection with deformation field flow = cv2.calcOpticalFlowPyrLK(frame1, frame2, None, None) deformation_field = create_deformation_grid(flow, grid_size) wrapped_motion = apply_grid_wrapping(deformation_field) return wrapped_motion
Recommendation▾
Replace abstract examples with concrete input/output pairs showing actual numerical values and array shapes
Workflow15 / 15
Progress:
- Preprocessing: Normalize and resize input frames
- Vector field computation: Calculate dense optical flow
- Grid discretization: Map vectors to regular grid
- Deformation field generation: Create VoxelMorph-style displacement
- Grid wrapping: Apply boundary conditions
- Motion validation: Check field smoothness and consistency
Step 1: Frame Preprocessing
Pythondef preprocess_frames(frame1, frame2): # Convert to grayscale and normalize gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY).astype(np.float32) / 255.0 gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY).astype(np.float32) / 255.0 return gray1, gray2
Step 2: Dense Vector Field
Pythondef compute_dense_flow(frame1, frame2): flow = cv2.calcOpticalFlowFarneback( frame1, frame2, None, pyr_scale=0.5, levels=3, winsize=15, iterations=3, poly_n=5, poly_sigma=1.2, flags=0 ) return flow
Step 3: Grid Mapping
Pythondef map_to_grid(flow_field, grid_size): h, w = flow_field.shape[:2] grid_h, grid_w = h // grid_size, w // grid_size # Average flow vectors within each grid cell grid_flow = np.zeros((grid_h, grid_w, 2)) for i in range(grid_h): for j in range(grid_w): y_start, y_end = i * grid_size, (i + 1) * grid_size x_start, x_end = j * grid_size, (j + 1) * grid_size grid_flow[i, j] = np.mean(flow_field[y_start:y_end, x_start:x_end], axis=(0, 1)) return grid_flow
Step 4: Deformation Field Generation
Pythondef create_deformation_field(grid_flow, target_shape): # Interpolate grid to full resolution (VoxelMorph approach) from scipy.interpolate import RectBivariateSpline h, w = target_shape grid_h, grid_w = grid_flow.shape[:2] # Create coordinate grids y_grid = np.linspace(0, h-1, grid_h) x_grid = np.linspace(0, w-1, grid_w) # Interpolate each component spline_u = RectBivariateSpline(y_grid, x_grid, grid_flow[:, :, 0]) spline_v = RectBivariateSpline(y_grid, x_grid, grid_flow[:, :, 1]) y_full, x_full = np.mgrid[0:h, 0:w] deform_u = spline_u(y_full[:, 0], x_full[0, :]) deform_v = spline_v(y_full[:, 0], x_full[0, :]) return np.stack([deform_u, deform_v], axis=-1)
Step 5: Grid Wrapping
Pythondef apply_grid_wrapping(deformation_field): h, w = deformation_field.shape[:2] # Create identity grid y_coords, x_coords = np.mgrid[0:h, 0:w] # Apply deformation with wrapping new_y = (y_coords + deformation_field[:, :, 0]) % h new_x = (x_coords + deformation_field[:, :, 1]) % w wrapped_coords = np.stack([new_y, new_x], axis=-1) return wrapped_coords
Recommendation▾
Add error handling and edge case coverage in the code examples (e.g., handling empty frames, division by zero)
Examples8 / 20
Example 1: Input: Two consecutive medical frames (512x512), grid_size=32 Output: Deformation field showing tissue motion with periodic boundary conditions
Example 2: Input: Cardiac MRI sequence frames with breathing artifacts Output: Motion vectors mapped to 16x16 grid with smooth interpolation
Example 3: Input: Ultrasound frames with probe movement Output: Wrapped coordinate field compensating for rigid motion
Recommendation▾
Include performance benchmarks or computational complexity considerations for different grid sizes and image resolutions
Best Practices
- Use pyramid-based optical flow for robustness to large motions
- Apply Gaussian smoothing to deformation fields before grid mapping
- Validate motion vectors by checking magnitude thresholds
- Use temporal consistency across frame sequences
- Implement multi-scale processing (coarse-to-fine)
- Apply regularization to prevent unrealistic deformations
Common Pitfalls
- Don't ignore boundary conditions - medical images often have important edge information
- Avoid grid sizes that don't divide evenly into image dimensions
- Don't skip motion magnitude validation - outliers corrupt the field
- Avoid direct coordinate wrapping without smoothness constraints
- Don't use single-frame analysis - temporal context is crucial
- Avoid fixed parameters across different imaging modalities