{"id":21802537,"url":"https://github.com/qengineering/fast-background-substraction","last_synced_at":"2025-06-25T09:07:16.698Z","repository":{"id":112946234,"uuid":"392755660","full_name":"Qengineering/Fast-Background-Substraction","owner":"Qengineering","description":"Ultra fast background substraction","archived":false,"fork":false,"pushed_at":"2021-12-07T11:41:23.000Z","size":7851,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T18:55:23.535Z","etag":null,"topics":["background-subtraction","cpp","jetson-nano","linux","opencv","raspberry-pi","windows","x86-64"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Qengineering.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2021-08-04T16:22:16.000Z","updated_at":"2024-07-01T12:09:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"e2750d9d-178f-4c5b-89ae-c26873328f04","html_url":"https://github.com/Qengineering/Fast-Background-Substraction","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Qengineering/Fast-Background-Substraction","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FFast-Background-Substraction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FFast-Background-Substraction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FFast-Background-Substraction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FFast-Background-Substraction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Qengineering","download_url":"https://codeload.github.com/Qengineering/Fast-Background-Substraction/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FFast-Background-Substraction/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261841960,"owners_count":23217914,"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":["background-subtraction","cpp","jetson-nano","linux","opencv","raspberry-pi","windows","x86-64"],"created_at":"2024-11-27T11:29:22.818Z","updated_at":"2025-06-25T09:07:16.677Z","avatar_url":"https://github.com/Qengineering.png","language":"C++","readme":"# Fast Background Substraction\n\n\nhttps://user-images.githubusercontent.com/44409029/128219655-52ff1c72-8ad9-4323-a0a4-0eba52268144.mp4\n\n\n\n## A fast background substraction in C++\n\n[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\u003cbr/\u003e\n\nThis C++ application filters the background from a static image. It's written for a Raspberry Pi but can be used on any machine with OpenCV. It's a fast algorithm to detect moving objects in an image given a static background. The available OpenCV routines were too slow for our Raspberry Pi Zero, hence this faster solution.\u003cbr/\u003e\nThe given times are with a Raspberry Pi 4 with a 32-bit OS, running at 1500 MHz.\n\n------------\n\n## Dependencies.\nTo run the application, you have to:\n- OpenCV installed. [Install OpenCV 4.5](https://qengineering.eu/install-opencv-4.5-on-raspberry-64-os.html) \u003cbr/\u003e\n- Code::Blocks installed. (`$ sudo apt-get install codeblocks`)\n\n------------\n\n## Installing the app.\nTo extract and run the application in Code::Blocks \u003cbr/\u003e\n$ mkdir *MyDir* \u003cbr/\u003e\n$ cd *MyDir* \u003cbr/\u003e\n$ wget https://github.com/Qengineering/Fast-Background-Substraction/archive/refs/heads/main.zip \u003cbr/\u003e\n$ unzip -j master.zip \u003cbr/\u003e\nRemove master.zip, LICENSE and README.md as they are no longer needed. \u003cbr/\u003e \n$ rm master.zip \u003cbr/\u003e\n$ rm LICENSE \u003cbr/\u003e\n$ rm README.md \u003cbr/\u003e \u003cbr/\u003e\nYour *MyDir* folder must now look like this: \u003cbr/\u003e \n768x576.avi (example movie)\u003cbr/\u003e\nBackground.cbp (code::blocks project file) \u003cbr/\u003e\nMainQeng.cpp (C++ source file with the new algorithm) \u003cbr/\u003e\nMainMOG2.cpp (C++ source file with exisiting OpenCV algorithms)\n\n------------\n\n## Running the app.\nTo run the application load the project file Background.cbp in Code::Blocks.\u003cbr/\u003e \nYou can load and run either MainQeng.cpp or MainMOG2.cpp in the IDE.\u003cbr/\u003e\nThe code speaks for itself. Here the bare bones.\u003cbr/\u003e\n### Qeng\n```\n  //get black/white\n  cv::cvtColor(frame, bw_frame, COLOR_BGR2GRAY);\n  //go to float\n  bw_frame.convertTo(fImage, CV_32FC1);\n  //calculate Tau\n  f=Tcnt; f/=Xcnt;\n  Tcnt--; if(Tcnt\u003c1) Tcnt=1;\n  //weighted background\n  cv::addWeighted(fImage, f, sum_frame, (1.0-f), 0.0, sum_frame);\n  //substrac current image from static background\n  cv::subtract(sum_frame,fImage,sub_frame);\n  //get a 8 bit black and white image\n  cv::convertScaleAbs(sub_frame,finalImage,1.5);\n\n```\n### MOG2\n```\n  //create Background Subtractor objects\n  Ptr\u003cBackgroundSubtractor\u003e pBackSub;\n  pBackSub = createBackgroundSubtractorMOG2();\n....\n  //update the background model\n  pBackSub-\u003eapply(frame, finalImage);\n```\n### KNN\n```\n  //create Background Subtractor objects\n  Ptr\u003cBackgroundSubtractor\u003e pBackSub;\n  pBackSub = createBackgroundSubtractorKNN();\n....\n  //update the background model\n  pBackSub-\u003eapply(frame, finalImage);\n```\n\n------------\n\n## Tau\nOur algorithm adapts an average background over time. Every single frame updates this `sum_frame` a little bit. The adaptation is needed for slowly changing (lighting) conditions like moving shadows. It will also remove static objects from the scene. A parked car will vanish after some time in the background. Until, of course, it drives away. The constant Tau defines the adaption rate. The higher this number, the faster something disappears in the background.\u003cbr/\u003e\u003cbr/\u003e\nRule of the thumb: **T = 5/(Tau * FPS)**\u003cbr/\u003e\nGiven an FPS (Frames Per Second) of 30 and a Tau of 0.05, an object will completely vanish in the background in 3.3 Sec.\u003cbr/\u003e\u003cbr/\u003e\nThe given percentage is the ratio of white pixels, in other words, the moving object.\u003cbr/\u003e\nBecause the algorithm works with grey values, it is sensitive to contrast. A person wearing a green sweater on the green grass will give a not that high response as the same person walking on the street. However, we gladly pay this small price for the speed the routine achieves.\n\n------------\n\n\n## RaspiCam.\nIf you want to use a camera please alter line 253 in main.cpp to\u003cbr/\u003e\n`cv::VideoCapture cap(0);                //RaspiCam`\u003cbr/\u003e\nIf you want to run a movie please alter line 253 in main.cpp to\u003cbr/\u003e\n`cv::VideoCapture cap( \"768x576.avi\");   //Movie`\u003cbr/\u003e\n\n------------\n\n## Papers.\n- Zoran Zivkovic and Ferdinand van der Heijden. Efficient adaptive density estimation per image pixel for the task of background subtraction. Pattern recognition letters, 27(7):773–780, 2006. [link](https://www.academia.edu/33302633/Efficient_adaptive_density_estimation_per_image_pixel_for_the_task_of_background_subtraction)\u003cbr/\u003e\n- Zoran Zivkovic. Improved adaptive gaussian mixture model for background subtraction. In Pattern Recognition, 2004. ICPR 2004. Proceedings of the 17th International Conference on, volume 2, pages 28–31. IEEE, 2004. [link](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.4658)\u003cbr/\u003e\n\n------------\n\n[![paypal](https://qengineering.eu/images/TipJarSmall4.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=CPZTM5BB3FCYL) \n\n","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=CPZTM5BB3FCYL"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqengineering%2Ffast-background-substraction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqengineering%2Ffast-background-substraction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqengineering%2Ffast-background-substraction/lists"}