This Python project is designed to analyze the harmonic oscillation of an object using computer vision techniques. It leverages various libraries, including OpenCV for video feed processing, SciPy for data analysis, NumPy for numerical operations, and PyQt5 for creating a graphical user interface (GUI). The project detects and tracks an object in a video feed, extracts its motion data, fits it to a damped oscillation function, and calculates physical parameters such as pendulum length and pivot point location.
It is highly advisable for you, to thoroughly go through the blog. Doing so will greatly facilitate the project setup process and enable a better grasp of the user interface (UI). Your understanding of this section is pivotal in ensuring the efficient configuration of the project and in comprehending the UI.
- Computer Vision-Based Harmonic Oscillation Analysis
- Overview
- Read the blog
- It is highly advisable for you, to thoroughly go through the blog. Doing so will greatly facilitate the project setup process and enable a better grasp of the user interface (UI). Your understanding of this section is pivotal in ensuring the efficient configuration of the project and in comprehending the UI.
- Table of Contents
- Prerequisites
- Setting Up a Virtual Environment
- Installing Dependencies
- Usage
- Object Detection
- Data Collection
- Curve Fitting
- Parameter Estimation
- GUI Integration
- Visualization
- Contributing
Before you begin, ensure you have met the following requirements:
- Python 3.6 or higher installed on your system. You can download Python from python.org.
- Git installed (optional, but recommended for cloning the repository).
It's a good practice to work within a virtual environment to isolate your project's dependencies. Here's how to set up and activate a virtual environment:
-
Open a terminal or command prompt.
-
Clone this repository to your local machine:
git clone https://github.com/rudrodip/Harmonic-Oscillator-CV
- Navigate to your project directory:
cd Harmonic-Oscillator-CV
- Create a virtual environment (replace
venv
with your preferred name):
python -m venv venv
- Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS and Linux:
source venv/bin/activate
- Your terminal prompt should now show the name of the virtual environment, indicating that it's active.
Once you have your virtual environment set up and activated, you can install the project's dependencies:
-
Make sure you are in your project directory with the activated virtual environment.
-
Install the dependencies from the
requirements.txt
file:
pip install -r requirements.txt
- Run the main application script:
python app/app.py
-
Use the GUI to configure object detection settings and start the analysis.
-
View real-time plots of position vs. time and the fitted damped oscillation function.
-
Extract physical parameters such as pendulum length and pivot point location.
This project supports multiple object detection methods:
- Color-based masks
- Edge detection
- Hough circles detection
Experiment with these methods and choose the one that works best for your specific application.
The application captures video frames, detects the object in each frame, and collects position data over time. The data is stored as time vs. x-position pairs in memory.
SciPy's curve fitting functions are used to fit the collected data to a damped oscillation function. This step helps extract parameters like amplitude, frequency, damping coefficient, phase and a constant.
def underdamped_harmonic_oscillator(t, A, gamma, w, phi, C):
"""
Calculate the position of an underdamped harmonic oscillator at time t.
Args:
t: Time values.
A: Amplitude of oscillation.
gamma: Damping coefficient.
f: Frequency of oscillation.
phi: Phase angle.
C: Constant offset.
Returns:
Position values at the given time points.
"""
return A * np.exp(-gamma * t) * np.cos(w * t + phi) + C
Equation for decaying oscillation component
def upper_decaying_component_curve(t, A, gamma, C):
"""
Calculate the upper decaying component of a curve at time t.
Args:
t: Time values.
A: Amplitude of decay.
gamma: Decay rate.
C: Constant offset.
Returns:
Value of the upper decaying component at the given time points.
"""
return C + A * np.exp(-gamma * t)
This equation calculates the residuals for fitting a rotated circle to a set of data points, taking into account the transformation of coordinates based on the specified center and rotation.
The rotation of coordinates in the rotated_circle_residuals
function is based on the following mathematical transformations:
- If
$\theta$ (the angle of rotation) is approximately zero:
- If
$\theta$ is not zero:
def rotated_circle_residuals(params, x, y):
"""
Parameters:
params (list): A list containing the parameters of the rotated circle.
params[0] (float): 'a' - X-coordinate of the circle's center.
params[1] (float): 'b' - Y-coordinate of the circle's center.
params[2] (float): 'r' - Radius of the circle.
params[3] (float): 'theta' - Angle of rotation of the circle (in radians).
x (array): Array containing the x-coordinates of data points.
y (array): Array containing the y-coordinates of data points.
Returns:
predicted_y (array): Array of residuals, i.e., the differences between
the observed data points and the corresponding points on the rotated circle.
Notes:
This function calculates the residuals for fitting a rotated circle to a set of data points.
It transforms the data points based on the specified center ('a', 'b') and rotation ('theta')
and computes the differences between the observed 'y' values and the predicted 'y' values
on the rotated circle.
"""
a, b, r, theta = params # Extract the parameters
if np.isclose(theta, 0, atol=1e-6):
x_rotated = x - a
y_rotated = y - b
else:
x_rotated = (x - a) * np.cos(theta) + (y - b) * np.sin(theta)
y_rotated = (y - b) * np.cos(theta) - (x - a) * np.sin(theta)
predicted_y = x_rotated**2 + y_rotated**2 - r**2 # Calculate residuals based on the rotated circle
return predicted_y
The derived parameters are used to estimate other physical properties:
- Pendulum length: Calculated based on the frequency of oscillation.
- Pivot point: Determined using the least squares algorithm.
- Bob radius: Measured from image contours.
- Distance between pivot point and bob surface: Calculated based on bob radius and length of the string.
The project features a PyQt5-based GUI that provides a user-friendly interface for configuring the analysis and visualizing the results.
Real-time plots display position vs. time and the fitted damped oscillation function. Additionally, the pendulum string and pivot point are drawn on the video feed.
Contributions to this project are welcome. Feel free to submit bug reports, feature requests, or pull requests to improve the functionality and usability of the application.