{"id":22147797,"url":"https://github.com/rudrodip/harmonic-oscillator-cv","last_synced_at":"2025-10-24T01:02:46.823Z","repository":{"id":192087121,"uuid":"686003029","full_name":"rudrodip/Harmonic-Oscillator-CV","owner":"rudrodip","description":"This Python project is designed to analyze the harmonic oscillation of an object using computer vision techniques.","archived":false,"fork":false,"pushed_at":"2024-07-05T18:03:32.000Z","size":71747,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-01T13:49:52.334Z","etag":null,"topics":["numpy","opencv","opencv-python-headless","pyqt5","python","scipy"],"latest_commit_sha":null,"homepage":"https://www.rdsx.dev/projects/harmonic-motion-analyzer","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rudrodip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-01T14:00:44.000Z","updated_at":"2024-11-17T03:40:27.000Z","dependencies_parsed_at":"2023-09-27T18:40:58.074Z","dependency_job_id":null,"html_url":"https://github.com/rudrodip/Harmonic-Oscillator-CV","commit_stats":null,"previous_names":["rudrodip/harmonic-oscillator-cv"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudrodip%2FHarmonic-Oscillator-CV","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudrodip%2FHarmonic-Oscillator-CV/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudrodip%2FHarmonic-Oscillator-CV/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudrodip%2FHarmonic-Oscillator-CV/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rudrodip","download_url":"https://codeload.github.com/rudrodip/Harmonic-Oscillator-CV/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227642906,"owners_count":17797960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["numpy","opencv","opencv-python-headless","pyqt5","python","scipy"],"created_at":"2024-12-01T23:21:21.862Z","updated_at":"2025-10-24T01:02:42.737Z","avatar_url":"https://github.com/rudrodip.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Computer Vision-Based Harmonic Oscillation Analysis\n\n## Overview\n\nThis 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.\n\n[![Harmonic Oscillator Analyzer](https://img.youtube.com/vi/dalsCsHtreU/0.jpg)](https://www.youtube.com/watch?v=dalsCsHtreU\u0026t=1220s)\n\n### Click on the image to watch a review of the project\n\n---\n\n## Read the blog\n## It is highly advisable for you, to thoroughly go through the [blog](https://rudrodip.vercel.app/blog/harmonic-oscillation-analyzer). 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.\n\n## Table of Contents\n\n- [Computer Vision-Based Harmonic Oscillation Analysis](#computer-vision-based-harmonic-oscillation-analysis)\n  - [Overview](#overview)\n    - [Click on the image to watch a review of the project](#click-on-the-image-to-watch-a-review-of-the-project)\n  - [Read the blog](#read-the-blog)\n  - [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.](#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)\n  - [Table of Contents](#table-of-contents)\n  - [Prerequisites](#prerequisites)\n  - [Setting Up a Virtual Environment](#setting-up-a-virtual-environment)\n  - [Installing Dependencies](#installing-dependencies)\n  - [Usage](#usage)\n  - [Object Detection](#object-detection)\n  - [Data Collection](#data-collection)\n  - [Curve Fitting](#curve-fitting)\n    - [Equation for the under-damped oscillation function](#equation-for-the-under-damped-oscillation-function)\n    - [Equation for circle residual](#equation-for-circle-residual)\n  - [Parameter Estimation](#parameter-estimation)\n  - [GUI Integration](#gui-integration)\n  - [Visualization](#visualization)\n  - [Contributing](#contributing)\n\n## Prerequisites\n\nBefore you begin, ensure you have met the following requirements:\n\n- Python 3.6 or higher installed on your system. You can download Python from [python.org](https://www.python.org/downloads/).\n- [Git](https://git-scm.com/) installed (optional, but recommended for cloning the repository).\n\n## Setting Up a Virtual Environment\n\nIt'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:\n\n1. Open a terminal or command prompt.\n\n2. Clone this repository to your local machine:\n\n```bash\ngit clone https://github.com/rudrodip/Harmonic-Oscillator-CV\n```\n\n3. Navigate to your project directory:\n\n```bash\n cd Harmonic-Oscillator-CV\n```\n\n4. Create a virtual environment (replace `venv` with your preferred name):\n\n```bash\n  python -m venv venv\n```\n\n5. Activate the virtual environment:\n\n  - On Windows:\n\n```bash\n  venv\\Scripts\\activate\n```\n\n  - On macOS and Linux:\n\n```bash\n  source venv/bin/activate\n```\n\n6. Your terminal prompt should now show the name of the virtual environment, indicating that it's active.\n\n\n## Installing Dependencies\n\nOnce you have your virtual environment set up and activated, you can install the project's dependencies:\n\n1. Make sure you are in your project directory with the activated virtual environment.\n\n2. Install the dependencies from the `requirements.txt` file:\n\n```bash\n   pip install -r requirements.txt\n```\n\n## Usage\n\n1. Run the main application script:\n\n```bash\npython app/app.py\n```\n\n2. Use the GUI to configure object detection settings and start the analysis.\n\n3. View real-time plots of position vs. time and the fitted damped oscillation function.\n\n4. Extract physical parameters such as pendulum length and pivot point location.\n\n## Object Detection\n\nThis project supports multiple object detection methods:\n\n- Color-based masks\n- Edge detection\n- Hough circles detection\n\nExperiment with these methods and choose the one that works best for your specific application.\n\n## Data Collection\n\nThe 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.\n\n## Curve Fitting\n\nSciPy'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.\n\n### Equation for the under-damped oscillation function\n\n$$\nx(t) = A e^{-\\gamma t} \\cos(\\omega t + \\phi) + C\n$$\n\n```py\ndef underdamped_harmonic_oscillator(t, A, gamma, w, phi, C):\n    \"\"\"\n    Calculate the position of an underdamped harmonic oscillator at time t.\n\n    Args:\n        t: Time values.\n        A: Amplitude of oscillation.\n        gamma: Damping coefficient.\n        f: Frequency of oscillation.\n        phi: Phase angle.\n        C: Constant offset.\n\n    Returns:\n        Position values at the given time points.\n    \"\"\"\n    return A * np.exp(-gamma * t) * np.cos(w * t + phi) + C\n```\n\nEquation for decaying oscillation component\n\n$$\nx(t) = C \\pm A e^{-\\gamma t}\n$$\n\n```py\ndef upper_decaying_component_curve(t, A, gamma, C):\n    \"\"\"\n    Calculate the upper decaying component of a curve at time t.\n\n    Args:\n        t: Time values.\n        A: Amplitude of decay.\n        gamma: Decay rate.\n        C: Constant offset.\n\n    Returns:\n        Value of the upper decaying component at the given time points.\n    \"\"\"\n    return C + A * np.exp(-gamma * t)\n```\n\n### Equation for circle residual\n\n$$\npredicted_y = (x_{\\text{rotated}}^2 + y_{\\text{rotated}}^2) - r^2\n$$\n\nThis 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.\n\nThe rotation of coordinates in the `rotated_circle_residuals` function is based on the following mathematical transformations:\n\n1. If $\\theta$ (the angle of rotation) is approximately zero:\n\n$$\n\\begin{align*}\nx_{\\text{rotated}} \u0026= x - a \\\\\ny_{\\text{rotated}} \u0026= y - b\n\\end{align*}\n$$\n\n2. If $\\theta$ is not zero:\n\n$$\n\\begin{align*}\nx_{\\text{rotated}} \u0026= (x - a) \\cdot \\cos(\\theta) + (y - b) \\cdot \\sin(\\theta) \\\\\ny_{\\text{rotated}} \u0026= (y - b) \\cdot \\cos(\\theta) - (x - a) \\cdot \\sin(\\theta)\n\\end{align*}\n$$\n\n```py\ndef rotated_circle_residuals(params, x, y):\n    \"\"\"\n    Parameters:\n        params (list): A list containing the parameters of the rotated circle.\n            params[0] (float): 'a' - X-coordinate of the circle's center.\n            params[1] (float): 'b' - Y-coordinate of the circle's center.\n            params[2] (float): 'r' - Radius of the circle.\n            params[3] (float): 'theta' - Angle of rotation of the circle (in radians).\n        x (array): Array containing the x-coordinates of data points.\n        y (array): Array containing the y-coordinates of data points.\n\n    Returns:\n        predicted_y (array): Array of residuals, i.e., the differences between\n        the observed data points and the corresponding points on the rotated circle.\n\n    Notes:\n        This function calculates the residuals for fitting a rotated circle to a set of data points.\n        It transforms the data points based on the specified center ('a', 'b') and rotation ('theta')\n        and computes the differences between the observed 'y' values and the predicted 'y' values\n        on the rotated circle.\n    \"\"\"\n    a, b, r, theta = params  # Extract the parameters\n\n    if np.isclose(theta, 0, atol=1e-6):\n        x_rotated = x - a\n        y_rotated = y - b\n    else:\n        x_rotated = (x - a) * np.cos(theta) + (y - b) * np.sin(theta)\n        y_rotated = (y - b) * np.cos(theta) - (x - a) * np.sin(theta)\n\n    predicted_y = x_rotated**2 + y_rotated**2 - r**2  # Calculate residuals based on the rotated circle\n    return predicted_y\n```\n\n## Parameter Estimation\n\nThe derived parameters are used to estimate other physical properties:\n\n- Pendulum length: Calculated based on the frequency of oscillation.\n- Pivot point: Determined using the least squares algorithm.\n- Bob radius: Measured from image contours.\n- Distance between pivot point and bob surface: Calculated based on bob radius and length of the string.\n\n## GUI Integration\n\nThe project features a PyQt5-based GUI that provides a user-friendly interface for configuring the analysis and visualizing the results.\n\n## Visualization\n\nReal-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.\n\n## Contributing\n\nContributions 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frudrodip%2Fharmonic-oscillator-cv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frudrodip%2Fharmonic-oscillator-cv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frudrodip%2Fharmonic-oscillator-cv/lists"}