{ "cells": [ { "cell_type": "markdown", "id": "6b98a089-5b10-48b3-a62d-703a45c92290", "metadata": {}, "source": [ "# Background Subtraction" ] }, { "cell_type": "code", "execution_count": 1, "id": "d45a3370-5107-4034-9222-0239b4d4585d", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "\n", "backSub = cv2.createBackgroundSubtractorMOG2()\n", "#backSub = cv2.createBackgroundSubtractorKNN()\n", "\n", "capture = cv2.VideoCapture('videos/vtest.avi')\n", "\n", "while True:\n", " ret, frame = capture.read()\n", " if frame is None:\n", " break\n", " \n", " fgMask = backSub.apply(frame)\n", " \n", " \n", " cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)\n", " cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15),\n", " cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))\n", " \n", " \n", " cv2.imshow('Frame', frame)\n", " cv2.imshow('FG Mask', fgMask)\n", " \n", " keyboard = cv2.waitKey(30)\n", " if keyboard == 'q' or keyboard == 27:\n", " break\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "156bd64d-d61b-4c50-b1ad-9d0beb656545", "metadata": {}, "source": [ "
اعمال عملیات مرفولوژی روی نتیجه \n", "
" ] }, { "cell_type": "code", "execution_count": 4, "id": "0b42165e-6361-4746-84bc-bdc22e50c4e7", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np\n", "\n", "backSub = cv2.createBackgroundSubtractorMOG2()\n", "#backSub = cv2.createBackgroundSubtractorKNN()\n", "\n", "capture = cv2.VideoCapture('videos/vtest.avi')\n", "\n", "while True:\n", " ret, frame = capture.read()\n", " if frame is None:\n", " break\n", " \n", " fgMask = backSub.apply(frame)\n", " \n", " kernel = np.ones((5,5), np.uint8)\n", "\n", " fgMask = cv2.erode(fgMask, kernel, iterations=2)\n", " fgMask = cv2.dilate(fgMask, kernel, iterations=2)\n", " fgMask[np.abs(fgMask) < 250] = 0\n", " \n", " \n", " cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)\n", " cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15),\n", " cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))\n", " \n", " \n", " cv2.imshow('Frame', frame)\n", " cv2.imshow('FG Mask', fgMask)\n", " \n", " keyboard = cv2.waitKey(30)\n", " if keyboard == 'q' or keyboard == 27:\n", " break\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "85cdc42b-0241-425e-88b9-64d892d16487", "metadata": {}, "source": [ "
نتیجه روی وبکم \n", "
" ] }, { "cell_type": "code", "execution_count": 5, "id": "0b19f173-a967-4601-a825-a0fc4235bd9c", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "\n", "backSub = cv2.createBackgroundSubtractorMOG2()\n", "#backSub = cv2.createBackgroundSubtractorKNN()\n", "\n", "capture = cv2.VideoCapture(0)\n", "\n", "while True:\n", " ret, frame = capture.read()\n", " if frame is None:\n", " break\n", " \n", " fgMask = backSub.apply(frame)\n", " \n", " cv2.imshow('Frame', frame)\n", " cv2.imshow('FG Mask', fgMask)\n", " \n", " keyboard = cv2.waitKey(30)\n", " if keyboard == 'q' or keyboard == 27:\n", " break\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "419d5946-ab88-4d97-b7eb-17dc3f7712f5", "metadata": {}, "source": [ "https://docs.opencv.org/4.x/d1/dc5/tutorial_background_subtraction.html" ] } ], "metadata": { "kernelspec": { "display_name": "vision-class", "language": "python", "name": "tensorflow_25" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }