-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9789f3
commit 3296b38
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import cv2\n", | ||
"import time\n", | ||
"cap=cv2.VideoCapture(0)#VideoCapture:Class for video capturing from video files, image sequences or cameras.\n", | ||
" #cv2.VideoCapture(device):id of the opened video capturing device (i.e. a camera index). \n", | ||
" #If there is a single camera connected, just pass 0.\n", | ||
"fourcc=cv2.VideoWriter_fourcc(*'XVID') \n", | ||
"out=cv2.VideoWriter('harry.avi' , fourcc, 20.0, (640,480))\n", | ||
"#FourCC is a 4-byte code used to specify the video codec. The codecs for Windows is DIVX. Therefore *’XVID' is used.\n", | ||
"#Then to save Operated Video from a webcam, cv2.VideoWriter() function is used.\n", | ||
"\n", | ||
"time.sleep(2) #to halt the execution of the program for given time in seconds.\n", | ||
"\n", | ||
"background=0 #capturing background\n", | ||
"for i in range(30):\n", | ||
" ret,background=cap.read() #capturing image\n", | ||
"\n", | ||
"while(cap.isOpened()): #Returns true if video capturing has been initialized already.\n", | ||
" ret, img = cap.read() #.read():Grabs, decodes and returns the next video frame.\n", | ||
" if not ret:\n", | ||
" break\n", | ||
" \n", | ||
" hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #.cvtColor():Converts BGR image into HSV image.\n", | ||
" lower_red = np.array([0,120,70])\n", | ||
" upper_red = np.array([10,255,255])\n", | ||
" mask1 = cv2.inRange(hsv , lower_red , upper_red) \n", | ||
" \n", | ||
" lower_red = np.array([170,120,70])\n", | ||
" upper_red = np.array([180,255,255])\n", | ||
" mask2 = cv2.inRange(hsv , lower_red , upper_red)\n", | ||
" \n", | ||
" #in above steps, we find the pixel location of red color and store them in mask1 and mask2. The HSV range of true red is \n", | ||
" #(0,255,255) and to accomodate variations of it we will consider the above ranges.\n", | ||
" #To learn more check:- http://hanzratech.in/2015/02/07/caveat-thresholding-hue-component.html\n", | ||
" \n", | ||
" mask1 = mask1 + mask2 #OR\n", | ||
" \n", | ||
" #in order to carry out process of Dilution and Erosion:-\n", | ||
" mask1=cv2.morphologyEx(mask1, cv2.MORPH_OPEN ,np.ones((3,3) , np.uint8) , iterations=2) #to remove small objects.\n", | ||
" \n", | ||
" mask2=cv2.morphologyEx(mask1, cv2.MORPH_DILATE ,np.ones((3,3) , np.uint8) , iterations=1) #to make image wider.\n", | ||
" \n", | ||
" mask2 = cv2.bitwise_not(mask1) #to invert every bit of an array.\n", | ||
" \n", | ||
" res1 = cv2.bitwise_and(background, background, mask=mask1) #calculates the per-element bit-wise conjunction of two \n", | ||
" res2 = cv2.bitwise_and(img, img, mask=mask2) #arrays or an array and a scalar.\n", | ||
" \n", | ||
" final_output = cv2.addWeighted(res1 , 1, res2 , 1, 0) #calculates the weighted sum of two arrays.\n", | ||
" #cv2.addWeighted(array,alpha,array,beta,gamma):- alpha,beta(weights of arrays) gamma(scalar added to each sum)\n", | ||
" \n", | ||
" cv2.imshow('Harry' , final_output)\n", | ||
" \n", | ||
" if cv2.waitKey(10) & 0xFF==ord('q'): #to cleanup the camera and close any open windows\n", | ||
" cap.release()\n", | ||
" cv2.destroyAllWindows()\n", | ||
" break" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"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.7.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |