Back to Posts

Acknowledgment

Special thanks to:

Eric Xie for finding optimizations in the tracking algorithm; and

Andy Wen for debugging issues with frame processing.

Pendulum Motion Analysis

Tracking a pendulum using computer vision and mathematical modeling with OpenCV.

Color Detection of the Bob using Cosine Similarity

The pendulum bob is detected using cosine similarity in RGB space. Instead of thresholding in HSV, we compare each pixel color to a reference bob color $$C_r = (R_r, G_r, B_r)$$ using the cosine similarity formula:

$$S = \frac{C_r \cdot C_p}{\|C_r\| \|C_p\|}$$

where $$ C_p = (R_p, G_p, B_p) $$ is the color of a pixel. Pixels with similarity above a threshold $$ S_{\text{min}} $$ are considered part of the bob. The centroid $$ (x_c, y_c) $$ is then calculated as:

$$ x_c = \frac{\sum x_i}{N}, \quad y_c = \frac{\sum y_i}{N} $$

Finding the Pivot Using Least Squares Circle Fitting

The pendulum moves along a circular arc, meaning the pivot (origin) $$(x_0, y_0)$$ is the center of a fitted circle. We solve for $$ (x_0, y_0) $$ using the objective function:

$$ E^* = \arg\min_{x_0, y_0} \sum_{i} \left( (x_i - x_0)^2 + (y_i - y_0)^2 - R^2 \right)^2 $$

where the error $$E^*$$ is minimized using non-linear least squares optimization. The detected pivot is validated by ensuring radius consistency across frames:

$$ R_i = \sqrt{(x_i - x_0)^2 + (y_i - y_0)^2} \approx R_{\text{avg}} $$

Demonstration

Demonstration of the pendulum tracking algorithm.

Data Processing & Mathematical Fitting

The CSV data is processed to compute period times and track the pendulum's oscillation. The analysis scripts also fit exponential functions to describe damping effects.

A quadratic function is used to fit the period data, confirming theoretical predictions.

Detailed analysis scripts such as dataAnalysis.py, expoFit.py, and periodTime.py are available in the GitHub repository.

Selected Graphs

Below are some of the graphs generated from the data analysis:

Exponential Fitting of Pendulum's Decay with Quartic Period Fitting

Exponential fitting of the pendulum's decay with quartic period fitting.

Period Time Analysis of Pendulum at different angles

Period time analysis of the pendulum at different angles.

Peak Detection of Pendulum's Motion

Peak detection of the pendulum's motion.