{"id":87818,"date":"2023-07-14T07:04:59","date_gmt":"2023-07-14T01:34:59","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=87818"},"modified":"2026-06-03T15:03:09","modified_gmt":"2026-06-03T09:33:09","slug":"python-opencv-background-subtraction-project","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/","title":{"rendered":"Python OpenCV Background Subtraction Project"},"content":{"rendered":"<p>Background subtraction is a common technique used in computer vision to isolate moving objects in a video stream. It is widely used in applications such as surveillance, traffic monitoring, and sports analysis.<\/p>\n<p>In this project at DataFlair, we will explore how to perform background subtraction using OpenCV. To begin, let&#8217;s gain an understanding of the fundamental concept of background subtraction and explore the various techniques available in OpenCV. We will then walk through the step-by-step implementation of the most commonly used method, to extract foreground objects from a video stream.<\/p>\n<p>By the end of this project, you will have a clear understanding of the theory behind background subtraction and the practical skills to apply this technique using OpenCV. This project assumes that you have basic knowledge of Python and OpenCV, although we will explain each step in detail to make it accessible to beginners. So, let&#8217;s dive into the exciting world of background subtraction with OpenCV!<\/p>\n<h3>Background Subtraction Technique<\/h3>\n<p>Background subtraction is a technique used in computer vision to separate an object or a person from the background in an image or a video. OpenCV is a library that provides many functions and algorithms for image and video processing, including background subtraction. There are several popular background subtraction techniques in OpenCV, including simple subtraction, running average, Gaussian Mixture Model, adaptive background subtraction, and deep learning-based techniques. These methods differ in their complexity and accuracy, with some being more suitable for certain scenarios than others. Overall, background subtraction is an essential tool in various applications such as motion detection, video surveillance, and object tracking.<\/p>\n<h3>Prerequisites for Background Subtraction Using Python OpenCV<\/h3>\n<p>It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this, you should have the following system requirements.<\/p>\n<ul>\n<li>Python 3.7 (64-bit) and above<\/li>\n<li>Any python editor (VS code, Pycharm,etc.)<\/li>\n<\/ul>\n<h3>Download Python OpenCV Background Subtraction Project<\/h3>\n<p>Please download the source code of Python OpenCV Background Subtraction Project from the following link: <a href=\"https:\/\/drive.google.com\/file\/d\/1MSA0MRJdiI6dpCg025VCPccALG1t9Nqg\/view?usp=drive_link\"><strong>Python<\/strong> <strong>OpenCV Background Subtraction Project Code<\/strong><\/a><\/p>\n<h3>Installation<\/h3>\n<p>Open windows cmd as administrator<\/p>\n<p>1. To install the opencv library run the command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install opencv-python\r\n<\/pre>\n<p>2. To install the Mediapipe library run the command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install mediapipe\r\n<\/pre>\n<p>3. To install the Cvzone library run the command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install cvzone\r\n<\/pre>\n<h3>Let\u2019s Implement<\/h3>\n<p>1. We need to import some libraries that will be used in our implementation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2 \r\nimport cvzone \r\nfrom cvzone.SelfiSegmentationModule import SelfiSegmentation\r\n<\/pre>\n<p>2. By passing \u20180\u2019 in cv2.VideoCapture() function we are opening the webcam and storing webcam feed in the cap.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cap = cv2.VideoCapture(0)\r\n<\/pre>\n<p>3. These are used to adjust the size of the frames captured by the video capture object. The first line sets the width of the frame to 412 pixels and the second line sets the height of the frame to 480 pixels.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cap.set(3, 412)\r\ncap.set(4, 480)\r\n<\/pre>\n<p>4. It creates an instance of the SelfiSegmentation class from the cvzone library. The SelfiSegmentation class is used for background subtraction and can be used to remove the background from an image or a video stream.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">segment = SelfiSegmentation()\r\n<\/pre>\n<p>5. Start the while loop.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">while True:\r\n<\/pre>\n<p>6. cap.read() function returns two values, first is stored in \u2018ret\u2019 which is a boolean value The function cap.read() reads the frame and returns two values: ret (a boolean indicating if the frame was successfully read) and frame (an array of pixel values in the captured frame). These values can be used to process or display the image.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ret, frame = cap.read()\r\n<\/pre>\n<p>7. This code removes the background from an image or video by using a method called &#8220;removeBG&#8221;. It takes the current frame, a specified color (white), and a threshold value to determine how sensitive the algorithm is to changes in the background. The resulting image with the removed background is stored in a new variable called &#8220;BG_remove&#8221;.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">BG_remove = segment.removeBG(frame, (255, 255, 255), threshold=0.9)\r\n<\/pre>\n<p>8. This line of code uses the &#8220;stackImages&#8221; function from the cvzone library to combine the original frame and the image with the background removed horizontally. The resulting image is stored in the &#8220;stack_img&#8221; variable.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">stack_img = cvzone.stackImages([frame, BG_remove], 2, 1)\r\n<\/pre>\n<p>9. It is used to display Background removed images in a new window named DataFlair.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.imshow(\"DataFlair\", stack_img)\r\n<\/pre>\n<p>10. The cv2.waitKey() function waits for keyboard input for a specified time, and if no key is pressed, it returns -1. In this code, the if statement checks if the key pressed is &#8216;q&#8217; by comparing its code with the key code returned by cv2.waitKey(). If the &#8216;q&#8217; key is pressed, the program exits the loop and terminates, allowing the user to quit the program by pressing the &#8216;q&#8217; key.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.waitKey(1)\r\n    if cv2.waitKey(1) &amp; 0xFF == ord('q'):\r\n        break\r\n<\/pre>\n<p><strong>Note:-<\/strong> Step 6-10 must be under the while loop.<\/p>\n<p>11. cap.release() frees the resources used to capture the video or camera stream, allowing them to be used by other applications. cv2.destroyAllWindows() closes all OpenCV windows, freeing system resources and ensuring the program ends cleanly.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cap.release()\r\ncv2.destroyAllWindows()\r\n<\/pre>\n<h3>Python OpenCV Background Subtraction Output<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/opencv-background-subtraction-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88091 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/opencv-background-subtraction-output.webp\" alt=\"opencv background subtraction output\" width=\"1920\" height=\"1080\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/background-subtraction-project-output-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88096 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/background-subtraction-project-output-1.webp\" alt=\"background subtraction project output\" width=\"1920\" height=\"1080\" \/><\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>To summarize, background subtraction is a technique used to remove the background from an image or video to isolate foreground objects. In this code, we used OpenCV and cvzone to perform background subtraction on live video frames. We applied the &#8220;removeBG&#8221; method to each frame, combined the original and processed frames, and displayed them using the &#8220;imshow&#8221; function. This technique has many uses, including object tracking and video surveillance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background subtraction is a common technique used in computer vision to isolate moving objects in a video stream. It is widely used in applications such as surveillance, traffic monitoring, and sports analysis. In this&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":88090,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193],"tags":[5056,5057,391,392,5058,5059,482],"class_list":["post-87818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-opencv-tutorials","tag-background-subtraction-project","tag-opencv-background-subtraction-project","tag-opencv-project","tag-opencv-project-ideas","tag-python-opencv-background-subtraction","tag-python-opencv-background-subtraction-project","tag-python-opencv-projects"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python OpenCV Background Subtraction Project - TechVidvan<\/title>\n<meta name=\"description\" content=\"Discover the power of OpenCV background subtraction for real-time video processing. Enhance your computer vision skills today.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python OpenCV Background Subtraction Project - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Discover the power of OpenCV background subtraction for real-time video processing. Enhance your computer vision skills today.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-14T01:34:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-03T09:33:09+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python OpenCV Background Subtraction Project - TechVidvan","description":"Discover the power of OpenCV background subtraction for real-time video processing. Enhance your computer vision skills today.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/","og_locale":"en_US","og_type":"article","og_title":"Python OpenCV Background Subtraction Project - TechVidvan","og_description":"Discover the power of OpenCV background subtraction for real-time video processing. Enhance your computer vision skills today.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-07-14T01:34:59+00:00","article_modified_time":"2026-06-03T09:33:09+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python OpenCV Background Subtraction Project","datePublished":"2023-07-14T01:34:59+00:00","dateModified":"2026-06-03T09:33:09+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/"},"wordCount":841,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#primaryimage"},"thumbnailUrl":"","keywords":["background subtraction project","opencv background subtraction project","opencv project","opencv project ideas","python opencv background subtraction","python opencv background subtraction project","python opencv projects"],"articleSection":["OpenCV Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/","url":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/","name":"Python OpenCV Background Subtraction Project - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-07-14T01:34:59+00:00","dateModified":"2026-06-03T09:33:09+00:00","description":"Discover the power of OpenCV background subtraction for real-time video processing. Enhance your computer vision skills today.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-background-subtraction-project\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python OpenCV Background Subtraction Project"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=87818"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87818\/revisions"}],"predecessor-version":[{"id":448024,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87818\/revisions\/448024"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=87818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=87818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=87818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}