{"id":50864276,"url":"https://github.com/gsaini/opencv-use-cases","last_synced_at":"2026-06-14T23:34:33.120Z","repository":{"id":321152593,"uuid":"1081581347","full_name":"gsaini/opencv-use-cases","owner":"gsaini","description":"A Python-based project for removing image backgrounds using OpenCV, featuring both simple threshold-based and advanced GrabCut algorithms. Includes a user-friendly Streamlit web interface for interactive processing.","archived":false,"fork":false,"pushed_at":"2025-10-29T03:14:44.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-14T23:34:31.982Z","etag":null,"topics":["computer-vision","image-processing","opencv","pillow","pillow-library","python","straml"],"latest_commit_sha":null,"homepage":"","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/gsaini.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-23T01:39:40.000Z","updated_at":"2025-10-29T03:14:47.000Z","dependencies_parsed_at":"2025-10-28T05:21:57.364Z","dependency_job_id":"bda7f88d-119f-432b-baea-8fd2023c1bae","html_url":"https://github.com/gsaini/opencv-use-cases","commit_stats":null,"previous_names":["gsaini/opencv-use-cases"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gsaini/opencv-use-cases","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsaini%2Fopencv-use-cases","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsaini%2Fopencv-use-cases/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsaini%2Fopencv-use-cases/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsaini%2Fopencv-use-cases/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gsaini","download_url":"https://codeload.github.com/gsaini/opencv-use-cases/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsaini%2Fopencv-use-cases/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34342089,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-14T02:00:07.365Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["computer-vision","image-processing","opencv","pillow","pillow-library","python","straml"],"created_at":"2026-06-14T23:34:32.097Z","updated_at":"2026-06-14T23:34:33.108Z","avatar_url":"https://github.com/gsaini.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Open CV Use Cases\n\n![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge\u0026logo=python\u0026logoColor=ffdd54)\n![Streamlit](https://img.shields.io/badge/Streamlit-%23FE4B4B.svg?style=for-the-badge\u0026logo=streamlit\u0026logoColor=white)\n\n## Remove Background from Images Using OpenCV\n\nQuick, practical steps and two working approaches: (A) fast for simple/uniform backgrounds, (B) robust using GrabCut for general photos.\n\nPrerequisites\n\n- Python 3.x\n- pip install opencv-python numpy\n\nMethod A — Simple background removal (uniform background)\n\n1. Convert image to grayscale, blur, and threshold.\n2. Find the largest contour (assumed foreground).\n3. Create a mask, refine with morphology, apply mask and save as PNG with alpha.\n\nExample:\n\n```python\nimport cv2\nimport numpy as np\n\ndef remove_bg_simple(in_path, out_path):\n    img = cv2.imread(in_path)\n    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    blur = cv2.GaussianBlur(gray, (5,5), 0)\n    _, th = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)\n\n    # keep largest contour\n    contours, _ = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    mask = np.zeros_like(th)\n    if contours:\n        c = max(contours, key=cv2.contourArea)\n        cv2.drawContours(mask, [c], -1, 255, -1)\n\n    # refine mask\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))\n    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2)\n    mask = cv2.GaussianBlur(mask, (5,5), 0)\n\n    b, g, r = cv2.split(img)\n    rgba = [b, g, r, mask]\n    out = cv2.merge(rgba, 4)\n    cv2.imwrite(out_path, out)\n```\n\nMethod B — Robust removal using GrabCut\n\n1. Initialize a mask (rectangle or rough mask) and run cv2.grabCut.\n2. Convert the result to a binary mask, refine with morphology.\n3. Apply the mask to the original and save with alpha.\n\nExample:\n\n```python\nimport cv2\nimport numpy as np\n\ndef remove_bg_grabcut(in_path, out_path, iter_count=5, rect_padding=10):\n    img = cv2.imread(in_path)\n    h, w = img.shape[:2]\n    mask = np.zeros((h, w), np.uint8)\n\n    # rectangle slightly inside the image\n    rect = (rect_padding, rect_padding, w-rect_padding*2, h-rect_padding*2)\n\n    bgdModel = np.zeros((1,65), np.float64)\n    fgdModel = np.zeros((1,65), np.float64)\n\n    cv2.grabCut(img, mask, rect, bgdModel, fgdModel, iter_count, cv2.GC_INIT_WITH_RECT)\n\n    # mask: 0/2 background, 1/3 foreground\n    mask2 = np.where((mask==2)|(mask==0), 0, 255).astype('uint8')\n\n    # refine\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))\n    mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel, iterations=2)\n    mask2 = cv2.GaussianBlur(mask2, (3,3), 0)\n\n    b, g, r = cv2.split(img)\n    rgba = cv2.merge([b, g, r, mask2])\n    cv2.imwrite(out_path, rgba)\n```\n\nTips\n\n- For complex scenes, combine user input (scribbles) with GrabCut by initializing mask values cv2.GC_FGD/cv2.GC_BGD.\n- Adjust morphological kernel sizes and blur to control edge softness.\n- Save output as PNG to preserve transparency.\n- For batch processing, run these functions over your dataset and tune parameters per image type.\n\nStreamlit app\n\nThis repository includes a simple Streamlit app `background_removal.py` that lets you upload an image, choose a background removal method (Simple or GrabCut), preview the result and download a PNG with transparency.\n\nRun the app:\n\n```bash\nsource .venv/bin/activate\npip install -r requirements.txt\nstreamlit run background_removal.py\n```\n\nUse the sliders when GrabCut is selected to tune iterations and rectangle padding.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsaini%2Fopencv-use-cases","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgsaini%2Fopencv-use-cases","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsaini%2Fopencv-use-cases/lists"}