├── .gitignore ├── Beginning Tips.ipynb ├── Haarcascades ├── haarcascade_car.xml ├── haarcascade_eye.xml ├── haarcascade_frontalface_default.xml └── haarcascade_fullbody.xml ├── Lecture 10.2 - Photo Denoising.ipynb ├── Lecture 10.3 - Mini Project #12 - Photo Restoration using inpainting.ipynb ├── Lecture 2.4 - Reading, writing and displaying images.ipynb ├── Lecture 2.5 - Grayscaling.ipynb ├── Lecture 2.6 - Color Spaces.ipynb ├── Lecture 2.7 - Histograms.ipynb ├── Lecture 2.8 - Drawing Images.ipynb ├── Lecture 3.10 - Sharpening.ipynb ├── Lecture 3.11 - Thresholding, Binarization & Adaptive Thresholding.ipynb ├── Lecture 3.12 - Dilation, Erosion, Opening and Closing.ipynb ├── Lecture 3.13 - Edge Detection & Image Gradients.ipynb ├── Lecture 3.14 - Perspective & Affine Transforms.ipynb ├── Lecture 3.15 - Mini Project # 1 - Live Sketch Using Webcam.ipynb ├── Lecture 3.2 - Translations.ipynb ├── Lecture 3.3 - Rotations.ipynb ├── Lecture 3.4 - Scaling, re-sizing and interpolations.ipynb ├── Lecture 3.5 - Image Pyramids.ipynb ├── Lecture 3.6 - Cropping.ipynb ├── Lecture 3.7 - Arithmetic Operations.ipynb ├── Lecture 3.8 - Bitwise Operations and Masking.ipynb ├── Lecture 3.9 - Convolutions and Blurring.ipynb ├── Lecture 4.1 - Understanding Contours.ipynb ├── Lecture 4.2 - Sorting Contours.ipynb ├── Lecture 4.3 - Approximating Contours and Convex Hull .ipynb ├── Lecture 4.4 - Matching Contours Shape.ipynb ├── Lecture 4.5 - Mini Project # 2 - Identifying Contours by Shape.ipynb ├── Lecture 4.6 - Line Detection using Hough Lines.ipynb ├── Lecture 4.7 - Circle Detection using Hough Cirlces.ipynb ├── Lecture 4.8 - Blob Detection.ipynb ├── Lecture 4.9 - Mini Project # 3 - Counting Circles and Ellipses .ipynb ├── Lecture 5.2 - Mini Project # 4 - Finding Waldo.ipynb ├── Lecture 5.4 - Finding Corners.ipynb ├── Lecture 5.5 - SIFT, SURF, FAST, BRIEF & ORB.ipynb ├── Lecture 5.6 - Mini Project # 5 - Object Detection using SIFT & ORB.ipynb ├── Lecture 5.7 Histogram of Oriented Gradients.ipynb ├── Lecture 6.2 - Face & Eye Detection.ipynb ├── Lecture 6.3 - Mini Project # 6 - Car & Pedestrian Detection.ipynb ├── Lecture 7.1 - Facial Landmarks.ipynb ├── Lecture 7.2 - Merging Faces.ipynb ├── Lecture 7.3 - Mini Project # 7 - Live Face Swaps.ipynb ├── Lecture 7.4 - Mini Project # 8 – Yawn Detector and Counting.ipynb ├── Lecture 8.2 - Mini Project # 9 - Handwritten Digit Recognition.ipynb ├── Lecture 8.3 - Mini Project # 10 Face Recognition – Unlock Your Computer With Your Face!.ipynb ├── Lecture 9.1 - Filtering by Color.ipynb ├── Lecture 9.2 - Background Subtraction.ipynb ├── Lecture 9.3 - Meanshift Object Tracking.ipynb ├── Lecture 9.4 - Camshift Object Tracking.ipynb ├── Lecture 9.5 - Optical Flow Object Tracking.ipynb ├── Lecture 9.6 - Mini Project # 11 - Ball Tracking.ipynb ├── README.md ├── Slides └── Mastering Computer Vision with OpenCV in Python.pdf ├── image_with_landmarks.jpg ├── images ├── 4star.jpg ├── Hillary.jpg ├── IMG_7539.jpg ├── IMG_8295.JPG ├── Origin_of_Species.jpg ├── Sunflowers.jpg ├── Trump.jpg ├── WaldoBeach.jpg ├── abraham.jpg ├── abraham_mask.png ├── beatle.jpg ├── blobs.jpg ├── bottlecaps.jpg ├── box_in_scene.png ├── bunchofshapes.jpg ├── candy.jpg ├── cars.avi ├── chess.JPG ├── chihuahua.jpg ├── coffee.jpg ├── contours.jpg ├── digits.png ├── domino.png ├── download.png ├── eigenface_reconstruction_opencv.png ├── elephant.jpg ├── ex2.jpg ├── faceswap.JPG ├── gradient.jpg ├── hand.jpg ├── house.jpg ├── input.jpg ├── input33.JPG ├── kim.jpg ├── lourve_noise.jpg ├── marsface.jpg ├── mask.jpg ├── numbers.jpg ├── obama.jpg ├── obamafacerecog.jpg ├── opencv.png ├── opencv_inv.png ├── output.jpg ├── photorestore.JPG ├── rot.jpg ├── scan.jpg ├── shapes.jpg ├── shapes_donut.jpg ├── shapestomatch.jpg ├── soduku.jpg ├── someshapes.jpg ├── tobago.jpg ├── truck.jpg ├── vinod_kumar.jpg └── walking.avi ├── output_shape_number_1.jpg ├── output_shape_number_2.jpg ├── output_shape_number_3.jpg └── output_shape_number_4.jpg /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/.gitignore -------------------------------------------------------------------------------- /Beginning Tips.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### See where your python.exe is located " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import sys\n", 19 | "print(sys.executable)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "#### See your working directory" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": { 33 | "collapsed": false 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "import os\n", 38 | "print(os.getcwd())" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "#### Test your imports" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "import cv2\n", 57 | "import numpy as np\n", 58 | "import matplotlib" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "#### Display the version of OpenCV " 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": { 72 | "collapsed": true 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "print cv2.__version__" 77 | ] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 2", 83 | "language": "python", 84 | "name": "python2" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 2 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython2", 96 | "version": "2.7.11" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 0 101 | } 102 | -------------------------------------------------------------------------------- /Lecture 10.2 - Photo Denoising.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Photo Denoising" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "import cv2\n", 20 | "\n", 21 | "image = cv2.imread('images/elephant.jpg')\n", 22 | "\n", 23 | "# cv2.fastNlMeansDenoisingColored(input, None, h, hForColorComponents, templateWindowSize, searchWindowSize)\n", 24 | "# None are - the filter strength 'h' (5-12 is a good range)\n", 25 | "# Next is hForColorComponents, set as same value as h again\n", 26 | "# templateWindowSize (odd numbers only) rec. 7\n", 27 | "# searchWindowSize (odd numbers only) rec. 21\n", 28 | "\n", 29 | "dst = cv2.fastNlMeansDenoisingColored(image, None, 11, 6, 7, 21)\n", 30 | "\n", 31 | "cv2.imshow('Fast Means Denoising', dst)\n", 32 | "cv2.waitKey(0)\n", 33 | "\n", 34 | "cv2.destroyAllWindows()" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "**There are 4 variations of Non-Local Means Denoising:**\n", 42 | "\n", 43 | "- cv2.fastNlMeansDenoising() - works with a single grayscale images\n", 44 | "- cv2.fastNlMeansDenoisingColored() - works with a color image.\n", 45 | "- cv2.fastNlMeansDenoisingMulti() - works with image sequence captured in short period of time (grayscale images)\n", 46 | "- cv2.fastNlMeansDenoisingColoredMulti() - same as above, but for color images." 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 2", 53 | "language": "python", 54 | "name": "python2" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 2 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython2", 66 | "version": "2.7.11" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 0 71 | } 72 | -------------------------------------------------------------------------------- /Lecture 10.3 - Mini Project #12 - Photo Restoration using inpainting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Photo Restoration using inpainting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 14, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "\n", 22 | "# Load our damaged photo\n", 23 | "image = cv2.imread('images/abraham.jpg')\n", 24 | "cv2.imshow('Original Damaged Photo', image)\n", 25 | "cv2.waitKey(0)\n", 26 | "\n", 27 | "# Load the photo where we've marked the damaged areas\n", 28 | "marked_damages = cv2.imread('images/mask.jpg', 0)\n", 29 | "cv2.imshow('Marked Damages', marked_damages)\n", 30 | "cv2.waitKey(0)\n", 31 | "\n", 32 | "# Let's make a mask out of our marked image be changing all colors \n", 33 | "# that are not white, to black\n", 34 | "ret, thresh1 = cv2.threshold(marked_damages, 254, 255, cv2.THRESH_BINARY)\n", 35 | "cv2.imshow('Threshold Binary', thresh1)\n", 36 | "cv2.waitKey(0)\n", 37 | "\n", 38 | "# Let's dilate (make thicker) our the marks w made\n", 39 | "# since thresholding has narrowed it slightly\n", 40 | "kernel = np.ones((7,7), np.uint8)\n", 41 | "mask = cv2.dilate(thresh1, kernel, iterations = 1)\n", 42 | "cv2.imshow('Dilated Mask', mask)\n", 43 | "cv2.imwrite(\"images/abraham_mask.png\", mask)\n", 44 | "\n", 45 | "cv2.waitKey(0)\n", 46 | "restored = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)\n", 47 | "\n", 48 | "cv2.imshow('Restored', restored)\n", 49 | "cv2.waitKey(0)\n", 50 | "cv2.destroyAllWindows()\n" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [], 60 | "source": [] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "**Inpainting** is the process of reconstructing lost or deteriorated parts of images and videos. It is an advanced form of interpolation that can be used to replace lost or corrupted parts of the image data." 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "**cv2.inpaint**(input image, mask, inpaintRadius, Inpaint Method)\n", 74 | "\n", 75 | "**inpaintRadius** – Radius of a circular neighborhood of each point inpainted that is considered by the algorithm. Smaller values look less blurred, while larger values look more pixelated or blurred. \n", 76 | "\n", 77 | "**Inpaint Methods**\n", 78 | "- INPAINT_NS - Navier-Stokes based method [Navier01]\n", 79 | "- INPAINT_TELEA - Method by Alexandru Telea [Telea04] - Better as it integrates more seamlessley into the image." 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": { 86 | "collapsed": true 87 | }, 88 | "outputs": [], 89 | "source": [] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": true 96 | }, 97 | "outputs": [], 98 | "source": [] 99 | } 100 | ], 101 | "metadata": { 102 | "kernelspec": { 103 | "display_name": "Python 2", 104 | "language": "python", 105 | "name": "python2" 106 | }, 107 | "language_info": { 108 | "codemirror_mode": { 109 | "name": "ipython", 110 | "version": 2 111 | }, 112 | "file_extension": ".py", 113 | "mimetype": "text/x-python", 114 | "name": "python", 115 | "nbconvert_exporter": "python", 116 | "pygments_lexer": "ipython2", 117 | "version": "2.7.11" 118 | } 119 | }, 120 | "nbformat": 4, 121 | "nbformat_minor": 0 122 | } 123 | -------------------------------------------------------------------------------- /Lecture 2.4 - Reading, writing and displaying images.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Hi and welcome to your first code along lesson!\n", 8 | "\n", 9 | "#### Reading, writing and displaying images with OpenCV" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "Let's start by importing the OpenCV libary " 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": true 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "# Press CTRL + ENTER to run this line\n", 28 | "# You should see an * between the [ ] on the left\n", 29 | "# OpenCV takes a couple seconds to import the first time\n", 30 | "\n", 31 | "import cv2" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": { 47 | "collapsed": true 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "# Now let's import numpy\n", 52 | "# We use as np, so that everything we call on numpy, we can type np instead\n", 53 | "# It's short and looks neater\n", 54 | "\n", 55 | "import numpy as np " 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "Let's now load our first image" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": { 69 | "collapsed": false 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "# We don't need to do this again, but it's a good habit\n", 74 | "import cv2 \n", 75 | "\n", 76 | "# Load an image using 'imread' specifying the path to image\n", 77 | "input = cv2.imread('./images/input.jpg')\n", 78 | "\n", 79 | "# Our file 'input.jpg' is now loaded and stored in python \n", 80 | "# as a varaible we named 'image'\n", 81 | "\n", 82 | "# To display our image variable, we use 'imshow'\n", 83 | "# The first parameter will be title shown on image window\n", 84 | "# The second parameter is the image varialbe\n", 85 | "cv2.imshow('Hello World', input)\n", 86 | "\n", 87 | "# 'waitKey' allows us to input information when a image window is open\n", 88 | "# By leaving it blank it just waits for anykey to be pressed before \n", 89 | "# continuing. By placing numbers (except 0), we can specify a delay for\n", 90 | "# how long you keep the window open (time is in milliseconds here)\n", 91 | "cv2.waitKey()\n", 92 | "\n", 93 | "# This closes all open windows \n", 94 | "# Failure to place this will cause your program to hang\n", 95 | "cv2.destroyAllWindows()" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": { 102 | "collapsed": true 103 | }, 104 | "outputs": [], 105 | "source": [ 106 | "# Same as above without the extraneous comments\n", 107 | "\n", 108 | "import cv2 \n", 109 | "\n", 110 | "input = cv2.imread('./images/input.jpg')\n", 111 | "\n", 112 | "cv2.imshow('Hello World', input)\n", 113 | "cv2.waitKey(0)\n", 114 | "cv2.destroyAllWindows()" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "### Let's take a closer look at how images are stored" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 16, 127 | "metadata": { 128 | "collapsed": true 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "# Import numpy\n", 133 | "import numpy as np" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 15, 139 | "metadata": { 140 | "collapsed": false 141 | }, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "(830L, 1245L, 3L)\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "print input.shape" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "#### Shape gives the dimensions of the image array\n", 160 | "\n", 161 | "The 2D dimensions are 830 pixels in high bv 1245 pixels wide.\n", 162 | "The '3L' means that there are 3 other components (RGB) that make up this image." 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 17, 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "Height of Image: 830 pixels\n", 177 | "Width of Image: 1245 pixels\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "# Let's print each dimension of the image\n", 183 | "\n", 184 | "print 'Height of Image:', int(input.shape[0]), 'pixels'\n", 185 | "print 'Width of Image: ', int(input.shape[1]), 'pixels'" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### How do we save images we edit in OpenCV?" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 18, 198 | "metadata": { 199 | "collapsed": false 200 | }, 201 | "outputs": [ 202 | { 203 | "data": { 204 | "text/plain": [ 205 | "True" 206 | ] 207 | }, 208 | "execution_count": 18, 209 | "metadata": {}, 210 | "output_type": "execute_result" 211 | } 212 | ], 213 | "source": [ 214 | "# Simply use 'imwrite' specificing the file name and the image to be saved\n", 215 | "cv2.imwrite('output.jpg', input)\n", 216 | "cv2.imwrite('output.png', input)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "metadata": { 223 | "collapsed": true 224 | }, 225 | "outputs": [], 226 | "source": [] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": { 232 | "collapsed": true 233 | }, 234 | "outputs": [], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": { 241 | "collapsed": true 242 | }, 243 | "outputs": [], 244 | "source": [] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": { 250 | "collapsed": true 251 | }, 252 | "outputs": [], 253 | "source": [] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": { 259 | "collapsed": true 260 | }, 261 | "outputs": [], 262 | "source": [] 263 | } 264 | ], 265 | "metadata": { 266 | "kernelspec": { 267 | "display_name": "Python 2", 268 | "language": "python", 269 | "name": "python2" 270 | }, 271 | "language_info": { 272 | "codemirror_mode": { 273 | "name": "ipython", 274 | "version": 2 275 | }, 276 | "file_extension": ".py", 277 | "mimetype": "text/x-python", 278 | "name": "python", 279 | "nbconvert_exporter": "python", 280 | "pygments_lexer": "ipython2", 281 | "version": "2.7.11" 282 | } 283 | }, 284 | "nbformat": 4, 285 | "nbformat_minor": 0 286 | } 287 | -------------------------------------------------------------------------------- /Lecture 2.5 - Grayscaling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Grayscaling\n", 8 | "\n", 9 | "#### Grayscaling is process by which an image is converted from a full color to shades of grey (black & white)\n", 10 | "\n", 11 | "In OpenCV, many functions grayscale images before processing. This is done because it simplifies the image, acting almost as a noise reduction and increasing processing time as there is less information in the image.\n", 12 | "\n", 13 | "### Let convert our color image to greyscale" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import cv2\n", 25 | "\n", 26 | "# Load our input image\n", 27 | "image = cv2.imread('./images/input.jpg')\n", 28 | "cv2.imshow('Original', image)\n", 29 | "cv2.waitKey()\n", 30 | "\n", 31 | "# We use cvtColor, to convert to grayscale\n", 32 | "gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 33 | "\n", 34 | "cv2.imshow('Grayscale', gray_image)\n", 35 | "cv2.waitKey()\n", 36 | "cv2.destroyAllWindows()" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 5, 42 | "metadata": { 43 | "collapsed": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "#Another method faster method\n", 48 | "img = cv2.imread('./images/input.jpg',0)\n", 49 | "\n", 50 | "cv2.imshow('Grayscale', img)\n", 51 | "cv2.waitKey()\n", 52 | "cv2.destroyAllWindows()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 2", 64 | "language": "python", 65 | "name": "python2" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 2 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython2", 77 | "version": "2.7.11" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 0 82 | } 83 | -------------------------------------------------------------------------------- /Lecture 2.6 - Color Spaces.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Let's take a closer look at color spaces\n", 8 | "\n", 9 | "You may have remembered we talked about images being stored in RGB (Red Green Blue) color Spaces. Let's take a look at that in OpenCV.\n", 10 | "\n", 11 | "### First thing to remember about OpenCV's RGB is that it's BGR (I know, this is annoying)\n", 12 | "\n", 13 | "Let's look at the image shape again. The '3L' " 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "import cv2\n", 25 | "import numpy as np\n", 26 | "\n", 27 | "image = cv2.imread('./images/input.jpg')" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "### Let's look at the individual color levels for the first pixel (0,0)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 5, 40 | "metadata": { 41 | "collapsed": false 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "13 19 32\n", 49 | "(830L, 1245L, 3L)\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "# BGR Values for the first 0,0 pixel\n", 55 | "B, G, R = image[10, 50] \n", 56 | "print B, G, R\n", 57 | "print image.shape" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Let's see what happens when we convert it to grayscale" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 7, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "(830L, 1245L)\n", 79 | "22\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 85 | "print gray_img.shape\n", 86 | "print gray_img[10, 50] " 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "It's now only 2 dimensions. Each pixel coordinate has only one value (previously 3) with a range of 0 to 255" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 8, 99 | "metadata": { 100 | "collapsed": false 101 | }, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "21" 107 | ] 108 | }, 109 | "execution_count": 8, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "gray_img[0, 0] " 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "### Another useful color space is HSV \n", 123 | "Infact HSV is very useful in color filtering." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 11, 129 | "metadata": { 130 | "collapsed": true 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "#H: 0 - 180, S: 0 - 255, V: 0 - 255\n", 135 | "\n", 136 | "image = cv2.imread('./images/input.jpg')\n", 137 | "\n", 138 | "hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n", 139 | "\n", 140 | "cv2.imshow('HSV image', hsv_image)\n", 141 | "cv2.imshow('Hue channel', hsv_image[:, :, 0])\n", 142 | "cv2.imshow('Saturation channel', hsv_image[:, :, 1])\n", 143 | "cv2.imshow('Value channel', hsv_image[:, :, 2])\n", 144 | "\n", 145 | "cv2.waitKey()\n", 146 | "cv2.destroyAllWindows()" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "### Let's now explore lookng at individual channels in an RGB image" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 12, 159 | "metadata": { 160 | "collapsed": false 161 | }, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "(830L, 1245L)\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "image = cv2.imread('./images/input.jpg')\n", 173 | "\n", 174 | "# OpenCV's 'split' function splites the image into each color index\n", 175 | "B, G, R = cv2.split(image)\n", 176 | "\n", 177 | "print B.shape\n", 178 | "cv2.imshow(\"Red\", R)\n", 179 | "cv2.imshow(\"Green\", G)\n", 180 | "cv2.imshow(\"Blue\", B)\n", 181 | "cv2.waitKey(0)\n", 182 | "cv2.destroyAllWindows()\n", 183 | "\n", 184 | "# Let's re-make the original image, \n", 185 | "merged = cv2.merge([B, G, R]) \n", 186 | "cv2.imshow(\"Merged\", merged) \n", 187 | "\n", 188 | "# Let's amplify the blue color\n", 189 | "merged = cv2.merge([B+100, G, R])\n", 190 | "cv2.imshow(\"Merged with Blue Amplified\", merged) \n", 191 | "\n", 192 | "cv2.waitKey(0)\n", 193 | "cv2.destroyAllWindows()" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 20, 199 | "metadata": { 200 | "collapsed": false 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "import cv2\n", 205 | "import numpy as np\n", 206 | "\n", 207 | "B, G, R = cv2.split(image)\n", 208 | "\n", 209 | "# Let's create a matrix of zeros \n", 210 | "# with dimensions of the image h x w \n", 211 | "zeros = np.zeros(image.shape[:2], dtype = \"uint8\")\n", 212 | "\n", 213 | "cv2.imshow(\"Red\", cv2.merge([zeros, zeros, R]))\n", 214 | "cv2.imshow(\"Green\", cv2.merge([zeros, G, zeros]))\n", 215 | "cv2.imshow(\"Blue\", cv2.merge([B, zeros, zeros]))\n", 216 | "\n", 217 | "cv2.waitKey(0)\n", 218 | "cv2.destroyAllWindows()" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 19, 224 | "metadata": { 225 | "collapsed": false 226 | }, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "(830L, 1245L)" 232 | ] 233 | }, 234 | "execution_count": 19, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "image.shape[:2]" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "#### You can view a list of color converisons here, but keep in mind you won't ever use or need many of these\n", 248 | "\n", 249 | "http://docs.opencv.org/trunk/d7/d1b/group__imgproc__misc.html#ga4e0972be5de079fed4e3a10e24ef5ef0" 250 | ] 251 | } 252 | ], 253 | "metadata": { 254 | "kernelspec": { 255 | "display_name": "Python 2", 256 | "language": "python", 257 | "name": "python2" 258 | }, 259 | "language_info": { 260 | "codemirror_mode": { 261 | "name": "ipython", 262 | "version": 2 263 | }, 264 | "file_extension": ".py", 265 | "mimetype": "text/x-python", 266 | "name": "python", 267 | "nbconvert_exporter": "python", 268 | "pygments_lexer": "ipython2", 269 | "version": "2.7.11" 270 | } 271 | }, 272 | "nbformat": 4, 273 | "nbformat_minor": 0 274 | } 275 | -------------------------------------------------------------------------------- /Lecture 2.7 - Histograms.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Histograms are a great way to visualize individual color components" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "# We need to import matplotlib to create our histogram plots\n", 22 | "from matplotlib import pyplot as plt\n", 23 | "\n", 24 | "image = cv2.imread('images/input.jpg')\n", 25 | "\n", 26 | "histogram = cv2.calcHist([image], [0], None, [256], [0, 256])\n", 27 | "\n", 28 | "# We plot a histogram, ravel() flatens our image array \n", 29 | "plt.hist(image.ravel(), 256, [0, 256]); plt.show()\n", 30 | "\n", 31 | "# Viewing Separate Color Channels\n", 32 | "color = ('b', 'g', 'r')\n", 33 | "\n", 34 | "# We now separate the colors and plot each in the Histogram\n", 35 | "for i, col in enumerate(color):\n", 36 | " histogram2 = cv2.calcHist([image], [i], None, [256], [0, 256])\n", 37 | " plt.plot(histogram2, color = col)\n", 38 | " plt.xlim([0,256])\n", 39 | " \n", 40 | "plt.show()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "**cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])**\n", 48 | "\n", 49 | "- images : it is the source image of type uint8 or float32. it should be given in square brackets, ie, \"[img]\".\n", 50 | "- channels : it is also given in square brackets. It is the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively.\n", 51 | "- mask : mask image. To find histogram of full image, it is given as \"None\". But if you want to find histogram of particular region of image, you have to create a mask image for that and give it as mask. (I will show an example later.)\n", 52 | "- histSize : this represents our BIN count. Need to be given in square brackets. For full scale, we pass [256].\n", 53 | "- ranges : this is our RANGE. Normally, it is [0,256]." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": { 69 | "collapsed": true 70 | }, 71 | "outputs": [], 72 | "source": [] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 6, 77 | "metadata": { 78 | "collapsed": true 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "import cv2\n", 83 | "import numpy as np\n", 84 | "\n", 85 | "# We need to import matplotlib to create our histogram plots\n", 86 | "from matplotlib import pyplot as plt\n", 87 | "\n", 88 | "image = cv2.imread('images/tobago.jpg')\n", 89 | "\n", 90 | "histogram = cv2.calcHist([image], [0], None, [256], [0, 256])\n", 91 | "\n", 92 | "# We plot a histogram, ravel() flatens our image array \n", 93 | "plt.hist(image.ravel(), 256, [0, 256]); plt.show()\n", 94 | "\n", 95 | "# Viewing Separate Color Channels\n", 96 | "color = ('b', 'g', 'r')\n", 97 | "\n", 98 | "# We now separate the colors and plot each in the Histogram\n", 99 | "for i, col in enumerate(color):\n", 100 | " histogram2 = cv2.calcHist([image], [i], None, [256], [0, 256])\n", 101 | " plt.plot(histogram2, color = col)\n", 102 | " plt.xlim([0,256])\n", 103 | " \n", 104 | "plt.show()" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 5, 110 | "metadata": { 111 | "collapsed": true 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "image = cv2.imread('images/tobago.jpg')\n", 116 | "cv2.imshow(\"Tobago\", image) \n", 117 | "\n", 118 | "cv2.waitKey(0)\n", 119 | "cv2.destroyAllWindows()" 120 | ] 121 | } 122 | ], 123 | "metadata": { 124 | "kernelspec": { 125 | "display_name": "Python 2", 126 | "language": "python", 127 | "name": "python2" 128 | }, 129 | "language_info": { 130 | "codemirror_mode": { 131 | "name": "ipython", 132 | "version": 2 133 | }, 134 | "file_extension": ".py", 135 | "mimetype": "text/x-python", 136 | "name": "python", 137 | "nbconvert_exporter": "python", 138 | "pygments_lexer": "ipython2", 139 | "version": "2.7.11" 140 | } 141 | }, 142 | "nbformat": 4, 143 | "nbformat_minor": 0 144 | } 145 | -------------------------------------------------------------------------------- /Lecture 2.8 - Drawing Images.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Drawing images and shapes using OpenCV\n", 8 | "\n", 9 | "Let's start off my making a black square" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 22, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import cv2\n", 21 | "import numpy as np\n", 22 | "\n", 23 | "# Create a black image\n", 24 | "image = np.zeros((512,512,3), np.uint8)\n", 25 | "\n", 26 | "# Can we make this in black and white?\n", 27 | "image_bw = np.zeros((512,512), np.uint8)\n", 28 | "\n", 29 | "cv2.imshow(\"Black Rectangle (Color)\", image)\n", 30 | "cv2.imshow(\"Black Rectangle (B&W)\", image_bw)\n", 31 | "\n", 32 | "cv2.waitKey(0)\n", 33 | "cv2.destroyAllWindows()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "### Let's draw a line over our black sqare\n", 41 | "\n", 42 | "cv2.line(image, starting cordinates, ending cordinates, color, thickness)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 23, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "# Draw a diagonal blue line of thickness of 5 pixels\n", 54 | "image = np.zeros((512,512,3), np.uint8)\n", 55 | "cv2.line(image, (0,0), (511,511), (255,127,0), 5)\n", 56 | "cv2.imshow(\"Blue Line\", image)\n", 57 | "\n", 58 | "cv2.waitKey(0)\n", 59 | "cv2.destroyAllWindows()" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "### Let's now draw a rectangle\n", 67 | "\n", 68 | "cv2.rectangle(image, starting vertex, opposite vertex, color, thickness)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 25, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "# Draw a Rectangle in\n", 80 | "image = np.zeros((512,512,3), np.uint8)\n", 81 | "\n", 82 | "cv2.rectangle(image, (100,100), (300,250), (127,50,127), -1)\n", 83 | "cv2.imshow(\"Rectangle\", image)\n", 84 | "cv2.waitKey(0)\n", 85 | "cv2.destroyAllWindows()" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "### How about cirlcles?\n", 93 | "\n", 94 | "cv2.cirlce(image, center, radius, color, fill)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 26, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [], 104 | "source": [ 105 | "image = np.zeros((512,512,3), np.uint8)\n", 106 | "\n", 107 | "cv2.circle(image, (350, 350), 100, (15,75,50), -1) \n", 108 | "cv2.imshow(\"Circle\", image)\n", 109 | "cv2.waitKey(0)\n", 110 | "cv2.destroyAllWindows()" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "## And polygons?" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 27, 123 | "metadata": { 124 | "collapsed": false 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "image = np.zeros((512,512,3), np.uint8)\n", 129 | "\n", 130 | "# Let's define four points\n", 131 | "pts = np.array( [[10,50], [400,50], [90,200], [50,500]], np.int32)\n", 132 | "\n", 133 | "# Let's now reshape our points in form required by polylines\n", 134 | "pts = pts.reshape((-1,1,2))\n", 135 | "\n", 136 | "cv2.polylines(image, [pts], True, (0,0,255), 3)\n", 137 | "cv2.imshow(\"Polygon\", image)\n", 138 | "cv2.waitKey(0)\n", 139 | "cv2.destroyAllWindows()" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "### Let's even add text with cv2.putText\n", 147 | "\n", 148 | "cv2.putText(image, 'Text to Display', bottom left starting point, Font, Font Size, Color, Thickness)\n", 149 | "\n", 150 | "- FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN\n", 151 | "- FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX \n", 152 | "- FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL\n", 153 | "- FONT_HERSHEY_SCRIPT_SIMPLEX\n", 154 | "- FONT_HERSHEY_SCRIPT_COMPLEX" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 28, 160 | "metadata": { 161 | "collapsed": false 162 | }, 163 | "outputs": [], 164 | "source": [ 165 | "image = np.zeros((512,512,3), np.uint8)\n", 166 | "\n", 167 | "cv2.putText(image, 'Hello World!', (75,290), cv2.FONT_HERSHEY_COMPLEX, 2, (100,170,0), 3)\n", 168 | "cv2.imshow(\"Hello World!\", image)\n", 169 | "cv2.waitKey(0)\n", 170 | "cv2.destroyAllWindows()" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": { 177 | "collapsed": true 178 | }, 179 | "outputs": [], 180 | "source": [] 181 | } 182 | ], 183 | "metadata": { 184 | "kernelspec": { 185 | "display_name": "Python 2", 186 | "language": "python", 187 | "name": "python2" 188 | }, 189 | "language_info": { 190 | "codemirror_mode": { 191 | "name": "ipython", 192 | "version": 2 193 | }, 194 | "file_extension": ".py", 195 | "mimetype": "text/x-python", 196 | "name": "python", 197 | "nbconvert_exporter": "python", 198 | "pygments_lexer": "ipython2", 199 | "version": "2.7.11" 200 | } 201 | }, 202 | "nbformat": 4, 203 | "nbformat_minor": 0 204 | } 205 | -------------------------------------------------------------------------------- /Lecture 3.10 - Sharpening.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Sharpening \n", 8 | "\n", 9 | "By altering our kernels we can implement sharpening, which has the effects of in strengthening or emphasizing edges in an image." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import cv2\n", 21 | "import numpy as np\n", 22 | "\n", 23 | "image = cv2.imread('images/input.jpg')\n", 24 | "cv2.imshow('Original', image)\n", 25 | "\n", 26 | "# Create our shapening kernel, we don't normalize since the \n", 27 | "# the values in the matrix sum to 1\n", 28 | "kernel_sharpening = np.array([[-1,-1,-1], \n", 29 | " [-1,9,-1], \n", 30 | " [-1,-1,-1]])\n", 31 | "\n", 32 | "# applying different kernels to the input image\n", 33 | "sharpened = cv2.filter2D(image, -1, kernel_sharpening)\n", 34 | "\n", 35 | "cv2.imshow('Image Sharpening', sharpened)\n", 36 | "\n", 37 | "cv2.waitKey(0)\n", 38 | "cv2.destroyAllWindows()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": { 45 | "collapsed": false 46 | }, 47 | "outputs": [], 48 | "source": [] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 2", 54 | "language": "python", 55 | "name": "python2" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 2 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython2", 67 | "version": "2.7.11" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 0 72 | } 73 | -------------------------------------------------------------------------------- /Lecture 3.11 - Thresholding, Binarization & Adaptive Thresholding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Thresholding, Binarization & Adaptive Thresholding\n", 8 | "\n", 9 | "\n", 10 | "In thresholding, we convert a grey scale image to it's binary form" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 3, 16 | "metadata": { 17 | "collapsed": false 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "import cv2\n", 22 | "import numpy as np\n", 23 | "\n", 24 | "# Load our image as greyscale \n", 25 | "image = cv2.imread('images/gradient.jpg',0)\n", 26 | "cv2.imshow('Original', image)\n", 27 | "\n", 28 | "# Values below 127 goes to 0 (black, everything above goes to 255 (white)\n", 29 | "ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)\n", 30 | "cv2.imshow('1 Threshold Binary', thresh1)\n", 31 | "\n", 32 | "# Values below 127 go to 255 and values above 127 go to 0 (reverse of above)\n", 33 | "ret,thresh2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)\n", 34 | "cv2.imshow('2 Threshold Binary Inverse', thresh2)\n", 35 | "\n", 36 | "# Values above 127 are truncated (held) at 127 (the 255 argument is unused)\n", 37 | "ret,thresh3 = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC)\n", 38 | "cv2.imshow('3 THRESH TRUNC', thresh3)\n", 39 | "\n", 40 | "# Values below 127 go to 0, above 127 are unchanged \n", 41 | "ret,thresh4 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO)\n", 42 | "cv2.imshow('4 THRESH TOZERO', thresh4)\n", 43 | "\n", 44 | "# Resever of above, below 127 is unchanged, above 127 goes to 0\n", 45 | "ret,thresh5 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV)\n", 46 | "cv2.imshow('5 THRESH TOZERO INV', thresh5)\n", 47 | "cv2.waitKey(0) \n", 48 | " \n", 49 | "cv2.destroyAllWindows()" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "### Is there a better way off thresholding?\n", 57 | "\n", 58 | "The biggest downfall of those simple threshold methods is that we need to provide the threshold value (i.e. the 127 value we used previously).\n", 59 | "#### What if there was a smarter way of doing this?\n", 60 | "\n", 61 | "There is with, Adaptive thresholding. \n", 62 | "\n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 4, 68 | "metadata": { 69 | "collapsed": true 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "import cv2\n", 74 | "import numpy as np\n", 75 | "\n", 76 | "# Load our new image\n", 77 | "image = cv2.imread('images/Origin_of_Species.jpg', 0)\n", 78 | "\n", 79 | "cv2.imshow('Original', image)\n", 80 | "cv2.waitKey(0) \n", 81 | "\n", 82 | "# Values below 127 goes to 0 (black, everything above goes to 255 (white)\n", 83 | "ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)\n", 84 | "cv2.imshow('Threshold Binary', thresh1)\n", 85 | "cv2.waitKey(0) \n", 86 | "\n", 87 | "# It's good practice to blur images as it removes noise\n", 88 | "image = cv2.GaussianBlur(image, (3, 3), 0)\n", 89 | "\n", 90 | "# Using adaptiveThreshold\n", 91 | "thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \n", 92 | " cv2.THRESH_BINARY, 3, 5) \n", 93 | "cv2.imshow(\"Adaptive Mean Thresholding\", thresh) \n", 94 | "cv2.waitKey(0) \n", 95 | "\n", 96 | "_, th2 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n", 97 | "cv2.imshow(\"Otsu's Thresholding\", thresh) \n", 98 | "cv2.waitKey(0) \n", 99 | "\n", 100 | "# Otsu's thresholding after Gaussian filtering\n", 101 | "blur = cv2.GaussianBlur(image, (5,5), 0)\n", 102 | "_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n", 103 | "cv2.imshow(\"Guassian Otsu's Thresholding\", thresh) \n", 104 | "cv2.waitKey(0) \n", 105 | "\n", 106 | "cv2.destroyAllWindows()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": { 113 | "collapsed": true 114 | }, 115 | "outputs": [], 116 | "source": [] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [] 126 | } 127 | ], 128 | "metadata": { 129 | "kernelspec": { 130 | "display_name": "Python 2", 131 | "language": "python", 132 | "name": "python2" 133 | }, 134 | "language_info": { 135 | "codemirror_mode": { 136 | "name": "ipython", 137 | "version": 2 138 | }, 139 | "file_extension": ".py", 140 | "mimetype": "text/x-python", 141 | "name": "python", 142 | "nbconvert_exporter": "python", 143 | "pygments_lexer": "ipython2", 144 | "version": "2.7.11" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 0 149 | } 150 | -------------------------------------------------------------------------------- /Lecture 3.12 - Dilation, Erosion, Opening and Closing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Dilation, Erosion, Opening and Closing " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "image = cv2.imread('images/opencv_inv.png', 0)\n", 22 | "\n", 23 | "cv2.imshow('Original', image)\n", 24 | "cv2.waitKey(0)\n", 25 | "\n", 26 | "# Let's define our kernel size\n", 27 | "kernel = np.ones((5,5), np.uint8)\n", 28 | "\n", 29 | "# Now we erode\n", 30 | "erosion = cv2.erode(image, kernel, iterations = 1)\n", 31 | "cv2.imshow('Erosion', erosion)\n", 32 | "cv2.waitKey(0)\n", 33 | "\n", 34 | "# \n", 35 | "dilation = cv2.dilate(image, kernel, iterations = 1)\n", 36 | "cv2.imshow('Dilation', dilation)\n", 37 | "cv2.waitKey(0)\n", 38 | "\n", 39 | "# Opening - Good for removing noise\n", 40 | "opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)\n", 41 | "cv2.imshow('Opening', opening)\n", 42 | "cv2.waitKey(0)\n", 43 | "\n", 44 | "# Closing - Good for removing noise\n", 45 | "closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)\n", 46 | "cv2.imshow('Closing', closing)\n", 47 | "cv2.waitKey(0)\n", 48 | "\n", 49 | "\n", 50 | "cv2.destroyAllWindows()" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "### There are some other less popular morphology operations, see the official OpenCV site:\n", 58 | "\n", 59 | "http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": { 75 | "collapsed": true 76 | }, 77 | "outputs": [], 78 | "source": [] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": { 84 | "collapsed": true 85 | }, 86 | "outputs": [], 87 | "source": [] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 2", 93 | "language": "python", 94 | "name": "python2" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 2 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython2", 106 | "version": "2.7.11" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 0 111 | } 112 | -------------------------------------------------------------------------------- /Lecture 3.13 - Edge Detection & Image Gradients.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Edge Detection & Image Gradients" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 8, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "image = cv2.imread('images/input.jpg',0)\n", 22 | "\n", 23 | "height, width = image.shape\n", 24 | "\n", 25 | "# Extract Sobel Edges\n", 26 | "sobel_x = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)\n", 27 | "sobel_y = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)\n", 28 | "\n", 29 | "cv2.imshow('Original', image)\n", 30 | "cv2.waitKey(0)\n", 31 | "cv2.imshow('Sobel X', sobel_x)\n", 32 | "cv2.waitKey(0)\n", 33 | "cv2.imshow('Sobel Y', sobel_y)\n", 34 | "cv2.waitKey(0)\n", 35 | "\n", 36 | "sobel_OR = cv2.bitwise_or(sobel_x, sobel_y)\n", 37 | "cv2.imshow('sobel_OR', sobel_OR)\n", 38 | "cv2.waitKey(0)\n", 39 | "\n", 40 | "laplacian = cv2.Laplacian(image, cv2.CV_64F)\n", 41 | "cv2.imshow('Laplacian', laplacian)\n", 42 | "cv2.waitKey(0)\n", 43 | "\n", 44 | "\n", 45 | "\n", 46 | "\n", 47 | "\n", 48 | "## Then, we need to provide two values: threshold1 and threshold2. Any gradient value larger than threshold2\n", 49 | "# is considered to be an edge. Any value below threshold1 is considered not to be an edge. \n", 50 | "#Values in between threshold1 and threshold2 are either classified as edges or non-edges based on how their \n", 51 | "#intensities are “connected”. In this case, any gradient values below 60 are considered non-edges\n", 52 | "#whereas any values above 120 are considered edges.\n", 53 | "\n", 54 | "\n", 55 | "# Canny Edge Detection uses gradient values as thresholds\n", 56 | "# The first threshold gradient\n", 57 | "canny = cv2.Canny(image, 50, 120)\n", 58 | "cv2.imshow('Canny', canny)\n", 59 | "cv2.waitKey(0)\n", 60 | "\n", 61 | "cv2.destroyAllWindows()" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": { 68 | "collapsed": true 69 | }, 70 | "outputs": [], 71 | "source": [] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 2", 77 | "language": "python", 78 | "name": "python2" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 2 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython2", 90 | "version": "2.7.11" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 0 95 | } 96 | -------------------------------------------------------------------------------- /Lecture 3.14 - Perspective & Affine Transforms.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "## Getting Perpsective Transform" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 4, 14 | "metadata": { 15 | "collapsed": false 16 | }, 17 | "outputs": [], 18 | "source": [ 19 | "import cv2\n", 20 | "import numpy as np\n", 21 | "import matplotlib.pyplot as plt\n", 22 | "\n", 23 | "image = cv2.imread('images/scan.jpg')\n", 24 | "\n", 25 | "cv2.imshow('Original', image)\n", 26 | "cv2.waitKey(0)\n", 27 | "\n", 28 | "# Cordinates of the 4 corners of the original image\n", 29 | "points_A = np.float32([[320,15], [700,215], [85,610], [530,780]])\n", 30 | "\n", 31 | "# Cordinates of the 4 corners of the desired output\n", 32 | "# We use a ratio of an A4 Paper 1 : 1.41\n", 33 | "points_B = np.float32([[0,0], [420,0], [0,594], [420,594]])\n", 34 | " \n", 35 | "# Use the two sets of four points to compute \n", 36 | "# the Perspective Transformation matrix, M \n", 37 | "M = cv2.getPerspectiveTransform(points_A, points_B)\n", 38 | " \n", 39 | "warped = cv2.warpPerspective(image, M, (420,594))\n", 40 | " \n", 41 | "cv2.imshow('warpPerspective', warped)\n", 42 | "cv2.waitKey(0)\n", 43 | "cv2.destroyAllWindows()" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "## In affine transforms you only need 3 coordiantes to obtain the correct transform" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 5, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "import cv2\n", 62 | "import numpy as np\n", 63 | "import matplotlib.pyplot as plt\n", 64 | "\n", 65 | "image = cv2.imread('images/ex2.jpg')\n", 66 | "rows,cols,ch = image.shape\n", 67 | "\n", 68 | "cv2.imshow('Original', image)\n", 69 | "cv2.waitKey(0)\n", 70 | "\n", 71 | "# Cordinates of the 4 corners of the original image\n", 72 | "points_A = np.float32([[320,15], [700,215], [85,610]])\n", 73 | "\n", 74 | "# Cordinates of the 4 corners of the desired output\n", 75 | "# We use a ratio of an A4 Paper 1 : 1.41\n", 76 | "points_B = np.float32([[0,0], [420,0], [0,594]])\n", 77 | " \n", 78 | "# Use the two sets of four points to compute \n", 79 | "# the Perspective Transformation matrix, M \n", 80 | "M = cv2.getAffineTransform(points_A, points_B)\n", 81 | "\n", 82 | "warped = cv2.warpAffine(image, M, (cols, rows))\n", 83 | " \n", 84 | "cv2.imshow('warpPerspective', warped)\n", 85 | "cv2.waitKey(0)\n", 86 | "cv2.destroyAllWindows()" 87 | ] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 2", 93 | "language": "python", 94 | "name": "python2" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 2 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython2", 106 | "version": "2.7.11" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 0 111 | } 112 | -------------------------------------------------------------------------------- /Lecture 3.15 - Mini Project # 1 - Live Sketch Using Webcam.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project # 1 - Live Sketch Using Webcam" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "# Our sketch generating function\n", 22 | "def sketch(image):\n", 23 | " # Convert image to grayscale\n", 24 | " img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 25 | " \n", 26 | " # Clean up image using Guassian Blur\n", 27 | " img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0)\n", 28 | " \n", 29 | " # Extract edges\n", 30 | " canny_edges = cv2.Canny(img_gray_blur, 10, 70)\n", 31 | " \n", 32 | " # Do an invert binarize the image \n", 33 | " ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV)\n", 34 | " return mask\n", 35 | "\n", 36 | "\n", 37 | "# Initialize webcam, cap is the object provided by VideoCapture\n", 38 | "# It contains a boolean indicating if it was sucessful (ret)\n", 39 | "# It also contains the images collected from the webcam (frame)\n", 40 | "cap = cv2.VideoCapture(0)\n", 41 | "\n", 42 | "while True:\n", 43 | " ret, frame = cap.read()\n", 44 | " cv2.imshow('Our Live Sketcher', sketch(frame))\n", 45 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 46 | " break\n", 47 | " \n", 48 | "# Release camera and close windows\n", 49 | "cap.release()\n", 50 | "cv2.destroyAllWindows() " 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "outputs": [], 60 | "source": [] 61 | } 62 | ], 63 | "metadata": { 64 | "kernelspec": { 65 | "display_name": "Python 2", 66 | "language": "python", 67 | "name": "python2" 68 | }, 69 | "language_info": { 70 | "codemirror_mode": { 71 | "name": "ipython", 72 | "version": 2 73 | }, 74 | "file_extension": ".py", 75 | "mimetype": "text/x-python", 76 | "name": "python", 77 | "nbconvert_exporter": "python", 78 | "pygments_lexer": "ipython2", 79 | "version": "2.7.11" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 0 84 | } 85 | -------------------------------------------------------------------------------- /Lecture 3.2 - Translations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Translations\n", 8 | "\n", 9 | "This an affine transform that simply shifts the position of an image.\n", 10 | "\n", 11 | "We use cv2.warpAffine to implement these transformations.\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "import cv2\n", 23 | "import numpy as np\n", 24 | "\n", 25 | "image = cv2.imread('images/input.jpg')\n", 26 | "\n", 27 | "# Store height and width of the image\n", 28 | "height, width = image.shape[:2]\n", 29 | "\n", 30 | "quarter_height, quarter_width = height/4, width/4\n", 31 | "\n", 32 | "# | 1 0 Tx |\n", 33 | "# T = | 0 1 Ty |\n", 34 | "\n", 35 | "# T is our translation matrix\n", 36 | "T = np.float32([[1, 0, quarter_width], [0, 1,quarter_height]])\n", 37 | "\n", 38 | "# We use warpAffine to transform the image using the matrix, T\n", 39 | "img_translation = cv2.warpAffine(image, T, (width, height))\n", 40 | "cv2.imshow('Translation', img_translation)\n", 41 | "cv2.waitKey()\n", 42 | "cv2.destroyAllWindows()" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[[ 1. 0. 311.]\n", 66 | " [ 0. 1. 207.]]\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "# Let's take a look at T\n", 72 | "\n", 73 | "print T" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": { 80 | "collapsed": true 81 | }, 82 | "outputs": [], 83 | "source": [] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": { 89 | "collapsed": true 90 | }, 91 | "outputs": [], 92 | "source": [] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "collapsed": true 99 | }, 100 | "outputs": [], 101 | "source": [] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": { 107 | "collapsed": true 108 | }, 109 | "outputs": [], 110 | "source": [] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": { 116 | "collapsed": true 117 | }, 118 | "outputs": [], 119 | "source": [] 120 | } 121 | ], 122 | "metadata": { 123 | "kernelspec": { 124 | "display_name": "Python 2", 125 | "language": "python", 126 | "name": "python2" 127 | }, 128 | "language_info": { 129 | "codemirror_mode": { 130 | "name": "ipython", 131 | "version": 2 132 | }, 133 | "file_extension": ".py", 134 | "mimetype": "text/x-python", 135 | "name": "python", 136 | "nbconvert_exporter": "python", 137 | "pygments_lexer": "ipython2", 138 | "version": "2.7.11" 139 | } 140 | }, 141 | "nbformat": 4, 142 | "nbformat_minor": 0 143 | } 144 | -------------------------------------------------------------------------------- /Lecture 3.3 - Rotations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Rotations\n", 8 | "\n", 9 | "cv2.getRotationMatrix2D(rotation_center_x, rotation_center_y, angle of rotation, scale)\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "image = cv2.imread('images/input.jpg')\n", 22 | "height, width = image.shape[:2]\n", 23 | "\n", 24 | "# Divide by two to rototate the image around its centre\n", 25 | "rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 90, .5)\n", 26 | "\n", 27 | "rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))\n", 28 | "\n", 29 | "cv2.imshow('Rotated Image', rotated_image)\n", 30 | "cv2.waitKey()\n", 31 | "cv2.destroyAllWindows()" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "### Notice all the black space surrounding the image.\n", 39 | "\n", 40 | "We could now crop the image as we can calculate it's new size (we haven't learned cropping yet!).\n", 41 | "\n", 42 | "But here's another method for simple rotations that uses the cv2.transpose function" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 6, 48 | "metadata": { 49 | "collapsed": true 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "#Other Option to Rotate\n", 54 | "img = cv2.imread('images/input.jpg')\n", 55 | "\n", 56 | "rotated_image = cv2.transpose(img)\n", 57 | "\n", 58 | "cv2.imshow('Rotated Image - Method 2', rotated_image)\n", 59 | "cv2.waitKey()\n", 60 | "cv2.destroyAllWindows()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": { 67 | "collapsed": true 68 | }, 69 | "outputs": [], 70 | "source": [] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 7, 75 | "metadata": { 76 | "collapsed": true 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "# Let's now to a horizontal flip.\n", 81 | "flipped = cv2.flip(image, 1)\n", 82 | "cv2.imshow('Horizontal Flip', flipped) \n", 83 | "cv2.waitKey()\n", 84 | "cv2.destroyAllWindows()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": { 91 | "collapsed": true 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "collapsed": true 101 | }, 102 | "outputs": [], 103 | "source": [] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Python 3", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.5.3" 123 | } 124 | }, 125 | "nbformat": 4, 126 | "nbformat_minor": 1 127 | } 128 | -------------------------------------------------------------------------------- /Lecture 3.4 - Scaling, re-sizing and interpolations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Scaling, re-sizing and interpolations\n", 8 | "\n", 9 | "Re-sizing is very easy using the cv2.resize function, it's arguments are:\n", 10 | "\n", 11 | "cv2.resize(image, dsize(output image size), x scale, y scale, interpolation)\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "import cv2\n", 23 | "import numpy as np\n", 24 | "\n", 25 | "# load our input image\n", 26 | "image = cv2.imread('images/input.jpg')\n", 27 | "\n", 28 | "# Let's make our image 3/4 of it's original size\n", 29 | "image_scaled = cv2.resize(image, None, fx=0.75, fy=0.75)\n", 30 | "cv2.imshow('Scaling - Linear Interpolation', image_scaled) \n", 31 | "cv2.waitKey()\n", 32 | "\n", 33 | "# Let's double the size of our image\n", 34 | "img_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)\n", 35 | "cv2.imshow('Scaling - Cubic Interpolation', img_scaled)\n", 36 | "cv2.waitKey()\n", 37 | "\n", 38 | "# Let's skew the re-sizing by setting exact dimensions\n", 39 | "img_scaled = cv2.resize(image, (900, 400), interpolation = cv2.INTER_AREA)\n", 40 | "cv2.imshow('Scaling - Skewed Size', img_scaled) \n", 41 | "cv2.waitKey()\n", 42 | "\n", 43 | "cv2.destroyAllWindows()" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": { 50 | "collapsed": true 51 | }, 52 | "outputs": [], 53 | "source": [] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 2", 68 | "language": "python", 69 | "name": "python2" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 2 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython2", 81 | "version": "2.7.11" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 0 86 | } 87 | -------------------------------------------------------------------------------- /Lecture 3.5 - Image Pyramids.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Image Pyramids\n", 8 | "\n", 9 | "Useful when scaling images in object detection." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 3, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import cv2\n", 21 | "\n", 22 | "image = cv2.imread('images/input.jpg')\n", 23 | "\n", 24 | "smaller = cv2.pyrDown(image)\n", 25 | "larger = cv2.pyrUp(smaller)\n", 26 | "\n", 27 | "cv2.imshow('Original', image )\n", 28 | "\n", 29 | "cv2.imshow('Smaller ', smaller )\n", 30 | "cv2.imshow('Larger ', larger )\n", 31 | "cv2.waitKey(0)\n", 32 | "cv2.destroyAllWindows()" 33 | ] 34 | } 35 | ], 36 | "metadata": { 37 | "kernelspec": { 38 | "display_name": "Python 2", 39 | "language": "python", 40 | "name": "python2" 41 | }, 42 | "language_info": { 43 | "codemirror_mode": { 44 | "name": "ipython", 45 | "version": 2 46 | }, 47 | "file_extension": ".py", 48 | "mimetype": "text/x-python", 49 | "name": "python", 50 | "nbconvert_exporter": "python", 51 | "pygments_lexer": "ipython2", 52 | "version": "2.7.11" 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 0 57 | } 58 | -------------------------------------------------------------------------------- /Lecture 3.6 - Cropping.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Cropping\n", 8 | "\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "metadata": { 15 | "collapsed": false 16 | }, 17 | "outputs": [], 18 | "source": [ 19 | "import cv2\n", 20 | "import numpy as np\n", 21 | "\n", 22 | "image = cv2.imread('images/input.jpg')\n", 23 | "height, width = image.shape[:2]\n", 24 | "\n", 25 | "# Let's get the starting pixel coordiantes (top left of cropping rectangle)\n", 26 | "start_row, start_col = int(height * .25), int(width * .25)\n", 27 | "\n", 28 | "# Let's get the ending pixel coordinates (bottom right)\n", 29 | "end_row, end_col = int(height * .75), int(width * .75)\n", 30 | "\n", 31 | "# Simply use indexing to crop out the rectangle we desire\n", 32 | "cropped = image[start_row:end_row , start_col:end_col]\n", 33 | "\n", 34 | "cv2.imshow(\"Original Image\", image)\n", 35 | "cv2.waitKey(0) \n", 36 | "cv2.imshow(\"Cropped Image\", cropped) \n", 37 | "cv2.waitKey(0) \n", 38 | "cv2.destroyAllWindows()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 2", 54 | "language": "python", 55 | "name": "python2" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 2 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython2", 67 | "version": "2.7.11" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 0 72 | } 73 | -------------------------------------------------------------------------------- /Lecture 3.7 - Arithmetic Operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Arithmetic Operations\n", 8 | "\n", 9 | "These are simple operations that allow us to directly add or subract to the color intensity.\n", 10 | "\n", 11 | "Calculates the per-element operation of two arrays. The overall effect is increasing or decreasing brightness." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "import cv2\n", 21 | "import numpy as np\n", 22 | "\n", 23 | "image = cv2.imread('images/input.jpg')\n", 24 | "\n", 25 | "# Create a matrix of ones, then multiply it by a scaler of 100 \n", 26 | "# This gives a matrix with same dimesions of our image with all values being 100\n", 27 | "M = np.ones(image.shape, dtype = \"uint8\") * 175 \n", 28 | "\n", 29 | "# We use this to add this matrix M, to our image\n", 30 | "# Notice the increase in brightness\n", 31 | "added = cv2.add(image, M)\n", 32 | "cv2.imshow(\"Added\", added)\n", 33 | "\n", 34 | "# Likewise we can also subtract\n", 35 | "# Notice the decrease in brightness\n", 36 | "subtracted = cv2.subtract(image, M)\n", 37 | "cv2.imshow(\"Subtracted\", subtracted)\n", 38 | "\n", 39 | "cv2.waitKey(0)\n", 40 | "cv2.destroyAllWindows()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 5, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "array([[[75, 75, 75],\n", 52 | " [75, 75, 75],\n", 53 | " [75, 75, 75],\n", 54 | " ..., \n", 55 | " [75, 75, 75],\n", 56 | " [75, 75, 75],\n", 57 | " [75, 75, 75]],\n", 58 | "\n", 59 | " [[75, 75, 75],\n", 60 | " [75, 75, 75],\n", 61 | " [75, 75, 75],\n", 62 | " ..., \n", 63 | " [75, 75, 75],\n", 64 | " [75, 75, 75],\n", 65 | " [75, 75, 75]],\n", 66 | "\n", 67 | " [[75, 75, 75],\n", 68 | " [75, 75, 75],\n", 69 | " [75, 75, 75],\n", 70 | " ..., \n", 71 | " [75, 75, 75],\n", 72 | " [75, 75, 75],\n", 73 | " [75, 75, 75]],\n", 74 | "\n", 75 | " ..., \n", 76 | " [[75, 75, 75],\n", 77 | " [75, 75, 75],\n", 78 | " [75, 75, 75],\n", 79 | " ..., \n", 80 | " [75, 75, 75],\n", 81 | " [75, 75, 75],\n", 82 | " [75, 75, 75]],\n", 83 | "\n", 84 | " [[75, 75, 75],\n", 85 | " [75, 75, 75],\n", 86 | " [75, 75, 75],\n", 87 | " ..., \n", 88 | " [75, 75, 75],\n", 89 | " [75, 75, 75],\n", 90 | " [75, 75, 75]],\n", 91 | "\n", 92 | " [[75, 75, 75],\n", 93 | " [75, 75, 75],\n", 94 | " [75, 75, 75],\n", 95 | " ..., \n", 96 | " [75, 75, 75],\n", 97 | " [75, 75, 75],\n", 98 | " [75, 75, 75]]], dtype=uint8)" 99 | ] 100 | }, 101 | "execution_count": 5, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "M = np.ones(image.shape, dtype = \"uint8\") * 75 \n", 108 | "M" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [] 119 | } 120 | ], 121 | "metadata": { 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.5.3" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 1 142 | } 143 | -------------------------------------------------------------------------------- /Lecture 3.8 - Bitwise Operations and Masking.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Bitwise Operations and Masking\n", 8 | "\n", 9 | "To demonstrate these operations let's create some simple images" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 5, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import cv2\n", 21 | "import numpy as np\n", 22 | "\n", 23 | "# If you're wondering why only two dimensions, well this is a grayscale image, \n", 24 | "# if we doing a colored image, we'd use \n", 25 | "# rectangle = np.zeros((300, 300, 3),np.uint8)\n", 26 | "\n", 27 | "# Making a sqare\n", 28 | "square = np.zeros((300, 300), np.uint8)\n", 29 | "cv2.rectangle(square, (50, 50), (250, 250), 255, -2)\n", 30 | "cv2.imshow(\"Square\", square)\n", 31 | "cv2.waitKey(0)\n", 32 | "\n", 33 | "# Making a ellipse\n", 34 | "ellipse = np.zeros((300, 300), np.uint8)\n", 35 | "cv2.ellipse(ellipse, (150, 150), (150, 150), 30, 0, 180, 255, -1)\n", 36 | "cv2.imshow(\"Ellipse\", ellipse)\n", 37 | "cv2.waitKey(0)\n", 38 | "\n", 39 | "cv2.destroyAllWindows()" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "### Experimenting with some bitwise operations" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 6, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "# Shows only where they intersect\n", 58 | "And = cv2.bitwise_and(square, ellipse)\n", 59 | "cv2.imshow(\"AND\", And)\n", 60 | "cv2.waitKey(0)\n", 61 | "\n", 62 | "# Shows where either square or ellipse is \n", 63 | "bitwiseOr = cv2.bitwise_or(square, ellipse)\n", 64 | "cv2.imshow(\"OR\", bitwiseOr)\n", 65 | "cv2.waitKey(0) \n", 66 | "\n", 67 | "# Shows where either exist by itself\n", 68 | "bitwiseXor = cv2.bitwise_xor(square, ellipse)\n", 69 | "cv2.imshow(\"XOR\", bitwiseXor)\n", 70 | "cv2.waitKey(0)\n", 71 | "\n", 72 | "# Shows everything that isn't part of the square\n", 73 | "bitwiseNot_sq = cv2.bitwise_not(square)\n", 74 | "cv2.imshow(\"NOT - square\", bitwiseNot_sq)\n", 75 | "cv2.waitKey(0)\n", 76 | "\n", 77 | "### Notice the last operation inverts the image totally\n", 78 | "\n", 79 | "cv2.destroyAllWindows()" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": { 86 | "collapsed": true 87 | }, 88 | "outputs": [], 89 | "source": [ 90 | "\n", 91 | "\n", 92 | "\n", 93 | "\n", 94 | "\n" 95 | ] 96 | } 97 | ], 98 | "metadata": { 99 | "kernelspec": { 100 | "display_name": "Python 2", 101 | "language": "python", 102 | "name": "python2" 103 | }, 104 | "language_info": { 105 | "codemirror_mode": { 106 | "name": "ipython", 107 | "version": 2 108 | }, 109 | "file_extension": ".py", 110 | "mimetype": "text/x-python", 111 | "name": "python", 112 | "nbconvert_exporter": "python", 113 | "pygments_lexer": "ipython2", 114 | "version": "2.7.11" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 0 119 | } 120 | -------------------------------------------------------------------------------- /Lecture 3.9 - Convolutions and Blurring.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Convolutions and Blurring" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import cv2\n", 19 | "import numpy as np\n", 20 | "\n", 21 | "image = cv2.imread('images/elephant.jpg')\n", 22 | "cv2.imshow('Original Image', image)\n", 23 | "cv2.waitKey(0)\n", 24 | "\n", 25 | "# Creating our 3 x 3 kernel\n", 26 | "kernel_3x3 = np.ones((3, 3), np.float32) / 9\n", 27 | "\n", 28 | "# We use the cv2.fitler2D to conovlve the kernal with an image \n", 29 | "blurred = cv2.filter2D(image, -1, kernel_3x3)\n", 30 | "cv2.imshow('3x3 Kernel Blurring', blurred)\n", 31 | "cv2.waitKey(0)\n", 32 | "\n", 33 | "# Creating our 7 x 7 kernel\n", 34 | "kernel_7x7 = np.ones((7, 7), np.float32) / 49\n", 35 | "\n", 36 | "blurred2 = cv2.filter2D(image, -1, kernel_7x7)\n", 37 | "cv2.imshow('7x7 Kernel Blurring', blurred2)\n", 38 | "cv2.waitKey(0)\n", 39 | "\n", 40 | "cv2.destroyAllWindows()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": { 47 | "collapsed": true 48 | }, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "### Other commonly used blurring methods in OpenCV" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 5, 62 | "metadata": { 63 | "collapsed": false 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "import cv2\n", 68 | "import numpy as np\n", 69 | "\n", 70 | "image = cv2.imread('images/elephant.jpg')\n", 71 | "\n", 72 | "# Averaging done by convolving the image with a normalized box filter. \n", 73 | "# This takes the pixels under the box and replaces the central element\n", 74 | "# Box size needs to odd and positive \n", 75 | "blur = cv2.blur(image, (3,3))\n", 76 | "cv2.imshow('Averaging', blur)\n", 77 | "cv2.waitKey(0)\n", 78 | "\n", 79 | "# Instead of box filter, gaussian kernel\n", 80 | "Gaussian = cv2.GaussianBlur(image, (7,7), 0)\n", 81 | "cv2.imshow('Gaussian Blurring', Gaussian)\n", 82 | "cv2.waitKey(0)\n", 83 | "\n", 84 | "# Takes median of all the pixels under kernel area and central \n", 85 | "# element is replaced with this median value\n", 86 | "median = cv2.medianBlur(image, 5)\n", 87 | "cv2.imshow('Median Blurring', median)\n", 88 | "cv2.waitKey(0)\n", 89 | "\n", 90 | "# Bilateral is very effective in noise removal while keeping edges sharp\n", 91 | "bilateral = cv2.bilateralFilter(image, 9, 75, 75)\n", 92 | "cv2.imshow('Bilateral Blurring', bilateral)\n", 93 | "cv2.waitKey(0)\n", 94 | "cv2.destroyAllWindows()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "## Image De-noising - Non-Local Means Denoising" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "collapsed": true 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "import numpy as np\n", 113 | "import cv2\n", 114 | "\n", 115 | "image = cv2.imread('images/elephant.jpg')\n", 116 | "\n", 117 | "# Parameters, after None are - the filter strength 'h' (5-10 is a good range)\n", 118 | "# Next is hForColorComponents, set as same value as h again\n", 119 | "# \n", 120 | "dst = cv2.fastNlMeansDenoisingColored(image, None, 6, 6, 7, 21)\n", 121 | "\n", 122 | "cv2.imshow('Fast Means Denoising', dst)\n", 123 | "cv2.waitKey(0)\n", 124 | "\n", 125 | "cv2.destroyAllWindows()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "**There are 4 variations of Non-Local Means Denoising:**\n", 133 | "\n", 134 | "- cv2.fastNlMeansDenoising() - works with a single grayscale images\n", 135 | "- cv2.fastNlMeansDenoisingColored() - works with a color image.\n", 136 | "- cv2.fastNlMeansDenoisingMulti() - works with image sequence captured in short period of time (grayscale images)\n", 137 | "- cv2.fastNlMeansDenoisingColoredMulti() - same as above, but for color images." 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "collapsed": true 145 | }, 146 | "outputs": [], 147 | "source": [] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": { 153 | "collapsed": true 154 | }, 155 | "outputs": [], 156 | "source": [] 157 | } 158 | ], 159 | "metadata": { 160 | "kernelspec": { 161 | "display_name": "Python 2", 162 | "language": "python", 163 | "name": "python2" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 2 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython2", 175 | "version": "2.7.11" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 0 180 | } 181 | -------------------------------------------------------------------------------- /Lecture 4.1 - Understanding Contours.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Contours" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Number of Contours found = 3\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "import cv2\n", 25 | "import numpy as np\n", 26 | "\n", 27 | "# Let's load a simple image with 3 black squares\n", 28 | "image = cv2.imread('images/shapes.jpg')\n", 29 | "cv2.imshow('Input Image', image)\n", 30 | "cv2.waitKey(0)\n", 31 | "\n", 32 | "# Grayscale\n", 33 | "gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n", 34 | "\n", 35 | "# Find Canny edges\n", 36 | "edged = cv2.Canny(gray, 30, 200)\n", 37 | "cv2.imshow('Canny Edges', edged)\n", 38 | "cv2.waitKey(0)\n", 39 | "\n", 40 | "# Finding Contours\n", 41 | "# Use a copy of your image e.g. edged.copy(), since findContours alters the image\n", 42 | "_,contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n", 43 | "cv2.imshow('Canny Edges After Contouring', edged)\n", 44 | "cv2.waitKey(0)\n", 45 | "\n", 46 | "print(\"Number of Contours found = \" + str(len(contours)))\n", 47 | "\n", 48 | "# Draw all contours\n", 49 | "# Use '-1' as the 3rd parameter to draw all\n", 50 | "cv2.drawContours(image, contours, -1, (0,255,0), 3)\n", 51 | "\n", 52 | "cv2.imshow('Contours', image)\n", 53 | "cv2.waitKey(0)\n", 54 | "cv2.destroyAllWindows()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "**cv2.findContours(image, Retrieval Mode, Approximation Method)**\n", 62 | "\n", 63 | "Returns -> contours, hierarchy\n", 64 | "\n", 65 | "**NOTE** In OpenCV 3.X, findContours returns a 3rd argument which is ret (or a boolean indicating if the function was successfully run). \n", 66 | "\n", 67 | "If you're using OpenCV 3.X replace line 12 with:\n", 68 | "\n", 69 | "_, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n", 70 | "\n", 71 | "The variable 'contours' are stored as a numpy array of (x,y) points that form the contour\n", 72 | "\n", 73 | "While, 'hierarchy' describes the child-parent relationships between contours (i.e. contours within contours)\n", 74 | "\n", 75 | "\n", 76 | "\n", 77 | "#### Approximation Methods\n", 78 | "\n", 79 | "Using cv2.CHAIN_APPROX_NONE stores all the boundary points. But we don't necessarily need all bounding points. If the points form a straight line, we only need the start and ending points of that line.\n", 80 | "\n", 81 | "Using cv2.CHAIN_APPROX_SIMPLE instead only provides these start and end points of bounding contours, thus resulting in much more efficent storage of contour information.." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": true 96 | }, 97 | "outputs": [], 98 | "source": [] 99 | } 100 | ], 101 | "metadata": { 102 | "kernelspec": { 103 | "display_name": "Python 3", 104 | "language": "python", 105 | "name": "python3" 106 | }, 107 | "language_info": { 108 | "codemirror_mode": { 109 | "name": "ipython", 110 | "version": 3 111 | }, 112 | "file_extension": ".py", 113 | "mimetype": "text/x-python", 114 | "name": "python", 115 | "nbconvert_exporter": "python", 116 | "pygments_lexer": "ipython3", 117 | "version": "3.5.3" 118 | } 119 | }, 120 | "nbformat": 4, 121 | "nbformat_minor": 1 122 | } 123 | -------------------------------------------------------------------------------- /Lecture 4.2 - Sorting Contours.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Sorting Contours\n", 8 | "\n", 9 | "We can sort contours in many ways." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Number of contours found = 4\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import cv2\n", 27 | "import numpy as np\n", 28 | "\n", 29 | "# Load our image\n", 30 | "image = cv2.imread('images/bunchofshapes.jpg')\n", 31 | "cv2.imshow('0 - Original Image', image)\n", 32 | "cv2.waitKey(0)\n", 33 | "\n", 34 | "# Create a black image with same dimensions as our loaded image\n", 35 | "blank_image = np.zeros((image.shape[0], image.shape[1], 3))\n", 36 | "\n", 37 | "# Create a copy of our original image\n", 38 | "orginal_image = image\n", 39 | "\n", 40 | "# Grayscale our image\n", 41 | "gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n", 42 | "\n", 43 | "# Find Canny edges\n", 44 | "edged = cv2.Canny(gray, 50, 200)\n", 45 | "cv2.imshow('1 - Canny Edges', edged)\n", 46 | "cv2.waitKey(0)\n", 47 | "\n", 48 | "# Find contours and print how many were found\n", 49 | "_,contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n", 50 | "print (\"Number of contours found = \", len(contours))\n", 51 | "\n", 52 | "#Draw all contours\n", 53 | "cv2.drawContours(blank_image, contours, -1, (0,255,0), 3)\n", 54 | "cv2.imshow('2 - All Contours over blank image', blank_image)\n", 55 | "cv2.waitKey(0)\n", 56 | "\n", 57 | "# Draw all contours over blank image\n", 58 | "cv2.drawContours(image, contours, -1, (0,255,0), 3)\n", 59 | "cv2.imshow('3 - All Contours', image)\n", 60 | "cv2.waitKey(0)\n", 61 | "\n", 62 | "cv2.destroyAllWindows()" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "### Let's now sort by area" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 6, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "Contor Areas before sorting\n", 82 | "[20587.5, 22900.5, 66581.5, 90222.0]\n", 83 | "Contor Areas after sorting\n", 84 | "[90222.0, 66581.5, 22900.5, 20587.5]\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "import cv2\n", 90 | "import numpy as np\n", 91 | "\n", 92 | "# Function we'll use to display contour area\n", 93 | "\n", 94 | "def get_contour_areas(contours):\n", 95 | " # returns the areas of all contours as list\n", 96 | " all_areas = []\n", 97 | " for cnt in contours:\n", 98 | " area = cv2.contourArea(cnt)\n", 99 | " all_areas.append(area)\n", 100 | " return all_areas\n", 101 | "\n", 102 | "# Load our image\n", 103 | "image = cv2.imread('images/bunchofshapes.jpg')\n", 104 | "orginal_image = image\n", 105 | "\n", 106 | "# Let's print the areas of the contours before sorting\n", 107 | "print (\"Contor Areas before sorting\") \n", 108 | "print (get_contour_areas(contours))\n", 109 | "\n", 110 | "# Sort contours large to small\n", 111 | "sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)\n", 112 | "#sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)[:3]\n", 113 | "\n", 114 | "print (\"Contor Areas after sorting\")\n", 115 | "print (get_contour_areas(sorted_contours))\n", 116 | "\n", 117 | "# Iterate over our contours and draw one at a time\n", 118 | "for c in sorted_contours:\n", 119 | " cv2.drawContours(orginal_image, [c], -1, (255,0,0), 3)\n", 120 | " cv2.waitKey(0)\n", 121 | " cv2.imshow('Contours by area', orginal_image)\n", 122 | "\n", 123 | "cv2.waitKey(0)\n", 124 | "cv2.destroyAllWindows()" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 10, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "ename": "SyntaxError", 134 | "evalue": "invalid syntax (, line 37)", 135 | "output_type": "error", 136 | "traceback": [ 137 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m37\u001b[0m\n\u001b[0;31m contours_left_to_right = sorted(contours, key = x_cord_contour, reverse = False)n\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "import cv2\n", 143 | "import numpy as np\n", 144 | "\n", 145 | "# Functions we'll use for sorting by position\n", 146 | "\n", 147 | "def x_cord_contour(contours):\n", 148 | " #Returns the X cordinate for the contour centroid\n", 149 | " if cv2.contourArea(contours) > 10:\n", 150 | " M = cv2.moments(contours)\n", 151 | " return (int(M['m10']/M['m00']))\n", 152 | "\n", 153 | " \n", 154 | "def label_contour_center(image, c):\n", 155 | " # Places a red circle on the centers of contours\n", 156 | " M = cv2.moments(c)\n", 157 | " cx = int(M['m10'] / M['m00'])\n", 158 | " cy = int(M['m01'] / M['m00'])\n", 159 | " \n", 160 | " # Draw the countour number on the image\n", 161 | " cv2.circle(image,(cx,cy), 10, (0,0,255), -1)\n", 162 | " return image\n", 163 | "\n", 164 | "\n", 165 | "# Load our image\n", 166 | "image = cv2.imread('images/bunchofshapes.jpg')\n", 167 | "orginal_image = image.copy()\n", 168 | "\n", 169 | "\n", 170 | "# Computer Center of Mass or centroids and draw them on our image\n", 171 | "for (i, c) in enumerate(contours):\n", 172 | " orig = label_contour_center(image, c)\n", 173 | " \n", 174 | "cv2.imshow(\"4 - Contour Centers \", image)\n", 175 | "cv2.waitKey(0)\n", 176 | "\n", 177 | "# Sort by left to right using our x_cord_contour function\n", 178 | "contours_left_to_right = sorted(contours, key = x_cord_contour, reverse = False)\n", 179 | "\n", 180 | "\n", 181 | "# Labeling Contours left to right\n", 182 | "for (i,c) in enumerate(contours_left_to_right):\n", 183 | " cv2.drawContours(orginal_image, [c], -1, (0,0,255), 3) \n", 184 | " M = cv2.moments(c)\n", 185 | " cx = int(M['m10'] / M['m00'])\n", 186 | " cy = int(M['m01'] / M['m00'])\n", 187 | " cv2.putText(orginal_image, str(i+1), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)\n", 188 | " cv2.imshow('6 - Left to Right Contour', orginal_image)\n", 189 | " cv2.waitKey(0)\n", 190 | " (x, y, w, h) = cv2.boundingRect(c) \n", 191 | " \n", 192 | " # Let's now crop each contour and save these images\n", 193 | " cropped_contour = orginal_image[y:y + h, x:x + w]\n", 194 | " image_name = \"output_shape_number_\" + str(i+1) + \".jpg\"\n", 195 | " print (image_name)\n", 196 | " cv2.imwrite(image_name, cropped_contour)\n", 197 | " \n", 198 | "cv2.destroyAllWindows()\n", 199 | "\n", 200 | "\n" 201 | ] 202 | } 203 | ], 204 | "metadata": { 205 | "kernelspec": { 206 | "display_name": "Python 3", 207 | "language": "python", 208 | "name": "python3" 209 | }, 210 | "language_info": { 211 | "codemirror_mode": { 212 | "name": "ipython", 213 | "version": 3 214 | }, 215 | "file_extension": ".py", 216 | "mimetype": "text/x-python", 217 | "name": "python", 218 | "nbconvert_exporter": "python", 219 | "pygments_lexer": "ipython3", 220 | "version": "3.5.3" 221 | } 222 | }, 223 | "nbformat": 4, 224 | "nbformat_minor": 1 225 | } 226 | -------------------------------------------------------------------------------- /Lecture 4.3 - Approximating Contours and Convex Hull .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Approximating Contours and Convex Hull \n", 8 | "\n", 9 | "***cv2.approxPolyDP(contour, Approximation Accuracy, Closed)***\n", 10 | "- **contour** – is the individual contour we wish to approximate\n", 11 | "- **Approximation Accuracy** – Important parameter is determining the accuracy of the approximation. Small values give precise- approximations, large values give more generic approximation. A good rule of thumb is less than 5% of the contour perimeter\n", 12 | "- **Closed** – a Boolean value that states whether the approximate contour should be open or closed \n" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 5, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "import numpy as np\n", 22 | "import cv2\n", 23 | "\n", 24 | "# Load image and keep a copy\n", 25 | "image = cv2.imread('images/house.jpg')\n", 26 | "orig_image = image.copy()\n", 27 | "cv2.imshow('Original Image', orig_image)\n", 28 | "cv2.waitKey(0) \n", 29 | "\n", 30 | "# Grayscale and binarize\n", 31 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 32 | "ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)\n", 33 | "\n", 34 | "# Find contours \n", 35 | "_ , contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n", 36 | "\n", 37 | "# Iterate through each contour and compute the bounding rectangle\n", 38 | "for c in contours:\n", 39 | " x,y,w,h = cv2.boundingRect(c)\n", 40 | " cv2.rectangle(orig_image,(x,y),(x+w,y+h),(0,0,255),2) \n", 41 | " cv2.imshow('Bounding Rectangle', orig_image)\n", 42 | "\n", 43 | "cv2.waitKey(0) \n", 44 | " \n", 45 | "# Iterate through each contour and compute the approx contour\n", 46 | "for c in contours:\n", 47 | " # Calculate accuracy as a percent of the contour perimeter\n", 48 | " accuracy = 0.03 * cv2.arcLength(c, True)\n", 49 | " approx = cv2.approxPolyDP(c, accuracy, True)\n", 50 | " cv2.drawContours(image, [approx], 0, (0, 255, 0), 2)\n", 51 | " cv2.imshow('Approx Poly DP', image)\n", 52 | " \n", 53 | "cv2.waitKey(0) \n", 54 | "cv2.destroyAllWindows()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## Convex Hull\n", 62 | "\n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 8, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "import numpy as np\n", 72 | "import cv2\n", 73 | "\n", 74 | "image = cv2.imread('images/hand.jpg')\n", 75 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 76 | "\n", 77 | "cv2.imshow('Original Image', image)\n", 78 | "cv2.waitKey(0) \n", 79 | "\n", 80 | "# Threshold the image\n", 81 | "ret, thresh = cv2.threshold(gray, 176, 255, 0)\n", 82 | "\n", 83 | "# Find contours \n", 84 | "_,contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n", 85 | "\n", 86 | "# Sort Contors by area and then remove the largest frame contour\n", 87 | "n = len(contours) - 1\n", 88 | "contours = sorted(contours, key=cv2.contourArea, reverse=False)[:n]\n", 89 | "\n", 90 | "# Iterate through contours and draw the convex hull\n", 91 | "for c in contours:\n", 92 | " hull = cv2.convexHull(c)\n", 93 | " cv2.drawContours(image, [hull], 0, (0, 255, 0), 2)\n", 94 | " cv2.imshow('Convex Hull', image)\n", 95 | "\n", 96 | "cv2.waitKey(0) \n", 97 | "cv2.destroyAllWindows()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": { 104 | "collapsed": true 105 | }, 106 | "outputs": [], 107 | "source": [] 108 | } 109 | ], 110 | "metadata": { 111 | "kernelspec": { 112 | "display_name": "Python 3", 113 | "language": "python", 114 | "name": "python3" 115 | }, 116 | "language_info": { 117 | "codemirror_mode": { 118 | "name": "ipython", 119 | "version": 3 120 | }, 121 | "file_extension": ".py", 122 | "mimetype": "text/x-python", 123 | "name": "python", 124 | "nbconvert_exporter": "python", 125 | "pygments_lexer": "ipython3", 126 | "version": "3.5.3" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 1 131 | } 132 | -------------------------------------------------------------------------------- /Lecture 4.4 - Matching Contours Shape.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Shape Matching" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "**cv2.matchShapes(contour template, contour, method, method parameter)**\n", 15 | "\n", 16 | "**Output** – match value (lower values means a closer match)\n", 17 | "\n", 18 | "- Contour Template – This is our reference contour that we’re trying to find in the new image\n", 19 | "- Contour – The individual contour we are checking against\n", 20 | "- Method – Type of contour matching (1, 2, 3)\n", 21 | "- Method Parameter – leave alone as 0.0 (not fully utilized in python OpenCV)\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "0.13081816783853514\n", 34 | "0.15902005339788694\n", 35 | "0.14987915682525596\n", 36 | "0.07094034474475601\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "import cv2\n", 42 | "import numpy as np\n", 43 | "\n", 44 | "# Load the shape template or reference image\n", 45 | "template = cv2.imread('images/4star.jpg',0)\n", 46 | "cv2.imshow('Template', template)\n", 47 | "cv2.waitKey()\n", 48 | "\n", 49 | "# Load the target image with the shapes we're trying to match\n", 50 | "target = cv2.imread('images/shapestomatch.jpg')\n", 51 | "target_gray = cv2.cvtColor(target,cv2.COLOR_BGR2GRAY)\n", 52 | "\n", 53 | "# Threshold both images first before using cv2.findContours\n", 54 | "ret, thresh1 = cv2.threshold(template, 127, 255, 0)\n", 55 | "ret, thresh2 = cv2.threshold(target_gray, 127, 255, 0)\n", 56 | "\n", 57 | "# Find contours in template\n", 58 | "_, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)\n", 59 | "\n", 60 | "# We need to sort the contours by area so that we can remove the largest\n", 61 | "# contour which is the image outline\n", 62 | "sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)\n", 63 | "\n", 64 | "# We extract the second largest contour which will be our template contour\n", 65 | "template_contour = contours[1]\n", 66 | "\n", 67 | "# Extract contours from second target image\n", 68 | "_, contours, hierarchy = cv2.findContours(thresh2, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)\n", 69 | "\n", 70 | "for c in contours:\n", 71 | " # Iterate through each contour in the target image and \n", 72 | " # use cv2.matchShapes to compare contour shapes\n", 73 | " match = cv2.matchShapes(template_contour, c, 3, 0.0)\n", 74 | " print (match)\n", 75 | " # If the match value is less than 0.15 we\n", 76 | " if match < 0.15:\n", 77 | " closest_contour = c\n", 78 | " else:\n", 79 | " closest_contour = [] \n", 80 | " \n", 81 | "cv2.drawContours(target, [closest_contour], -1, (0,255,0), 3)\n", 82 | "cv2.imshow('Output', target)\n", 83 | "cv2.waitKey()\n", 84 | "cv2.destroyAllWindows()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "collapsed": true 99 | }, 100 | "outputs": [], 101 | "source": [] 102 | } 103 | ], 104 | "metadata": { 105 | "kernelspec": { 106 | "display_name": "Python 3", 107 | "language": "python", 108 | "name": "python3" 109 | }, 110 | "language_info": { 111 | "codemirror_mode": { 112 | "name": "ipython", 113 | "version": 3 114 | }, 115 | "file_extension": ".py", 116 | "mimetype": "text/x-python", 117 | "name": "python", 118 | "nbconvert_exporter": "python", 119 | "pygments_lexer": "ipython3", 120 | "version": "3.5.3" 121 | } 122 | }, 123 | "nbformat": 4, 124 | "nbformat_minor": 1 125 | } 126 | -------------------------------------------------------------------------------- /Lecture 4.5 - Mini Project # 2 - Identifying Contours by Shape.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project 2 - Identifiy Contours by Shape" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import cv2\n", 18 | "\n", 19 | "# Load and then gray scale image\n", 20 | "\n", 21 | "image = cv2.imread('images/someshapes.jpg')\n", 22 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 23 | "\n", 24 | "cv2.imshow('Identifying Shapes',image)\n", 25 | "cv2.waitKey(0)\n", 26 | "\n", 27 | "ret, thresh = cv2.threshold(gray, 127, 255, 1)\n", 28 | "\n", 29 | "# Extract Contours\n", 30 | "_, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n", 31 | "\n", 32 | "for cnt in contours:\n", 33 | " \n", 34 | " # Get approximate polygons\n", 35 | " approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt,True),True)\n", 36 | " \n", 37 | " if len(approx) == 3:\n", 38 | " shape_name = \"Triangle\"\n", 39 | " cv2.drawContours(image,[cnt],0,(0,255,0),-1)\n", 40 | " \n", 41 | " # Find contour center to place text at the center\n", 42 | " M = cv2.moments(cnt)\n", 43 | " cx = int(M['m10'] / M['m00'])\n", 44 | " cy = int(M['m01'] / M['m00'])\n", 45 | " cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)\n", 46 | " \n", 47 | " elif len(approx) == 4:\n", 48 | " x,y,w,h = cv2.boundingRect(cnt)\n", 49 | " M = cv2.moments(cnt)\n", 50 | " cx = int(M['m10'] / M['m00'])\n", 51 | " cy = int(M['m01'] / M['m00'])\n", 52 | " \n", 53 | " # Check to see if 4-side polygon is square or rectangle\n", 54 | " # cv2.boundingRect returns the top left and then width and \n", 55 | " if abs(w-h) <= 3:\n", 56 | " shape_name = \"Square\"\n", 57 | " \n", 58 | " # Find contour center to place text at the center\n", 59 | " cv2.drawContours(image, [cnt], 0, (0, 125 ,255), -1)\n", 60 | " cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)\n", 61 | " else:\n", 62 | " shape_name = \"Rectangle\"\n", 63 | " \n", 64 | " # Find contour center to place text at the center\n", 65 | " cv2.drawContours(image, [cnt], 0, (0, 0, 255), -1)\n", 66 | " M = cv2.moments(cnt)\n", 67 | " cx = int(M['m10'] / M['m00'])\n", 68 | " cy = int(M['m01'] / M['m00'])\n", 69 | " cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)\n", 70 | " \n", 71 | " elif len(approx) == 10:\n", 72 | " shape_name = \"Star\"\n", 73 | " cv2.drawContours(image, [cnt], 0, (255, 255, 0), -1)\n", 74 | " M = cv2.moments(cnt)\n", 75 | " cx = int(M['m10'] / M['m00'])\n", 76 | " cy = int(M['m01'] / M['m00'])\n", 77 | " cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)\n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " elif len(approx) >= 15:\n", 82 | " shape_name = \"Circle\"\n", 83 | " cv2.drawContours(image, [cnt], 0, (0, 255, 255), -1)\n", 84 | " M = cv2.moments(cnt)\n", 85 | " cx = int(M['m10'] / M['m00'])\n", 86 | " cy = int(M['m01'] / M['m00'])\n", 87 | " cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)\n", 88 | "\n", 89 | " cv2.imshow('Identifying Shapes',image)\n", 90 | " cv2.waitKey(0)\n", 91 | " \n", 92 | "cv2.destroyAllWindows()" 93 | ] 94 | } 95 | ], 96 | "metadata": { 97 | "kernelspec": { 98 | "display_name": "Python 3", 99 | "language": "python", 100 | "name": "python3" 101 | }, 102 | "language_info": { 103 | "codemirror_mode": { 104 | "name": "ipython", 105 | "version": 3 106 | }, 107 | "file_extension": ".py", 108 | "mimetype": "text/x-python", 109 | "name": "python", 110 | "nbconvert_exporter": "python", 111 | "pygments_lexer": "ipython3", 112 | "version": "3.5.3" 113 | } 114 | }, 115 | "nbformat": 4, 116 | "nbformat_minor": 1 117 | } 118 | -------------------------------------------------------------------------------- /Lecture 4.6 - Line Detection using Hough Lines.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | " ## Line Detection - Using Hough Lines\n", 8 | " \n", 9 | "**cv2.HoughLines**(binarized/thresholded image, 𝜌 accuracy, 𝜃 accuracy, threshold)\n", 10 | "- Threshold here is the minimum vote for it to be considered a line\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 13, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import cv2\n", 20 | "import numpy as np\n", 21 | "\n", 22 | "image = cv2.imread('images/soduku.jpg')\n", 23 | "\n", 24 | "# Grayscale and Canny Edges extracted\n", 25 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 26 | "edges = cv2.Canny(gray, 100, 170, apertureSize = 3)\n", 27 | "\n", 28 | "# Run HoughLines using a rho accuracy of 1 pixel\n", 29 | "# theta accuracy of np.pi / 180 which is 1 degree\n", 30 | "# Our line threshold is set to 240 (number of points on line)\n", 31 | "lines = cv2.HoughLines(edges, 1, np.pi / 180, 240)\n", 32 | "\n", 33 | "# We iterate through each line and convert it to the format\n", 34 | "# required by cv.lines (i.e. requiring end points)\n", 35 | "for rho, theta in lines[0]:\n", 36 | " a = np.cos(theta)\n", 37 | " b = np.sin(theta)\n", 38 | " x0 = a * rho\n", 39 | " y0 = b * rho\n", 40 | " x1 = int(x0 + 1000 * (-b))\n", 41 | " y1 = int(y0 + 1000 * (a))\n", 42 | " x2 = int(x0 - 1000 * (-b))\n", 43 | " y2 = int(y0 - 1000 * (a))\n", 44 | " cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)\n", 45 | "\n", 46 | "cv2.imshow('Hough Lines', image)\n", 47 | "cv2.waitKey(0)\n", 48 | "cv2.destroyAllWindows()" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "### Probabilistic Hough Lines\n", 56 | "\n", 57 | "**cv2.HoughLinesP(binarized image, 𝜌 accuracy, 𝜃 accuracy, threshold, minimum line length, max line gap)\n", 58 | "\n", 59 | "\n" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 10, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "(95, 1, 4)\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "import cv2\n", 77 | "import numpy as np\n", 78 | "\n", 79 | "# Grayscale and Canny Edges extracted\n", 80 | "image = cv2.imread('images/soduku.jpg')\n", 81 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 82 | "edges = cv2.Canny(gray, 100, 170, apertureSize = 3)\n", 83 | "\n", 84 | "# Again we use the same rho and theta accuracies\n", 85 | "# However, we specific a minimum vote (pts along line) of 100\n", 86 | "# and Min line length of 5 pixels and max gap between lines of 10 pixels\n", 87 | "lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 230, 5, 10)\n", 88 | "print (lines.shape)\n", 89 | "\n", 90 | "for x1, y1, x2, y2 in lines[0]:\n", 91 | " cv2.line(image, (x1, y1), (x2, y2),(0, 255, 0), 3)\n", 92 | "\n", 93 | "cv2.imshow('Probabilistic Hough Lines', image)\n", 94 | "cv2.waitKey(0)\n", 95 | "cv2.destroyAllWindows()" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "http://cmp.felk.cvut.cz/~matas/papers/matas-bmvc98.pdf" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": { 109 | "collapsed": true 110 | }, 111 | "outputs": [], 112 | "source": [] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "\n", 119 | "\n" 120 | ] 121 | } 122 | ], 123 | "metadata": { 124 | "kernelspec": { 125 | "display_name": "Python 3", 126 | "language": "python", 127 | "name": "python3" 128 | }, 129 | "language_info": { 130 | "codemirror_mode": { 131 | "name": "ipython", 132 | "version": 3 133 | }, 134 | "file_extension": ".py", 135 | "mimetype": "text/x-python", 136 | "name": "python", 137 | "nbconvert_exporter": "python", 138 | "pygments_lexer": "ipython3", 139 | "version": "3.5.3" 140 | } 141 | }, 142 | "nbformat": 4, 143 | "nbformat_minor": 1 144 | } 145 | -------------------------------------------------------------------------------- /Lecture 4.7 - Circle Detection using Hough Cirlces.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Circle Detection - Hough Cirlces\n", 8 | "\n", 9 | "**cv2.HoughCircles**(image, method, dp, MinDist, param1, param2, minRadius, MaxRadius)\n", 10 | "\n", 11 | "\n", 12 | "- Method - currently only cv2.HOUGH_GRADIENT available\n", 13 | "- dp - Inverse ratio of accumulator resolution\n", 14 | "- MinDist - the minimum distance between the center of detected circles\n", 15 | "- param1 - Gradient value used in the edge detection\n", 16 | "- param2 - Accumulator threshold for the HOUGH_GRADIENT method (lower allows more circles to be detected (false positives))\n", 17 | "- minRadius - limits the smallest circle to this size (via radius)\n", 18 | "- MaxRadius - similarly sets the limit for the largest circles\n", 19 | "\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "import cv2\n", 31 | "import numpy as np\n", 32 | "#import cv2.cv as cv\n", 33 | "\n", 34 | "image = cv2.imread(\"images/bottlecaps.jpg\")\n", 35 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 36 | "blur = cv2.medianBlur(gray, 5)\n", 37 | "\n", 38 | "circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.5, 10)\n", 39 | "#circles = cv2.HoughCircles(gray, cv.CV_HOUGH_GRADIENT, 1, 10)\n", 40 | "\n", 41 | "circles = np.uint16(np.around(circles))\n", 42 | "\n", 43 | "for i in circles[0,:]:\n", 44 | " # draw the outer circle\n", 45 | " cv2.circle(image,(i[0], i[1]), i[2], (255, 0, 0), 2)\n", 46 | " \n", 47 | " # draw the center of the circle\n", 48 | " cv2.circle(image, (i[0], i[1]), 2, (0, 255, 0), 5)\n", 49 | "\n", 50 | "cv2.imshow('detected circles', image)\n", 51 | "cv2.waitKey(0)\n", 52 | "cv2.destroyAllWindows()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": { 68 | "collapsed": true 69 | }, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": { 77 | "collapsed": true 78 | }, 79 | "outputs": [], 80 | "source": [] 81 | } 82 | ], 83 | "metadata": { 84 | "kernelspec": { 85 | "display_name": "Python 3", 86 | "language": "python", 87 | "name": "python3" 88 | }, 89 | "language_info": { 90 | "codemirror_mode": { 91 | "name": "ipython", 92 | "version": 3 93 | }, 94 | "file_extension": ".py", 95 | "mimetype": "text/x-python", 96 | "name": "python", 97 | "nbconvert_exporter": "python", 98 | "pygments_lexer": "ipython3", 99 | "version": "3.5.3" 100 | } 101 | }, 102 | "nbformat": 4, 103 | "nbformat_minor": 1 104 | } 105 | -------------------------------------------------------------------------------- /Lecture 4.8 - Blob Detection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Blob Detection" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# Standard imports\n", 17 | "import cv2\n", 18 | "import numpy as np;\n", 19 | " \n", 20 | "# Read image\n", 21 | "image = cv2.imread(\"images/Sunflowers.jpg\")\n", 22 | " \n", 23 | "# Set up the detector with default parameters.\n", 24 | "detector = cv2.SimpleBlobDetector_create()\n", 25 | " \n", 26 | "# Detect blobs.\n", 27 | "keypoints = detector.detect(image)\n", 28 | " \n", 29 | "# Draw detected blobs as red circles.\n", 30 | "# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of\n", 31 | "# the circle corresponds to the size of blob\n", 32 | "blank = np.zeros((1,1)) \n", 33 | "blobs = cv2.drawKeypoints(image, keypoints, blank, (0,255,255),\n", 34 | " cv2.DRAW_MATCHES_FLAGS_DEFAULT)\n", 35 | " \n", 36 | "# Show keypoints\n", 37 | "cv2.imshow(\"Blobs\", blobs)\n", 38 | "cv2.waitKey(0)\n", 39 | "cv2.destroyAllWindows()" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "source": [ 48 | "The function **cv2.drawKeypoints** takes the following arguments:\n", 49 | "\n", 50 | "**cv2.drawKeypoints**(input image, keypoints, blank_output_array, color, flags)\n", 51 | "\n", 52 | "flags:\n", 53 | "- cv2.DRAW_MATCHES_FLAGS_DEFAULT\n", 54 | "- cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS\n", 55 | "- cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG\n", 56 | "- cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": { 68 | "collapsed": true 69 | }, 70 | "outputs": [], 71 | "source": [] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.5.3" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 1 95 | } 96 | -------------------------------------------------------------------------------- /Lecture 4.9 - Mini Project # 3 - Counting Circles and Ellipses .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project # 3 - Counting Circles and Ellipses " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 16, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import cv2\n", 17 | "import numpy as np\n", 18 | "\n", 19 | "# Load image\n", 20 | "image = cv2.imread('images/blobs.jpg', 0)\n", 21 | "# cv2.waitKey(0)\n", 22 | "# cv2.imshow(\"Blobs using default parameters\", image)\n", 23 | "\n", 24 | "# Intialize the detector using the default parameters\n", 25 | "# detector = cv2.SimpleBlobDetector()\n", 26 | " \n", 27 | "# Detect blobs\n", 28 | "#keypoints = detector.detect(image)\n", 29 | " \n", 30 | "# Draw blobs on our image as red circles\n", 31 | "# blank = np.zeros((1,1)) \n", 32 | "# blobs = cv2.drawKeypoints(image, keypoints, blank, (0,0,255),\n", 33 | "# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 34 | "\n", 35 | "# number_of_blobs = len(keypoints)\n", 36 | "# text = \"Total Number of Blobs: \" + str(len(keypoints))\n", 37 | "# cv2.putText(blobs, text, (20, 550), cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 0, 255), 2)\n", 38 | "\n", 39 | "# # Display image with blob keypoints\n", 40 | "# cv2.imshow(\"Blobs using default parameters\", blobs)\n", 41 | "# cv2.waitKey(0)\n", 42 | "\n", 43 | "\n", 44 | "# Set our filtering parameters\n", 45 | "# Initialize parameter settiing using cv2.SimpleBlobDetector\n", 46 | "params = cv2.SimpleBlobDetector_Params()\n", 47 | "\n", 48 | "# Set Area filtering parameters\n", 49 | "params.filterByArea = True\n", 50 | "params.minArea = 100\n", 51 | "\n", 52 | "# Set Circularity filtering parameters\n", 53 | "params.filterByCircularity = True \n", 54 | "params.minCircularity = 0.9\n", 55 | "\n", 56 | "# Set Convexity filtering parameters\n", 57 | "params.filterByConvexity = False\n", 58 | "params.minConvexity = 0.2\n", 59 | " \n", 60 | "# Set inertia filtering parameters\n", 61 | "params.filterByInertia = True\n", 62 | "params.minInertiaRatio = 0.01\n", 63 | "\n", 64 | "# Create a detector with the parameters\n", 65 | "detector = cv2.SimpleBlobDetector_create(params)\n", 66 | " \n", 67 | "# Detect blobs\n", 68 | "keypoints = detector.detect(image)\n", 69 | "\n", 70 | "# Draw blobs on our image as red circles\n", 71 | "blank = np.zeros((1,1)) \n", 72 | "blobs = cv2.drawKeypoints(image, keypoints, blank, (0,255,0),\n", 73 | " cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 74 | "\n", 75 | "number_of_blobs = len(keypoints)\n", 76 | "text = \"Number of Circular Blobs: \" + str(len(keypoints))\n", 77 | "cv2.putText(blobs, text, (20, 550), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 100, 255), 2)\n", 78 | "\n", 79 | "# Show blobs\n", 80 | "cv2.imshow(\"Filtering Circular Blobs Only\", blobs)\n", 81 | "cv2.waitKey(0)\n", 82 | "cv2.destroyAllWindows()" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": { 89 | "collapsed": true 90 | }, 91 | "outputs": [], 92 | "source": [] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "collapsed": true 99 | }, 100 | "outputs": [], 101 | "source": [] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "**NOTE** OpenCV 3.XX, use this line of code for intializing our blob detector\n", 108 | "\n", 109 | "`detector = cv2.SimpleBlobDetector_create(params)`\n", 110 | "\n", 111 | "OpenCV 2.4.X users use this:\n", 112 | "\n", 113 | "`detector = cv2.SimpleBlobDetector()`" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": { 120 | "collapsed": true 121 | }, 122 | "outputs": [], 123 | "source": [] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": { 129 | "collapsed": true 130 | }, 131 | "outputs": [], 132 | "source": [] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": { 138 | "collapsed": true 139 | }, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": { 147 | "collapsed": true 148 | }, 149 | "outputs": [], 150 | "source": [] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Python 3", 156 | "language": "python", 157 | "name": "python3" 158 | }, 159 | "language_info": { 160 | "codemirror_mode": { 161 | "name": "ipython", 162 | "version": 3 163 | }, 164 | "file_extension": ".py", 165 | "mimetype": "text/x-python", 166 | "name": "python", 167 | "nbconvert_exporter": "python", 168 | "pygments_lexer": "ipython3", 169 | "version": "3.5.3" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 1 174 | } 175 | -------------------------------------------------------------------------------- /Lecture 5.2 - Mini Project # 4 - Finding Waldo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project # 4 - Finding Waldo" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "ename": "TypeError", 17 | "evalue": "an integer is required (got type list)", 18 | "output_type": "error", 19 | "traceback": [ 20 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 21 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 22 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mtemplate\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'images/waldo.jpg'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatchTemplate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtemplate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'cv2.TM_CCOEFF'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0mmin_val\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_val\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_loc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_loc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mminMaxLoc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 23 | "\u001b[0;31mTypeError\u001b[0m: an integer is required (got type list)" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "import cv2\n", 29 | "import numpy as np\n", 30 | "\n", 31 | "# Load input image and convert to grayscale\n", 32 | "image = cv2.imread('images/WaldoBeach.jpg')\n", 33 | "cv2.imshow('Where is Waldo?', image)\n", 34 | "cv2.waitKey(0)\n", 35 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 36 | "\n", 37 | "# Load Template image\n", 38 | "template = cv2.imread('images/waldo.jpg',0)\n", 39 | "\n", 40 | "result = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF)\n", 41 | "min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)\n", 42 | "\n", 43 | "#Create Bounding Box\n", 44 | "top_left = max_loc\n", 45 | "bottom_right = (top_left[0] + 50, top_left[1] + 50)\n", 46 | "cv2.rectangle(image, top_left, bottom_right, (0,0,255), 5)\n", 47 | "\n", 48 | "cv2.imshow('Where is Waldo?', image)\n", 49 | "cv2.waitKey(0)\n", 50 | "cv2.destroyAllWindows()" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "### Notes on Template Matching\n", 58 | "\n", 59 | "There are a variety of methods to perform template matching, but in this case we are using the correlation coefficient which is specified by the flag **cv2.TM_CCOEFF.**\n", 60 | "\n", 61 | "So what exactly is the cv2.matchTemplate function doing?\n", 62 | "Essentially, this function takes a “sliding window” of our waldo query image and slides it across our puzzle image from left to right and top to bottom, one pixel at a time. Then, for each of these locations, we compute the correlation coefficient to determine how “good” or “bad” the match is. \n", 63 | "\n", 64 | "Regions with sufficiently high correlation can be considered “matches” for our waldo template.\n", 65 | "From there, all we need is a call to cv2.minMaxLoc on Line 22 to find where our “good” matches are.\n", 66 | "That’s really all there is to template matching!\n", 67 | "\n", 68 | "http://docs.opencv.org/2.4/modules/imgproc/doc/object_detection.html " 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": { 75 | "collapsed": true 76 | }, 77 | "outputs": [], 78 | "source": [] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": { 84 | "collapsed": true 85 | }, 86 | "outputs": [], 87 | "source": [] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [] 97 | } 98 | ], 99 | "metadata": { 100 | "kernelspec": { 101 | "display_name": "Python 3", 102 | "language": "python", 103 | "name": "python3" 104 | }, 105 | "language_info": { 106 | "codemirror_mode": { 107 | "name": "ipython", 108 | "version": 3 109 | }, 110 | "file_extension": ".py", 111 | "mimetype": "text/x-python", 112 | "name": "python", 113 | "nbconvert_exporter": "python", 114 | "pygments_lexer": "ipython3", 115 | "version": "3.5.3" 116 | } 117 | }, 118 | "nbformat": 4, 119 | "nbformat_minor": 1 120 | } 121 | -------------------------------------------------------------------------------- /Lecture 5.4 - Finding Corners.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Finding Corners" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import cv2\n", 17 | "import numpy as np\n", 18 | "\n", 19 | "# Load image then grayscale\n", 20 | "image = cv2.imread('images/chess.JPG')\n", 21 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 22 | "\n", 23 | "# The cornerHarris function requires the array datatype to be float32\n", 24 | "gray = np.float32(gray)\n", 25 | "\n", 26 | "harris_corners = cv2.cornerHarris(gray, 3, 3, 0.05)\n", 27 | "\n", 28 | "#We use dilation of the corner points to enlarge them\\\n", 29 | "kernel = np.ones((7,7),np.uint8)\n", 30 | "harris_corners = cv2.dilate(harris_corners, kernel, iterations = 2)\n", 31 | "\n", 32 | "# Threshold for an optimal value, it may vary depending on the image.\n", 33 | "image[harris_corners > 0.025 * harris_corners.max() ] = [255, 127, 127]\n", 34 | "\n", 35 | "cv2.imshow('Harris Corners', image)\n", 36 | "cv2.waitKey(0)\n", 37 | "cv2.destroyAllWindows()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "#### Harris Corner Detection is an algorithm developed in 1998 for corner detection (http://www.bmva.org/bmvc/1988/avc-88-023.pdf) and works fairly well.\n", 45 | "\n", 46 | "**cv2.cornerHarris**(input image, block size, ksize, k)\n", 47 | "- Input image - should be grayscale and float32 type.\n", 48 | "- blockSize - the size of neighborhood considered for corner detection\n", 49 | "- ksize - aperture parameter of Sobel derivative used.\n", 50 | "- k - harris detector free parameter in the equation\n", 51 | "- **Output** – array of corner locations (x,y)\n", 52 | "\n", 53 | "\n" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "## Improved Corner Detection using - Good Features to Track" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "metadata": { 67 | "collapsed": true 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "import cv2\n", 72 | "import numpy as np\n", 73 | "\n", 74 | "img = cv2.imread('images/chess.JPG')\n", 75 | "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", 76 | "\n", 77 | "# We specific the top 50 corners\n", 78 | "corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 150)\n", 79 | "\n", 80 | "for corner in corners:\n", 81 | " x, y = corner[0]\n", 82 | " x = int(x)\n", 83 | " y = int(y)\n", 84 | " cv2.rectangle(img,(x-10,y-10),(x+10,y+10),(0,255,0), 2)\n", 85 | " \n", 86 | "cv2.imshow(\"Corners Found\", img)\n", 87 | "cv2.waitKey()\n", 88 | "cv2.destroyAllWindows()" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "**cv2.goodFeaturesToTrack**(input image, maxCorners, qualityLevel, minDistance)\n", 96 | "\n", 97 | "- Input Image - 8-bit or floating-point 32-bit, single-channel image.\n", 98 | "- maxCorners – Maximum number of corners to return. If there are more corners than are found, the strongest of them is returned.\n", 99 | "- qualityLevel – Parameter characterizing the minimal accepted quality of image corners. The parameter value is multiplied by the best corner quality measure (smallest eigenvalue). The corners with the quality measure less than the product are rejected. For example, if the best corner has the quality measure = 1500, and the qualityLevel=0.01 , then all the corners with the quality - - measure less than 15 are rejected.\n", 100 | "- minDistance – Minimum possible Euclidean distance between the returned corners.\n" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": { 107 | "collapsed": true 108 | }, 109 | "outputs": [], 110 | "source": [] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": { 116 | "collapsed": true 117 | }, 118 | "outputs": [], 119 | "source": [] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": { 134 | "collapsed": true 135 | }, 136 | "outputs": [], 137 | "source": [] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.5.3" 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 1 161 | } 162 | -------------------------------------------------------------------------------- /Lecture 5.5 - SIFT, SURF, FAST, BRIEF & ORB.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Feature Detection" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "#### The SIFT & SURF algorithms are patented by their respective creators, and while they are free to use in academic and research settings, you should technically be obtaining a license/permission from the creators if you are using them in a commercial (i.e. for-profit) application." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## SIFT\n", 22 | "\n", 23 | "http://www.inf.fu-berlin.de/lehre/SS09/CV/uebungen/uebung09/SIFT.pdf" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 26, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Number of keypoints Detected: 1893\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "import cv2\n", 41 | "import numpy as np\n", 42 | "\n", 43 | "image = cv2.imread('images/input.jpg')\n", 44 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 45 | "\n", 46 | "#Create SIFT Feature Detector object\n", 47 | "#sift = cv2.SIFT()\n", 48 | "sift = cv2.xfeatures2d.SIFT_create()\n", 49 | "(keypoints, descs) = sift.detectAndCompute(gray, None)\n", 50 | "\n", 51 | "#Detect key points\n", 52 | "#keypoints = sift.detect(gray, None)\n", 53 | "print(\"Number of keypoints Detected: \", len(keypoints))\n", 54 | "\n", 55 | "# Draw rich key points on input image\n", 56 | "image = cv2.drawKeypoints(image, keypoints, 0,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 57 | "\n", 58 | "cv2.imshow('Feature Method - SIFT', image)\n", 59 | "cv2.waitKey(0)\n", 60 | "cv2.destroyAllWindows()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "## SURF\n", 68 | "\n", 69 | "http://www.vision.ee.ethz.ch/~surf/eccv06.pdf" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 29, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "Number of keypoints Detected: 3000\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "import cv2\n", 87 | "import numpy as np\n", 88 | "\n", 89 | "image = cv2.imread('images/input.jpg')\n", 90 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 91 | "\n", 92 | "#Create SURF Feature Detector object\n", 93 | "#surf = cv2.SURF()\n", 94 | "surf = cv2.xfeatures2d.SURF_create()\n", 95 | "\n", 96 | "# Only features, whose hessian is larger than hessianThreshold are retained by the detector\n", 97 | "#surf.hessianThreshold = 500\n", 98 | "(keypoints, descs) = surf.detectAndCompute(gray, None)\n", 99 | "#keypoints, descriptors = surf.detectAndCompute(gray, None)\n", 100 | "print (\"Number of keypoints Detected: \", len(keypoints))\n", 101 | "\n", 102 | "# Draw rich key points on input image\n", 103 | "image = cv2.drawKeypoints(image, keypoints,0, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 104 | "\n", 105 | "cv2.imshow('Feature Method - SURF', image)\n", 106 | "cv2.waitKey()\n", 107 | "cv2.destroyAllWindows()" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "## FAST\n", 115 | "\n", 116 | "https://www.edwardrosten.com/work/rosten_2006_machine.pdf\n", 117 | "http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/AV1011/AV1FeaturefromAcceleratedSegmentTest.pdf" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 30, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "Number of keypoints Detected: 8960\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "import cv2\n", 135 | "import numpy as np\n", 136 | "\n", 137 | "image = cv2.imread('images/input.jpg')\n", 138 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 139 | "\n", 140 | "# Create FAST Detector object\n", 141 | "fast = cv2.FastFeatureDetector_create()\n", 142 | "\n", 143 | "# Obtain Key points, by default non max suppression is On\n", 144 | "# to turn off set fast.setBool('nonmaxSuppression', False)\n", 145 | "keypoints = fast.detect(gray, None)\n", 146 | "print (\"Number of keypoints Detected: \", len(keypoints))\n", 147 | "\n", 148 | "# Draw rich keypoints on input image\n", 149 | "image = cv2.drawKeypoints(image, keypoints,0, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 150 | "\n", 151 | "cv2.imshow('Feature Method - FAST', image)\n", 152 | "cv2.waitKey()\n", 153 | "cv2.destroyAllWindows()" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "## BRIEF\n", 161 | "\n", 162 | "http://cvlabwww.epfl.ch/~lepetit/papers/calonder_pami11.pdf" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 31, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "Number of keypoints Detected: 8735\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "import cv2\n", 180 | "import numpy as np\n", 181 | "\n", 182 | "image = cv2.imread('images/input.jpg')\n", 183 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 184 | "\n", 185 | "# Create FAST detector object\n", 186 | "fast = cv2.FastFeatureDetector_create()\n", 187 | "\n", 188 | "# Create BRIEF extractor object\n", 189 | "brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()\n", 190 | "\n", 191 | "# Determine key points\n", 192 | "keypoints = fast.detect(gray, None)\n", 193 | "\n", 194 | "# Obtain descriptors and new final keypoints using BRIEF\n", 195 | "keypoints, descriptors = brief.compute(gray, keypoints)\n", 196 | "print (\"Number of keypoints Detected: \", len(keypoints))\n", 197 | "\n", 198 | "# Draw rich keypoints on input image\n", 199 | "image = cv2.drawKeypoints(image, keypoints,0, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 200 | " \n", 201 | "cv2.imshow('Feature Method - BRIEF', image)\n", 202 | "cv2.waitKey()\n", 203 | "cv2.destroyAllWindows()" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "## Oriented FAST and Rotated BRIEF (ORB)\n", 211 | "http://www.willowgarage.com/sites/default/files/orb_final.pdf" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 10, 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "name": "stdout", 221 | "output_type": "stream", 222 | "text": [ 223 | "Number of keypoints Detected: 500\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "import cv2\n", 229 | "import numpy as np\n", 230 | "\n", 231 | "image = cv2.imread('images/input.jpg')\n", 232 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 233 | "\n", 234 | "# Create ORB object, we can specify the number of key points we desire\n", 235 | "orb = cv2.ORB_create()\n", 236 | "\n", 237 | "# Determine key points\n", 238 | "keypoints = orb.detect(gray, None)\n", 239 | "\n", 240 | "# Obtain the descriptors\n", 241 | "keypoints, descriptors = orb.compute(gray, keypoints)\n", 242 | "print(\"Number of keypoints Detected: \", len(keypoints))\n", 243 | "\n", 244 | "# Draw rich keypoints on input image\n", 245 | "image = cv2.drawKeypoints(image, keypoints,0,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)\n", 246 | "\n", 247 | "cv2.imshow('Feature Method - ORB', image)\n", 248 | "cv2.waitKey()\n", 249 | "cv2.destroyAllWindows()" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": null, 255 | "metadata": { 256 | "collapsed": true 257 | }, 258 | "outputs": [], 259 | "source": [] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": { 265 | "collapsed": true 266 | }, 267 | "outputs": [], 268 | "source": [] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "metadata": { 274 | "collapsed": true 275 | }, 276 | "outputs": [], 277 | "source": [] 278 | } 279 | ], 280 | "metadata": { 281 | "kernelspec": { 282 | "display_name": "Python 3", 283 | "language": "python", 284 | "name": "python3" 285 | }, 286 | "language_info": { 287 | "codemirror_mode": { 288 | "name": "ipython", 289 | "version": 3 290 | }, 291 | "file_extension": ".py", 292 | "mimetype": "text/x-python", 293 | "name": "python", 294 | "nbconvert_exporter": "python", 295 | "pygments_lexer": "ipython3", 296 | "version": "3.5.3" 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 1 301 | } 302 | -------------------------------------------------------------------------------- /Lecture 6.2 - Face & Eye Detection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Face & Eye Detection using HAAR Cascade Classifiers" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "import cv2\n", 20 | "\n", 21 | "# We point OpenCV's CascadeClassifier function to where our \n", 22 | "# classifier (XML file format) is stored\n", 23 | "face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')\n", 24 | "\n", 25 | "# Load our image then convert it to grayscale\n", 26 | "image = cv2.imread('images/Trump.jpg')\n", 27 | "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", 28 | "\n", 29 | "# Our classifier returns the ROI of the detected face as a tuple\n", 30 | "# It stores the top left coordinate and the bottom right coordiantes\n", 31 | "faces = face_classifier.detectMultiScale(gray, 1.3, 5)\n", 32 | "\n", 33 | "# When no faces detected, face_classifier returns and empty tuple\n", 34 | "if faces is ():\n", 35 | " print(\"No faces found\")\n", 36 | "\n", 37 | "# We iterate through our faces array and draw a rectangle\n", 38 | "# over each face in faces\n", 39 | "for (x,y,w,h) in faces:\n", 40 | " cv2.rectangle(image, (x,y), (x+w,y+h), (127,0,255), 2)\n", 41 | " cv2.imshow('Face Detection', image)\n", 42 | " cv2.waitKey(0)\n", 43 | " \n", 44 | "cv2.destroyAllWindows()" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### Let's combine face and eye detection" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "metadata": { 58 | "collapsed": true 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "import numpy as np\n", 63 | "import cv2\n", 64 | " \n", 65 | "face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')\n", 66 | "eye_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_eye.xml')\n", 67 | " \n", 68 | "img = cv2.imread('images/Trump.jpg')\n", 69 | "gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n", 70 | "\n", 71 | "faces = face_classifier.detectMultiScale(gray, 1.3, 5)\n", 72 | "\n", 73 | "# When no faces detected, face_classifier returns and empty tuple\n", 74 | "if faces is ():\n", 75 | " print(\"No Face Found\")\n", 76 | "\n", 77 | "for (x,y,w,h) in faces:\n", 78 | " cv2.rectangle(img,(x,y),(x+w,y+h),(127,0,255),2)\n", 79 | " cv2.imshow('img',img)\n", 80 | " cv2.waitKey(0)\n", 81 | " roi_gray = gray[y:y+h, x:x+w]\n", 82 | " roi_color = img[y:y+h, x:x+w]\n", 83 | " eyes = eye_classifier.detectMultiScale(roi_gray)\n", 84 | " for (ex,ey,ew,eh) in eyes:\n", 85 | " cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,255,0),2)\n", 86 | " cv2.imshow('img',img)\n", 87 | " cv2.waitKey(0)\n", 88 | " \n", 89 | "cv2.destroyAllWindows()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "### Let's make a live face & eye detection, keeping the face inview at all times" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 7, 102 | "metadata": { 103 | "collapsed": true 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "import cv2\n", 108 | "import numpy as np\n", 109 | "\n", 110 | "face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')\n", 111 | "eye_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_eye.xml')\n", 112 | "\n", 113 | "def face_detector(img, size=0.5):\n", 114 | " # Convert image to grayscale\n", 115 | " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", 116 | " faces = face_classifier.detectMultiScale(gray, 1.2, 3)\n", 117 | " if faces is ():\n", 118 | " return img\n", 119 | " \n", 120 | " for (x,y,w,h) in faces:\n", 121 | " x = x - 50\n", 122 | " w = w + 50\n", 123 | " y = y - 50\n", 124 | " h = h + 50\n", 125 | " cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n", 126 | " roi_gray = gray[y:y+h, x:x+w]\n", 127 | " roi_color = img[y:y+h, x:x+w]\n", 128 | " eyes = eye_classifier.detectMultiScale(roi_gray)\n", 129 | " \n", 130 | " for (ex,ey,ew,eh) in eyes:\n", 131 | " cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),2) \n", 132 | " \n", 133 | " roi_color = cv2.flip(roi_color,1)\n", 134 | " return roi_color\n", 135 | "\n", 136 | "cap = cv2.VideoCapture(0)\n", 137 | "\n", 138 | "while True:\n", 139 | " ret, frame = cap.read()\n", 140 | " cv2.imshow('Our Face Extractor', face_detector(frame))\n", 141 | " if cv2.waitKey(33) == ord('a'):\n", 142 | " break\n", 143 | " \n", 144 | "cap.release()\n", 145 | "cv2.destroyAllWindows() " 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": { 151 | "collapsed": true 152 | }, 153 | "source": [ 154 | "### Tuning Cascade Classifiers\n", 155 | "\n", 156 | "*ourClassifier*.**detectMultiScale**(input image, **Scale Factor** , **Min Neighbors**)\n", 157 | "\n", 158 | "- **Scale Factor**\n", 159 | "Specifies how much we reduce the image size each time we scale. E.g. in face detection we typically use 1.3. This means we reduce the image by 30% each time it’s scaled. Smaller values, like 1.05 will take longer to compute, but will increase the rate of detection.\n", 160 | "\n", 161 | "\n", 162 | "\n", 163 | "- **Min Neighbors**\n", 164 | "Specifies the number of neighbors each potential window should have in order to consider it a positive detection. Typically set between 3-6. \n", 165 | "It acts as sensitivity setting, low values will sometimes detect multiples faces over a single face. High values will ensure less false positives, but you may miss some faces. \n" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 1, 171 | "metadata": { 172 | "collapsed": true 173 | }, 174 | "outputs": [], 175 | "source": [] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": { 181 | "collapsed": true 182 | }, 183 | "outputs": [], 184 | "source": [] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 3", 199 | "language": "python", 200 | "name": "python3" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 3 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython3", 212 | "version": "3.5.3" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 1 217 | } 218 | -------------------------------------------------------------------------------- /Lecture 6.3 - Mini Project # 6 - Car & Pedestrian Detection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project # 6 - Car & Pedestrian Detection\n", 8 | "\n", 9 | "**NOTE** \n", 10 | "- If no video loads after running code, you may need to copy your *opencv_ffmpeg.dll* \n", 11 | "- From: *C:\\opencv2413\\opencv\\sources\\3rdparty\\ffmpeg*\n", 12 | "- To: Where your python is installed e.g. *C:\\Anaconda2\\* \\\n", 13 | "- Once it's copied you'll need to rename the file according to the version of OpenCV you're using.\n", 14 | "- e.g. if you're using OpenCV 2.4.13 then rename the file as:\n", 15 | "- **opencv_ffmpeg2413_64.dll** or opencv_ffmpeg2413.dll (if you're using an X86 machine)\n", 16 | "- **opencv_ffmpeg310_64.dll** or opencv_ffmpeg310.dll (if you're using an X86 machine)\n", 17 | "\n", 18 | "To find out where you python.exe is installed, just run these two lines of code:" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "/home/vinod/anaconda3/envs/opencv/bin/python\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "import sys\n", 36 | "print(sys.executable)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "### Pedistrian Detection" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 4, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "import cv2\n", 53 | "import numpy as np\n", 54 | "\n", 55 | "# Create our body classifier\n", 56 | "body_classifier = cv2.CascadeClassifier('Haarcascades\\haarcascade_fullbody.xml')\n", 57 | "\n", 58 | "# Initiate video capture for video file\n", 59 | "cap = cv2.VideoCapture('images/walking.avi')\n", 60 | "\n", 61 | "# Loop once video is successfully loaded\n", 62 | "while cap.isOpened():\n", 63 | " \n", 64 | " # Read first frame\n", 65 | " ret, frame = cap.read()\n", 66 | " frame = cv2.resize(frame, None,fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)\n", 67 | "\n", 68 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 69 | " # Pass frame to our body classifier\n", 70 | " bodies = body_classifier.detectMultiScale(gray, 1.2, 3)\n", 71 | " \n", 72 | " # Extract bounding boxes for any bodies identified\n", 73 | " for (x,y,w,h) in bodies:\n", 74 | " cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)\n", 75 | " cv2.imshow('Pedestrians', frame)\n", 76 | "\n", 77 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 78 | " break\n", 79 | "\n", 80 | "cap.release()\n", 81 | "cv2.destroyAllWindows()" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "### Car Detection\n" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "import cv2\n", 98 | "import time\n", 99 | "import numpy as np\n", 100 | "\n", 101 | "# Create our body classifier\n", 102 | "car_classifier = cv2.CascadeClassifier('Haarcascades\\haarcascade_car.xml')\n", 103 | "\n", 104 | "# Initiate video capture for video file\n", 105 | "cap = cv2.VideoCapture('images/cars.avi')\n", 106 | "\n", 107 | "\n", 108 | "# Loop once video is successfully loaded\n", 109 | "while cap.isOpened():\n", 110 | " \n", 111 | " time.sleep(.05)\n", 112 | " # Read first frame\n", 113 | " ret, frame = cap.read()\n", 114 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 115 | " \n", 116 | " # Pass frame to our car classifier\n", 117 | " cars = car_classifier.detectMultiScale(gray, 1.4, 2)\n", 118 | " \n", 119 | " # Extract bounding boxes for any bodies identified\n", 120 | " for (x,y,w,h) in cars:\n", 121 | " cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)\n", 122 | " cv2.imshow('Cars', frame)\n", 123 | "\n", 124 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 125 | " break\n", 126 | "\n", 127 | "cap.release()\n", 128 | "cv2.destroyAllWindows()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "- **Full Body / Pedestrian Classifier ** - https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_fullbody.xml\n", 136 | "- **Car Classifier ** - http://www.codeforge.com/read/241845/cars3.xml__html\n" 137 | ] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.5.3" 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 1 161 | } 162 | -------------------------------------------------------------------------------- /Lecture 7.1 - Facial Landmarks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Facial Landmarks" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "-" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "See blog post here - https://matthewearl.github.io/2015/07/28/switching-eds-with-python/\n", 24 | "\n", 25 | "\n", 26 | "#### Install Instructions for dlib\n", 27 | "\n", 28 | "- Download and Install Dlib\n", 29 | "\n", 30 | "https://sourceforge.net/projects/dclib/\n", 31 | "\n", 32 | "- Extract files in C:/dlib \n", 33 | "- Use command prompt to Cd to folder and run “python setup.py install”\n", 34 | "\n", 35 | "#### Download the pre-trained model here \n", 36 | "\n", 37 | "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2\n", 38 | "\n", 39 | "- Place this file in your default ipython notebook folder\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "import cv2\n", 49 | "import dlib\n", 50 | "import numpy \n", 51 | "\n", 52 | "PREDICTOR_PATH = \"shape_predictor_68_face_landmarks.dat\"\n", 53 | "predictor = dlib.shape_predictor(PREDICTOR_PATH)\n", 54 | "detector = dlib.get_frontal_face_detector()\n", 55 | "\n", 56 | "\n", 57 | "class TooManyFaces(Exception):\n", 58 | " pass\n", 59 | "\n", 60 | "class NoFaces(Exception):\n", 61 | " pass\n", 62 | "\n", 63 | "def get_landmarks(im):\n", 64 | " rects = detector(im, 1)\n", 65 | "\n", 66 | " if len(rects) > 1:\n", 67 | " raise TooManyFaces\n", 68 | " if len(rects) == 0:\n", 69 | " raise NoFaces\n", 70 | "\n", 71 | " return numpy.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])\n", 72 | "\n", 73 | "def annotate_landmarks(im, landmarks):\n", 74 | " im = im.copy()\n", 75 | " for idx, point in enumerate(landmarks):\n", 76 | " pos = (point[0, 0], point[0, 1])\n", 77 | " cv2.putText(im, str(idx), pos,\n", 78 | " fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,\n", 79 | " fontScale=0.4,\n", 80 | " \n", 81 | " color=(0, 0, 255))\n", 82 | " cv2.circle(im, pos, 3, color=(0, 255, 255))\n", 83 | " return im\n", 84 | "\n", 85 | "image = cv2.imread('images/vinod_kumar.jpg')\n", 86 | "landmarks = get_landmarks(image)\n", 87 | "image_with_landmarks = annotate_landmarks(image, landmarks)\n", 88 | "\n", 89 | "cv2.imshow('Result', image_with_landmarks)\n", 90 | "cv2.imwrite('image_with_landmarks.jpg',image_with_landmarks)\n", 91 | "cv2.waitKey(0)\n", 92 | "cv2.destroyAllWindows()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 1, 98 | "metadata": { 99 | "collapsed": true 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "import dlib" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": { 110 | "collapsed": true 111 | }, 112 | "outputs": [], 113 | "source": [] 114 | } 115 | ], 116 | "metadata": { 117 | "kernelspec": { 118 | "display_name": "Python 3", 119 | "language": "python", 120 | "name": "python3" 121 | }, 122 | "language_info": { 123 | "codemirror_mode": { 124 | "name": "ipython", 125 | "version": 3 126 | }, 127 | "file_extension": ".py", 128 | "mimetype": "text/x-python", 129 | "name": "python", 130 | "nbconvert_exporter": "python", 131 | "pygments_lexer": "ipython3", 132 | "version": "3.5.3" 133 | } 134 | }, 135 | "nbformat": 4, 136 | "nbformat_minor": 1 137 | } 138 | -------------------------------------------------------------------------------- /Lecture 7.4 - Mini Project # 8 – Yawn Detector and Counting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project # 8 – Yawn Detector and Counting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "#!/usr/bin/python\n", 17 | "\n", 18 | "# Copyright (c) 2015 Matthew Earl\n", 19 | "# \n", 20 | "# Permission is hereby granted, free of charge, to any person obtaining a copy\n", 21 | "# of this software and associated documentation files (the \"Software\"), to deal\n", 22 | "# in the Software without restriction, including without limitation the rights\n", 23 | "# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n", 24 | "# copies of the Software, and to permit persons to whom the Software is\n", 25 | "# furnished to do so, subject to the following conditions:\n", 26 | "# \n", 27 | "# The above copyright notice and this permission notice shall be included\n", 28 | "# in all copies or substantial portions of the Software.\n", 29 | "# \n", 30 | "# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n", 31 | "# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n", 32 | "# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\n", 33 | "# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n", 34 | "# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n", 35 | "# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\n", 36 | "# USE OR OTHER DEALINGS IN THE SOFTWARE.\n", 37 | "\n", 38 | "\"\"\"\n", 39 | "This is the code behind the Switching Eds blog post:\n", 40 | " http://matthewearl.github.io/2015/07/28/switching-eds-with-python/\n", 41 | "See the above for an explanation of the code below.\n", 42 | "To run the script you'll need to install dlib (http://dlib.net) including its\n", 43 | "Python bindings, and OpenCV. You'll also need to obtain the trained model from\n", 44 | "sourceforge:\n", 45 | " http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2\n", 46 | "Unzip with `bunzip2` and change `PREDICTOR_PATH` to refer to this file. The\n", 47 | "script is run like so:\n", 48 | " ./faceswap.py \n", 49 | "If successful, a file `output.jpg` will be produced with the facial features\n", 50 | "from `` replaced with the facial features from ``.\n", 51 | "\"\"\"" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 7, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stderr", 61 | "output_type": "stream", 62 | "text": [ 63 | "/home/vinod/anaconda3/envs/opencv/lib/python3.5/site-packages/ipykernel_launcher.py:57: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "import cv2\n", 69 | "import dlib\n", 70 | "import numpy as np\n", 71 | "\n", 72 | "\n", 73 | "PREDICTOR_PATH = \"shape_predictor_68_face_landmarks.dat\"\n", 74 | "predictor = dlib.shape_predictor(PREDICTOR_PATH)\n", 75 | "#cascade_path='haarcascade_frontalface_default.xml'\n", 76 | "#cascade = cv2.CascadeClassifier(cascade_path)\n", 77 | "detector = dlib.get_frontal_face_detector()\n", 78 | "\n", 79 | "\n", 80 | "def get_landmarks(im):\n", 81 | " rects = detector(im, 1)\n", 82 | "\n", 83 | " if len(rects) > 1:\n", 84 | " return \"error\"\n", 85 | " if len(rects) == 0:\n", 86 | " return \"error\"\n", 87 | " return np.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])\n", 88 | "\n", 89 | "\n", 90 | "def annotate_landmarks(im, landmarks):\n", 91 | " im = im.copy()\n", 92 | " for idx, point in enumerate(landmarks):\n", 93 | " pos = (point[0, 0], point[0, 1])\n", 94 | " cv2.putText(im, str(idx), pos,\n", 95 | " fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,\n", 96 | " fontScale=0.4,\n", 97 | " color=(0, 0, 255))\n", 98 | " cv2.circle(im, pos, 3, color=(0, 255, 255))\n", 99 | " return im\n", 100 | "\n", 101 | "def top_lip(landmarks):\n", 102 | " top_lip_pts = []\n", 103 | " for i in range(50,53):\n", 104 | " top_lip_pts.append(landmarks[i])\n", 105 | " for i in range(61,64):\n", 106 | " top_lip_pts.append(landmarks[i])\n", 107 | " top_lip_all_pts = np.squeeze(np.asarray(top_lip_pts))\n", 108 | " top_lip_mean = np.mean(top_lip_pts, axis=0)\n", 109 | " return int(top_lip_mean[:,1])\n", 110 | "\n", 111 | "def bottom_lip(landmarks):\n", 112 | " bottom_lip_pts = []\n", 113 | " for i in range(65,68):\n", 114 | " bottom_lip_pts.append(landmarks[i])\n", 115 | " for i in range(56,59):\n", 116 | " bottom_lip_pts.append(landmarks[i])\n", 117 | " bottom_lip_all_pts = np.squeeze(np.asarray(bottom_lip_pts))\n", 118 | " bottom_lip_mean = np.mean(bottom_lip_pts, axis=0)\n", 119 | " return int(bottom_lip_mean[:,1])\n", 120 | "\n", 121 | "def mouth_open(image):\n", 122 | " landmarks = get_landmarks(image)\n", 123 | " \n", 124 | " if landmarks == \"error\":\n", 125 | " return image, 0\n", 126 | " \n", 127 | " image_with_landmarks = annotate_landmarks(image, landmarks)\n", 128 | " top_lip_center = top_lip(landmarks)\n", 129 | " bottom_lip_center = bottom_lip(landmarks)\n", 130 | " lip_distance = abs(top_lip_center - bottom_lip_center)\n", 131 | " return image_with_landmarks, lip_distance\n", 132 | "\n", 133 | " #cv2.imshow('Result', image_with_landmarks)\n", 134 | " #cv2.imwrite('image_with_landmarks.jpg',image_with_landmarks)\n", 135 | " #cv2.waitKey(0)\n", 136 | " #cv2.destroyAllWindows()\n", 137 | "\n", 138 | "cap = cv2.VideoCapture(0)\n", 139 | "yawns = 0\n", 140 | "yawn_status = False \n", 141 | "\n", 142 | "while True:\n", 143 | " ret, frame = cap.read() \n", 144 | " image_landmarks, lip_distance = mouth_open(frame)\n", 145 | " \n", 146 | " prev_yawn_status = yawn_status \n", 147 | " \n", 148 | " if lip_distance > 15:\n", 149 | " yawn_status = True \n", 150 | " \n", 151 | " cv2.putText(frame, \"Subject is Yawning\", (50,450), \n", 152 | " cv2.FONT_HERSHEY_COMPLEX, 1,(0,0,255),2)\n", 153 | " \n", 154 | "\n", 155 | " output_text = \" Yawn Count: \" + str(yawns + 1)\n", 156 | "\n", 157 | " cv2.putText(frame, output_text, (50,50),\n", 158 | " cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,127),2)\n", 159 | " \n", 160 | " else:\n", 161 | " yawn_status = False \n", 162 | " \n", 163 | " if prev_yawn_status == True and yawn_status == False:\n", 164 | " yawns += 1\n", 165 | "\n", 166 | " cv2.imshow('Live Landmarks', image_landmarks )\n", 167 | " cv2.imshow('Yawn Detection', frame )\n", 168 | " \n", 169 | " if cv2.waitKey(33) == ord('a'):\n", 170 | " break\n", 171 | " \n", 172 | "cap.release()\n", 173 | "cv2.destroyAllWindows() " 174 | ] 175 | } 176 | ], 177 | "metadata": { 178 | "kernelspec": { 179 | "display_name": "Python 3", 180 | "language": "python", 181 | "name": "python3" 182 | }, 183 | "language_info": { 184 | "codemirror_mode": { 185 | "name": "ipython", 186 | "version": 3 187 | }, 188 | "file_extension": ".py", 189 | "mimetype": "text/x-python", 190 | "name": "python", 191 | "nbconvert_exporter": "python", 192 | "pygments_lexer": "ipython3", 193 | "version": "3.5.3" 194 | } 195 | }, 196 | "nbformat": 4, 197 | "nbformat_minor": 1 198 | } 199 | -------------------------------------------------------------------------------- /Lecture 8.3 - Mini Project # 10 Face Recognition – Unlock Your Computer With Your Face!.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Face Recognition – Unlock Your Computer With Your Face!" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "\n", 15 | "### Step 1 - Create Training Data" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stdout", 25 | "output_type": "stream", 26 | "text": [ 27 | "Face not found\n", 28 | "Face not found\n", 29 | "Face not found\n", 30 | "Face not found\n", 31 | "Face not found\n", 32 | "Face not found\n", 33 | "Face not found\n", 34 | "Face not found\n", 35 | "Face not found\n", 36 | "Face not found\n", 37 | "Face not found\n", 38 | "Face not found\n", 39 | "Face not found\n", 40 | "Face not found\n", 41 | "Face not found\n", 42 | "Face not found\n", 43 | "Face not found\n", 44 | "Face not found\n", 45 | "Face not found\n", 46 | "Face not found\n", 47 | "Face not found\n", 48 | "Face not found\n", 49 | "Face not found\n", 50 | "Face not found\n", 51 | "Face not found\n", 52 | "Face not found\n", 53 | "Face not found\n", 54 | "Face not found\n", 55 | "Face not found\n", 56 | "Face not found\n", 57 | "Face not found\n", 58 | "Face not found\n", 59 | "Face not found\n", 60 | "Face not found\n", 61 | "Face not found\n", 62 | "Face not found\n", 63 | "Face not found\n", 64 | "Face not found\n", 65 | "Face not found\n", 66 | "Face not found\n", 67 | "Face not found\n", 68 | "Face not found\n", 69 | "Face not found\n", 70 | "Face not found\n", 71 | "Face not found\n", 72 | "Face not found\n", 73 | "Collecting Samples Complete\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "import cv2\n", 79 | "import numpy as np\n", 80 | "\n", 81 | "# Load HAAR face classifier\n", 82 | "face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')\n", 83 | "\n", 84 | "# Load functions\n", 85 | "def face_extractor(img):\n", 86 | " # Function detects faces and returns the cropped face\n", 87 | " # If no face detected, it returns the input image\n", 88 | " \n", 89 | " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", 90 | " faces = face_classifier.detectMultiScale(gray, 1.3, 5)\n", 91 | " \n", 92 | " if faces is ():\n", 93 | " return None\n", 94 | " \n", 95 | " # Crop all faces found\n", 96 | " for (x,y,w,h) in faces:\n", 97 | " cropped_face = img[y:y+h, x:x+w]\n", 98 | "\n", 99 | " return cropped_face\n", 100 | "\n", 101 | "# Initialize Webcam\n", 102 | "cap = cv2.VideoCapture(0)\n", 103 | "count = 0\n", 104 | "\n", 105 | "# Collect 100 samples of your face from webcam input\n", 106 | "while True:\n", 107 | "\n", 108 | " ret, frame = cap.read()\n", 109 | " if face_extractor(frame) is not None:\n", 110 | " count += 1\n", 111 | " face = cv2.resize(face_extractor(frame), (200, 200))\n", 112 | " face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)\n", 113 | "\n", 114 | " # Save file in specified directory with unique name\n", 115 | " file_name_path = './faces/user/' + str(count) + '.jpg'\n", 116 | " cv2.imwrite(file_name_path, face)\n", 117 | "\n", 118 | " # Put count on images and display live count\n", 119 | " cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)\n", 120 | " cv2.imshow('Face Cropper', face)\n", 121 | " \n", 122 | " else:\n", 123 | " print(\"Face not found\")\n", 124 | " pass\n", 125 | "\n", 126 | " if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key\n", 127 | " break\n", 128 | " \n", 129 | "cap.release()\n", 130 | "cv2.destroyAllWindows() \n", 131 | "print(\"Collecting Samples Complete\")" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "### Step 2 - Train Model" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 3, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "Model trained sucessefully\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "import cv2\n", 156 | "import numpy as np\n", 157 | "from os import listdir\n", 158 | "from os.path import isfile, join\n", 159 | "\n", 160 | "# Get the training data we previously made\n", 161 | "data_path = './faces/user/'\n", 162 | "onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]\n", 163 | "\n", 164 | "# Create arrays for training data and labels\n", 165 | "Training_Data, Labels = [], []\n", 166 | "\n", 167 | "# Open training images in our datapath\n", 168 | "# Create a numpy array for training data\n", 169 | "for i, files in enumerate(onlyfiles):\n", 170 | " image_path = data_path + onlyfiles[i]\n", 171 | " images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n", 172 | " Training_Data.append(np.asarray(images, dtype=np.uint8))\n", 173 | " Labels.append(i)\n", 174 | "\n", 175 | "# Create a numpy array for both training data and labels\n", 176 | "Labels = np.asarray(Labels, dtype=np.int32)\n", 177 | "\n", 178 | "# Initialize facial recognizer\n", 179 | "model = cv2.face.createLBPHFaceRecognizer()\n", 180 | "# NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer()\n", 181 | "\n", 182 | "# Let's train our model \n", 183 | "model.train(np.asarray(Training_Data), np.asarray(Labels))\n", 184 | "print(\"Model trained sucessefully\")\n" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "### Step 3 - Run Our Facial Recognition" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 2, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "import cv2\n", 201 | "import numpy as np\n", 202 | "\n", 203 | "\n", 204 | "face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')\n", 205 | "\n", 206 | "def face_detector(img, size=0.5):\n", 207 | " \n", 208 | " # Convert image to grayscale\n", 209 | " gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n", 210 | " faces = face_classifier.detectMultiScale(gray, 1.3, 5)\n", 211 | " if faces is ():\n", 212 | " return img, []\n", 213 | " \n", 214 | " for (x,y,w,h) in faces:\n", 215 | " cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)\n", 216 | " roi = img[y:y+h, x:x+w]\n", 217 | " roi = cv2.resize(roi, (200, 200))\n", 218 | " return img, roi\n", 219 | "\n", 220 | "\n", 221 | "# Open Webcam\n", 222 | "cap = cv2.VideoCapture(0)\n", 223 | "\n", 224 | "while True:\n", 225 | "\n", 226 | " ret, frame = cap.read()\n", 227 | " \n", 228 | " image, face = face_detector(frame)\n", 229 | " \n", 230 | " try:\n", 231 | " face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)\n", 232 | "\n", 233 | " # Pass face to prediction model\n", 234 | " # \"results\" comprises of a tuple containing the label and the confidence value\n", 235 | " results = model.predict(face)\n", 236 | " \n", 237 | " if results[1] < 500:\n", 238 | " confidence = int( 100 * (1 - (results[1])/400) )\n", 239 | " display_string = str(confidence) + '% Confident it is User'\n", 240 | " \n", 241 | " cv2.putText(image, display_string, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2)\n", 242 | " \n", 243 | " if confidence > 75:\n", 244 | " cv2.putText(image, \"Unlocked\", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)\n", 245 | " cv2.imshow('Face Recognition', image )\n", 246 | " else:\n", 247 | " cv2.putText(image, \"Locked\", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)\n", 248 | " cv2.imshow('Face Recognition', image )\n", 249 | "\n", 250 | " except:\n", 251 | " cv2.putText(image, \"No Face Found\", (220, 120) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)\n", 252 | " cv2.putText(image, \"Locked\", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)\n", 253 | " cv2.imshow('Face Recognition', image )\n", 254 | " pass\n", 255 | " \n", 256 | " if cv2.waitKey(33) == ord('a'): \n", 257 | " break\n", 258 | " \n", 259 | "cap.release()\n", 260 | "cv2.destroyAllWindows() " 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | " " 270 | ] 271 | } 272 | ], 273 | "metadata": { 274 | "kernelspec": { 275 | "display_name": "Python 3", 276 | "language": "python", 277 | "name": "python3" 278 | }, 279 | "language_info": { 280 | "codemirror_mode": { 281 | "name": "ipython", 282 | "version": 3 283 | }, 284 | "file_extension": ".py", 285 | "mimetype": "text/x-python", 286 | "name": "python", 287 | "nbconvert_exporter": "python", 288 | "pygments_lexer": "ipython3", 289 | "version": "3.5.3" 290 | } 291 | }, 292 | "nbformat": 4, 293 | "nbformat_minor": 1 294 | } 295 | -------------------------------------------------------------------------------- /Lecture 9.1 - Filtering by Color.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Filtering by Color" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 13, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import cv2\n", 17 | "import numpy as np\n", 18 | "\n", 19 | "# Initialize webcam\n", 20 | "cap = cv2.VideoCapture(0)\n", 21 | "\n", 22 | "# define range of PURPLE color in HSV\n", 23 | "lower_purple = np.array([100,80,90])\n", 24 | "upper_purple = np.array([110,90,105])\n", 25 | "\n", 26 | "# loop until break statement is exectured\n", 27 | "while True:\n", 28 | " \n", 29 | " # Read webcam image\n", 30 | " ret, frame = cap.read()\n", 31 | " \n", 32 | " # Convert image from RBG/BGR to HSV so we easily filter\n", 33 | " hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 34 | "\n", 35 | "\n", 36 | " # Use inRange to capture only the values between lower & upper_blue\n", 37 | " mask = cv2.inRange(hsv_img, lower_purple, upper_purple)\n", 38 | "\n", 39 | " # Perform Bitwise AND on mask and our original frame\n", 40 | " res = cv2.bitwise_and(frame, frame, mask=mask)\n", 41 | "\n", 42 | " cv2.imshow('Original', frame) \n", 43 | " cv2.imshow('mask', mask)\n", 44 | " cv2.imshow('Filtered Color Only', res)\n", 45 | " if cv2.waitKey(33) == ord('x'):\n", 46 | " break\n", 47 | " \n", 48 | "cap.release()\n", 49 | "cv2.destroyAllWindows()" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "cap.release()" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 20, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "[3 3 3]\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "import numpy as np\n", 85 | "y = np.asarray([5,5,5])\n", 86 | "print(y-np.asarray(3) + 1)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": { 102 | "collapsed": true 103 | }, 104 | "outputs": [], 105 | "source": [] 106 | } 107 | ], 108 | "metadata": { 109 | "kernelspec": { 110 | "display_name": "Python 3", 111 | "language": "python", 112 | "name": "python3" 113 | }, 114 | "language_info": { 115 | "codemirror_mode": { 116 | "name": "ipython", 117 | "version": 3 118 | }, 119 | "file_extension": ".py", 120 | "mimetype": "text/x-python", 121 | "name": "python", 122 | "nbconvert_exporter": "python", 123 | "pygments_lexer": "ipython3", 124 | "version": "3.5.3" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 1 129 | } 130 | -------------------------------------------------------------------------------- /Lecture 9.2 - Background Subtraction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Background Subtraction" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### 1. Gaussian Mixture-based Background/Foreground Segmentation Algorithm" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "# OpenCV 2.4.13 only\n", 26 | "import numpy as np\n", 27 | "import cv2\n", 28 | "\n", 29 | "cap = cv2.VideoCapture('walking.avi')\n", 30 | "\n", 31 | "# Initlaize background subtractor\n", 32 | "foreground_background = cv2.BackgroundSubtractorMOG()\n", 33 | "\n", 34 | "while True:\n", 35 | " \n", 36 | " ret, frame = cap.read()\n", 37 | "\n", 38 | " # Apply background subtractor to get our foreground mask\n", 39 | " foreground_mask = foreground_background.apply(frame)\n", 40 | "\n", 41 | " cv2.imshow('Output', foreground_mask)\n", 42 | " if cv2.waitKey(1) == 13: \n", 43 | " break\n", 44 | "\n", 45 | "cap.release()\n", 46 | "cv2.destroyAllWindows()" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "### What about using this on our webcam input?" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "# OpenCV 2.4.13 only\n", 65 | "import numpy as np\n", 66 | "import cv2\n", 67 | "\n", 68 | "# Intialize Webcam\n", 69 | "cap = cv2.VideoCapture(0)\n", 70 | "\n", 71 | "# Initlaize background subtractor\n", 72 | "foreground_background = cv2.BackgroundSubtractorMOG()\n", 73 | "\n", 74 | "while True:\n", 75 | " \n", 76 | " ret, frame = cap.read()\n", 77 | "\n", 78 | " # Apply background subtractor to get our foreground mask\n", 79 | " foreground_mask = foreground_background.apply(frame)\n", 80 | "\n", 81 | " cv2.imshow('Output', foreground_mask)\n", 82 | " if cv2.waitKey(1) == 13: \n", 83 | " break\n", 84 | "\n", 85 | "cap.release()\n", 86 | "cv2.destroyAllWindows()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### Let's try the Improved adaptive Gausian mixture model for background subtraction" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "collapsed": false 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "# OpenCV 2.4.13\n", 105 | "import numpy as np\n", 106 | "import cv2\n", 107 | "\n", 108 | "cap = cv2.VideoCapture('walking.avi')\n", 109 | "\n", 110 | "# Initlaize background subtractor\n", 111 | "foreground_background = cv2.BackgroundSubtractorMOG2()\n", 112 | "\n", 113 | "while True:\n", 114 | " ret, frame = cap.read()\n", 115 | "\n", 116 | " # Apply background subtractor to get our foreground mask\n", 117 | " foreground_mask = foreground_background.apply(frame)\n", 118 | "\n", 119 | " cv2.imshow('Output', foreground_mask)\n", 120 | " if cv2.waitKey(1) == 13: \n", 121 | " break\n", 122 | "\n", 123 | "cap.release()\n", 124 | "cv2.destroyAllWindows()" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "### Applying it to our webcam stream" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": { 138 | "collapsed": false 139 | }, 140 | "outputs": [], 141 | "source": [ 142 | "# OpenCV 2.4.13\n", 143 | "import numpy as np\n", 144 | "import cv2\n", 145 | "\n", 146 | "# Intialize Webcam\n", 147 | "cap = cv2.VideoCapture(0)\n", 148 | "\n", 149 | "# Initlaize background subtractor\n", 150 | "foreground_background = cv2.BackgroundSubtractorMOG2()\n", 151 | "\n", 152 | "while True:\n", 153 | " ret, frame = cap.read()\n", 154 | " \n", 155 | " # Apply background subtractor to get our foreground mask\n", 156 | " foreground_mask = foreground_background.apply(frame)\n", 157 | "\n", 158 | " cv2.imshow('Output', foreground_mask)\n", 159 | " if cv2.waitKey(1) == 13: \n", 160 | " break\n", 161 | "\n", 162 | "cap.release()\n", 163 | "cv2.destroyAllWindows()" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "## What about foreground substraction?" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 1, 185 | "metadata": { 186 | "collapsed": false 187 | }, 188 | "outputs": [], 189 | "source": [ 190 | "import cv2\n", 191 | "import numpy as np\n", 192 | "\n", 193 | "# Initalize webacam and store first frame\n", 194 | "cap = cv2.VideoCapture(0)\n", 195 | "ret, frame = cap.read()\n", 196 | "\n", 197 | "# Create a flaot numpy array with frame values\n", 198 | "average = np.float32(frame)\n", 199 | "\n", 200 | "while True:\n", 201 | " # Get webcam frmae\n", 202 | " ret, frame = cap.read()\n", 203 | " \n", 204 | " # 0.01 is the weight of image, play around to see how it changes\n", 205 | " cv2.accumulateWeighted(frame, average, 0.01)\n", 206 | " \n", 207 | " # Scales, calculates absolute values, and converts the result to 8-bit\n", 208 | " background = cv2.convertScaleAbs(average)\n", 209 | "\n", 210 | " cv2.imshow('Input', frame)\n", 211 | " cv2.imshow('Disapearing Background', background)\n", 212 | " \n", 213 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 214 | " break\n", 215 | "\n", 216 | "cv2.destroyAllWindows()\n", 217 | "cap.release()" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "### Background Substraction KKN\n", 225 | "#### OpenCV 3.X only!" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 1, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "# OpenCV 3.1.0\n", 237 | "import numpy as np\n", 238 | "import cv2\n", 239 | "\n", 240 | "cap = cv2.VideoCapture(0)\n", 241 | "\n", 242 | "kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))\n", 243 | "fgbg = cv2.createBackgroundSubtractorKNN()\n", 244 | "\n", 245 | "while(1):\n", 246 | " ret, frame = cap.read()\n", 247 | "\n", 248 | " fgmask = fgbg.apply(frame)\n", 249 | " fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)\n", 250 | "\n", 251 | " cv2.imshow('frame',fgmask)\n", 252 | " \n", 253 | " if cv2.waitKey(1) == 13: \n", 254 | " break\n", 255 | "\n", 256 | "cap.release()\n", 257 | "cv2.destroyAllWindows()" 258 | ] 259 | } 260 | ], 261 | "metadata": { 262 | "kernelspec": { 263 | "display_name": "Python 2", 264 | "language": "python", 265 | "name": "python2" 266 | }, 267 | "language_info": { 268 | "codemirror_mode": { 269 | "name": "ipython", 270 | "version": 2 271 | }, 272 | "file_extension": ".py", 273 | "mimetype": "text/x-python", 274 | "name": "python", 275 | "nbconvert_exporter": "python", 276 | "pygments_lexer": "ipython2", 277 | "version": "2.7.11" 278 | } 279 | }, 280 | "nbformat": 4, 281 | "nbformat_minor": 0 282 | } 283 | -------------------------------------------------------------------------------- /Lecture 9.3 - Meanshift Object Tracking.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Meanshift Object Tracking" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "import cv2\n", 20 | "\n", 21 | "# Initialize webcam\n", 22 | "cap = cv2.VideoCapture(0)\n", 23 | "\n", 24 | "# take first frame of the video\n", 25 | "ret, frame = cap.read()\n", 26 | "print type(frame)\n", 27 | "\n", 28 | "# setup default location of window\n", 29 | "r, h, c, w = 240, 100, 400, 160 \n", 30 | "track_window = (c, r, w, h)\n", 31 | "\n", 32 | "# Crop region of interest for tracking\n", 33 | "roi = frame[r:r+h, c:c+w]\n", 34 | "\n", 35 | "# Convert cropped window to HSV color space\n", 36 | "hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)\n", 37 | "\n", 38 | "# Create a mask between the HSV bounds\n", 39 | "lower_purple = np.array([125,0,0])\n", 40 | "upper_purple = np.array([175,255,255])\n", 41 | "mask = cv2.inRange(hsv_roi, lower_purple, upper_purple)\n", 42 | "\n", 43 | "# Obtain the color histogram of the ROI\n", 44 | "roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0,180])\n", 45 | "\n", 46 | "# Normalize values to lie between the range 0, 255\n", 47 | "cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)\n", 48 | "\n", 49 | "# Setup the termination criteria\n", 50 | "# We stop calculating the centroid shift after ten iterations \n", 51 | "# or if the centroid has moved at least 1 pixel\n", 52 | "term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )\n", 53 | "\n", 54 | "while True:\n", 55 | " \n", 56 | " # Read webcam frame\n", 57 | " ret, frame = cap.read()\n", 58 | "\n", 59 | " if ret == True:\n", 60 | " \n", 61 | " # Convert to HSV\n", 62 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 63 | " \n", 64 | " # Calculate the histogram back projection \n", 65 | " # Each pixel's value is it's probability\n", 66 | " dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)\n", 67 | "\n", 68 | " # apply meanshift to get the new location\n", 69 | " ret, track_window = cv2.meanShift(dst, track_window, term_crit)\n", 70 | "\n", 71 | " # Draw it on image\n", 72 | " x, y, w, h = track_window\n", 73 | " img2 = cv2.rectangle(frame, (x,y), (x+w, y+h), 255, 2) \n", 74 | "\n", 75 | " cv2.imshow('Meansift Tracking', img2)\n", 76 | " \n", 77 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 78 | " break\n", 79 | "\n", 80 | " else:\n", 81 | " break\n", 82 | "\n", 83 | "cv2.destroyAllWindows()\n", 84 | "cap.release()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "### What are cv2.calcHist and cv2.calcBackProject?" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "**cv2.calcHist ** is simply a function calculates the color histograms for an array of images.\n", 99 | "\n", 100 | "**calcBackProject** is a somewhat more complicated function, that calculates the histogram back projection.\n", 101 | "\n", 102 | "In this case, the histogram back projection gives a probability estimate an image is equal to the image the original histogram was generated. \n", 103 | "\n", 104 | "Confused yet?\n", 105 | "\n", 106 | "calcBackProject takes the histogram generated by calcHist and projects it back onto an image. The result is the probability that each pixel belongs to the image that originally generated the histogram.\n" 107 | ] 108 | } 109 | ], 110 | "metadata": { 111 | "kernelspec": { 112 | "display_name": "Python 2", 113 | "language": "python", 114 | "name": "python2" 115 | }, 116 | "language_info": { 117 | "codemirror_mode": { 118 | "name": "ipython", 119 | "version": 2 120 | }, 121 | "file_extension": ".py", 122 | "mimetype": "text/x-python", 123 | "name": "python", 124 | "nbconvert_exporter": "python", 125 | "pygments_lexer": "ipython2", 126 | "version": "2.7.11" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 0 131 | } 132 | -------------------------------------------------------------------------------- /Lecture 9.4 - Camshift Object Tracking.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Camshift Object Tracking" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "import cv2\n", 20 | "\n", 21 | "# Initialize webcam\n", 22 | "cap = cv2.VideoCapture(0)\n", 23 | "\n", 24 | "# take first frame of the video\n", 25 | "ret, frame = cap.read()\n", 26 | "\n", 27 | "# setup default location of window\n", 28 | "r, h, c, w = 240, 100, 400, 160 \n", 29 | "track_window = (c, r, w, h)\n", 30 | "\n", 31 | "# Crop region of interest for tracking\n", 32 | "roi = frame[r:r+h, c:c+w]\n", 33 | "\n", 34 | "# Convert cropped window to HSV color space\n", 35 | "hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)\n", 36 | "\n", 37 | "# Create a mask between the HSV bounds\n", 38 | "lower_purple = np.array([130,60,60])\n", 39 | "upper_purple = np.array([175,255,255])\n", 40 | "mask = cv2.inRange(hsv_roi, lower_purple, upper_purple)\n", 41 | "\n", 42 | "# Obtain the color histogram of the ROI\n", 43 | "roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0,180])\n", 44 | "\n", 45 | "# Normalize values to lie between the range 0, 255\n", 46 | "cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)\n", 47 | "\n", 48 | "# Setup the termination criteria\n", 49 | "# We stop calculating the centroid shift after ten iterations \n", 50 | "# or if the centroid has moved at least 1 pixel\n", 51 | "term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )\n", 52 | "\n", 53 | "while True:\n", 54 | " \n", 55 | " # Read webcam frame\n", 56 | " ret, frame = cap.read()\n", 57 | "\n", 58 | " if ret == True:\n", 59 | " # Convert to HSV\n", 60 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 61 | " \n", 62 | " # Calculate the histogram back projection \n", 63 | " # Each pixel's value is it's probability\n", 64 | " dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)\n", 65 | "\n", 66 | " # apply Camshift to get the new location\n", 67 | " ret, track_window = cv2.CamShift(dst, track_window, term_crit)\n", 68 | "\n", 69 | " # Draw it on image \n", 70 | " # We use polylines to represent Adaptive box \n", 71 | " pts = cv2.boxPoints(ret)\n", 72 | " pts = np.int0(pts)\n", 73 | " img2 = cv2.polylines(frame,[pts],True, 255,2)\n", 74 | " \n", 75 | " cv2.imshow('Camshift Tracking', img2)\n", 76 | " \n", 77 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 78 | " break\n", 79 | "\n", 80 | " else:\n", 81 | " break\n", 82 | "\n", 83 | "cv2.destroyAllWindows()\n", 84 | "cap.release()" 85 | ] 86 | } 87 | ], 88 | "metadata": { 89 | "kernelspec": { 90 | "display_name": "Python 2", 91 | "language": "python", 92 | "name": "python2" 93 | }, 94 | "language_info": { 95 | "codemirror_mode": { 96 | "name": "ipython", 97 | "version": 2 98 | }, 99 | "file_extension": ".py", 100 | "mimetype": "text/x-python", 101 | "name": "python", 102 | "nbconvert_exporter": "python", 103 | "pygments_lexer": "ipython2", 104 | "version": "2.7.11" 105 | } 106 | }, 107 | "nbformat": 4, 108 | "nbformat_minor": 0 109 | } 110 | -------------------------------------------------------------------------------- /Lecture 9.5 - Optical Flow Object Tracking.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Lucas-Kanade Optical Flow in OpenCV\n", 8 | "Requires OpenCV 3.1" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "metadata": { 15 | "collapsed": false 16 | }, 17 | "outputs": [], 18 | "source": [ 19 | "import numpy as np\n", 20 | "import cv2\n", 21 | "\n", 22 | "# Load video stream\n", 23 | "cap = cv2.VideoCapture('images/walking.avi')\n", 24 | "\n", 25 | "# Set parameters for ShiTomasi corner detection\n", 26 | "feature_params = dict( maxCorners = 100,\n", 27 | " qualityLevel = 0.3,\n", 28 | " minDistance = 7,\n", 29 | " blockSize = 7 )\n", 30 | "\n", 31 | "# Set parameters for lucas kanade optical flow\n", 32 | "lucas_kanade_params = dict( winSize = (15,15),\n", 33 | " maxLevel = 2,\n", 34 | " criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))\n", 35 | "\n", 36 | "# Create some random colors\n", 37 | "# Used to create our trails for object movement in the image \n", 38 | "color = np.random.randint(0,255,(100,3))\n", 39 | "\n", 40 | "# Take first frame and find corners in it\n", 41 | "ret, prev_frame = cap.read()\n", 42 | "prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)\n", 43 | "\n", 44 | "# Find inital corner locations\n", 45 | "prev_corners = cv2.goodFeaturesToTrack(prev_gray, mask = None, **feature_params)\n", 46 | "\n", 47 | "# Create a mask image for drawing purposes\n", 48 | "mask = np.zeros_like(prev_frame)\n", 49 | "\n", 50 | "while(1):\n", 51 | " ret, frame = cap.read()\n", 52 | " frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 53 | "\n", 54 | " # calculate optical flow\n", 55 | " new_corners, status, errors = cv2.calcOpticalFlowPyrLK(prev_gray, \n", 56 | " frame_gray, \n", 57 | " prev_corners, \n", 58 | " None, \n", 59 | " **lucas_kanade_params)\n", 60 | "\n", 61 | " # Select and store good points\n", 62 | " good_new = new_corners[status==1]\n", 63 | " good_old = prev_corners[status==1]\n", 64 | "\n", 65 | " # Draw the tracks\n", 66 | " for i,(new,old) in enumerate(zip(good_new, good_old)):\n", 67 | " a, b = new.ravel()\n", 68 | " c, d = old.ravel()\n", 69 | " mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)\n", 70 | " frame = cv2.circle(frame, (a,b), 5, color[i].tolist(),-1)\n", 71 | " \n", 72 | " img = cv2.add(frame,mask)\n", 73 | "\n", 74 | " # Show Optical Flow\n", 75 | " cv2.imshow('Optical Flow - Lucas-Kanade',img)\n", 76 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 77 | " break\n", 78 | "\n", 79 | " # Now update the previous frame and previous points\n", 80 | " prev_gray = frame_gray.copy()\n", 81 | " prev_corners = good_new.reshape(-1,1,2)\n", 82 | "\n", 83 | "cv2.destroyAllWindows()\n", 84 | "cap.release()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "### Dense Optical Flow\n", 92 | "Requires OpenCV 3.1" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 4, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "import cv2\n", 104 | "import numpy as np\n", 105 | "\n", 106 | "# Load video stream\n", 107 | "cap = cv2.VideoCapture(\"images/walking.avi\")\n", 108 | "\n", 109 | "# Get first frame\n", 110 | "ret, first_frame = cap.read()\n", 111 | "previous_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)\n", 112 | "hsv = np.zeros_like(first_frame)\n", 113 | "hsv[...,1] = 255\n", 114 | "\n", 115 | "while True:\n", 116 | " \n", 117 | " # Read of video file\n", 118 | " ret, frame2 = cap.read()\n", 119 | " next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)\n", 120 | "\n", 121 | " # Computes the dense optical flow using the Gunnar Farneback’s algorithm\n", 122 | " flow = cv2.calcOpticalFlowFarneback(previous_gray, next, \n", 123 | " None, 0.5, 3, 15, 3, 5, 1.2, 0)\n", 124 | "\n", 125 | " # use flow to calculate the magnitude (speed) and angle of motion\n", 126 | " # use these values to calculate the color to reflect speed and angle\n", 127 | " magnitude, angle = cv2.cartToPolar(flow[...,0], flow[...,1])\n", 128 | " hsv[...,0] = angle * (180 / (np.pi/2))\n", 129 | " hsv[...,2] = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX)\n", 130 | " final = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)\n", 131 | "\n", 132 | " # Show our demo of Dense Optical Flow\n", 133 | " cv2.imshow('Dense Optical Flow', final)\n", 134 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 135 | " break\n", 136 | " \n", 137 | " # Store current image as previous image\n", 138 | " previous_gray = next\n", 139 | "\n", 140 | "cap.release()\n", 141 | "cv2.destroyAllWindows()" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": { 153 | "collapsed": false 154 | }, 155 | "outputs": [], 156 | "source": [] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": { 162 | "collapsed": false 163 | }, 164 | "outputs": [], 165 | "source": [] 166 | } 167 | ], 168 | "metadata": { 169 | "kernelspec": { 170 | "display_name": "Python 2", 171 | "language": "python", 172 | "name": "python2" 173 | }, 174 | "language_info": { 175 | "codemirror_mode": { 176 | "name": "ipython", 177 | "version": 2 178 | }, 179 | "file_extension": ".py", 180 | "mimetype": "text/x-python", 181 | "name": "python", 182 | "nbconvert_exporter": "python", 183 | "pygments_lexer": "ipython2", 184 | "version": "2.7.11" 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 0 189 | } 190 | -------------------------------------------------------------------------------- /Lecture 9.6 - Mini Project # 11 - Ball Tracking.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Mini Project # 10 - Object Tracking" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 7, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "#Object Tracking\n", 19 | "import cv2\n", 20 | "import numpy as np\n", 21 | "\n", 22 | "# Initalize camera\n", 23 | "cap = cv2.VideoCapture(0)\n", 24 | "\n", 25 | "# define range of purple color in HSV\n", 26 | "lower_purple = np.array([130,50,90])\n", 27 | "upper_purple = np.array([170,255,255])\n", 28 | "\n", 29 | "# Create empty points array\n", 30 | "points = []\n", 31 | "\n", 32 | "# Get default camera window size\n", 33 | "ret, frame = cap.read()\n", 34 | "Height, Width = frame.shape[:2]\n", 35 | "frame_count = 0\n", 36 | "\n", 37 | "while True:\n", 38 | "\n", 39 | " # Capture webcame frame\n", 40 | " ret, frame = cap.read()\n", 41 | " hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 42 | "\n", 43 | " # Threshold the HSV image to get only blue colors\n", 44 | " mask = cv2.inRange(hsv_img, lower_purple, upper_purple)\n", 45 | " #mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)\n", 46 | " \n", 47 | " # Find contours, OpenCV 3.X users use this line instead\n", 48 | " # _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n", 49 | " contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n", 50 | " \n", 51 | " # Create empty centre array to store centroid center of mass\n", 52 | " center = int(Height/2), int(Width/2)\n", 53 | "\n", 54 | " if len(contours) > 0:\n", 55 | " \n", 56 | " # Get the largest contour and its center \n", 57 | " c = max(contours, key=cv2.contourArea)\n", 58 | " (x, y), radius = cv2.minEnclosingCircle(c)\n", 59 | " M = cv2.moments(c)\n", 60 | " try:\n", 61 | " center = (int(M[\"m10\"] / M[\"m00\"]), int(M[\"m01\"] / M[\"m00\"]))\n", 62 | "\n", 63 | " except:\n", 64 | " center = int(Height/2), int(Width/2)\n", 65 | "\n", 66 | " # Allow only countors that have a larger than 15 pixel radius\n", 67 | " if radius > 25:\n", 68 | " \n", 69 | " # Draw cirlce and leave the last center creating a trail\n", 70 | " cv2.circle(frame, (int(x), int(y)), int(radius),(0, 0, 255), 2)\n", 71 | " cv2.circle(frame, center, 5, (0, 255, 0), -1)\n", 72 | " \n", 73 | " # Log center points \n", 74 | " points.append(center)\n", 75 | " \n", 76 | " # loop over the set of tracked points\n", 77 | " if radius > 25:\n", 78 | " for i in range(1, len(points)):\n", 79 | " try:\n", 80 | " cv2.line(frame, points[i - 1], points[i], (0, 255, 0), 2)\n", 81 | " except:\n", 82 | " pass\n", 83 | " \n", 84 | " # Make frame count zero\n", 85 | " frame_count = 0\n", 86 | " else:\n", 87 | " # Count frames \n", 88 | " frame_count += 1\n", 89 | " \n", 90 | " # If we count 10 frames without object lets delete our trail\n", 91 | " if frame_count == 10:\n", 92 | " points = []\n", 93 | " # when frame_count reaches 20 let's clear our trail \n", 94 | " frame_count = 0\n", 95 | " \n", 96 | " # Display our object tracker\n", 97 | " frame = cv2.flip(frame, 1)\n", 98 | " cv2.imshow(\"Object Tracker\", frame)\n", 99 | "\n", 100 | " if cv2.waitKey(1) == 13: #13 is the Enter Key\n", 101 | " break\n", 102 | "\n", 103 | "# Release camera and close any open windows\n", 104 | "cap.release()\n", 105 | "cv2.destroyAllWindows()" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": { 112 | "collapsed": false 113 | }, 114 | "outputs": [], 115 | "source": [] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 2", 121 | "language": "python", 122 | "name": "python2" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 2 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython2", 134 | "version": "2.7.11" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 0 139 | } 140 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Computer Vision - OpenCV3 Python & Machine Learning 2 | 3 | This course is taught by Computer Vision expert from University of Edinberg. The course is implementation oriented and less of theorey. If you do not care about the theorey and are looking for a quick implememntation guide on popular OpenCV algorithms, this is the course to take. 4 | 5 | [Course Website](https://www.udemy.com/master-computer-vision-with-opencv-in-python/) 6 | 7 | ## What I learnt from this course 8 | 9 | - Key concepts of computer Vision & OpenCV. 10 | - Perform image manipulations such as transformations, cropping, blurring, thresholding, edge detection and cropping. 11 | - Segment images by understanding contours, circle, and line detection. How to approximate contours, do contour filtering and ordering as well as approximations. 12 | - Feature detection (SIFT, SURF, FAST, BRIEF & ORB) for object detection. 13 | - Implement Object Detection for faces, people & cars. 14 | - Extract facial landmarks for face analysis, applying filters and face swaps. 15 | - Implement Machine Learning in Computer Vision for handwritten digit recognition. 16 | - Implement Facial Recognition. 17 | - Implement and understand Motion Analysis & Object Tracking. 18 | - Use basic computational photography techniques for Photo Restoration (eliminate marks, lines, creases, and smudges from old damaged photos). 19 | - Getting started in Deep Learning. -------------------------------------------------------------------------------- /Slides/Mastering Computer Vision with OpenCV in Python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/Slides/Mastering Computer Vision with OpenCV in Python.pdf -------------------------------------------------------------------------------- /image_with_landmarks.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/image_with_landmarks.jpg -------------------------------------------------------------------------------- /images/4star.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/4star.jpg -------------------------------------------------------------------------------- /images/Hillary.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/Hillary.jpg -------------------------------------------------------------------------------- /images/IMG_7539.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/IMG_7539.jpg -------------------------------------------------------------------------------- /images/IMG_8295.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/IMG_8295.JPG -------------------------------------------------------------------------------- /images/Origin_of_Species.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/Origin_of_Species.jpg -------------------------------------------------------------------------------- /images/Sunflowers.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/Sunflowers.jpg -------------------------------------------------------------------------------- /images/Trump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/Trump.jpg -------------------------------------------------------------------------------- /images/WaldoBeach.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/WaldoBeach.jpg -------------------------------------------------------------------------------- /images/abraham.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/abraham.jpg -------------------------------------------------------------------------------- /images/abraham_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/abraham_mask.png -------------------------------------------------------------------------------- /images/beatle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/beatle.jpg -------------------------------------------------------------------------------- /images/blobs.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/blobs.jpg -------------------------------------------------------------------------------- /images/bottlecaps.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/bottlecaps.jpg -------------------------------------------------------------------------------- /images/box_in_scene.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/box_in_scene.png -------------------------------------------------------------------------------- /images/bunchofshapes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/bunchofshapes.jpg -------------------------------------------------------------------------------- /images/candy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/candy.jpg -------------------------------------------------------------------------------- /images/cars.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/cars.avi -------------------------------------------------------------------------------- /images/chess.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/chess.JPG -------------------------------------------------------------------------------- /images/chihuahua.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/chihuahua.jpg -------------------------------------------------------------------------------- /images/coffee.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/coffee.jpg -------------------------------------------------------------------------------- /images/contours.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/contours.jpg -------------------------------------------------------------------------------- /images/digits.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/digits.png -------------------------------------------------------------------------------- /images/domino.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/domino.png -------------------------------------------------------------------------------- /images/download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/download.png -------------------------------------------------------------------------------- /images/eigenface_reconstruction_opencv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/eigenface_reconstruction_opencv.png -------------------------------------------------------------------------------- /images/elephant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/elephant.jpg -------------------------------------------------------------------------------- /images/ex2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/ex2.jpg -------------------------------------------------------------------------------- /images/faceswap.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/faceswap.JPG -------------------------------------------------------------------------------- /images/gradient.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/gradient.jpg -------------------------------------------------------------------------------- /images/hand.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/hand.jpg -------------------------------------------------------------------------------- /images/house.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/house.jpg -------------------------------------------------------------------------------- /images/input.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/input.jpg -------------------------------------------------------------------------------- /images/input33.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/input33.JPG -------------------------------------------------------------------------------- /images/kim.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/kim.jpg -------------------------------------------------------------------------------- /images/lourve_noise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/lourve_noise.jpg -------------------------------------------------------------------------------- /images/marsface.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/marsface.jpg -------------------------------------------------------------------------------- /images/mask.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/mask.jpg -------------------------------------------------------------------------------- /images/numbers.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/numbers.jpg -------------------------------------------------------------------------------- /images/obama.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/obama.jpg -------------------------------------------------------------------------------- /images/obamafacerecog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/obamafacerecog.jpg -------------------------------------------------------------------------------- /images/opencv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/opencv.png -------------------------------------------------------------------------------- /images/opencv_inv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/opencv_inv.png -------------------------------------------------------------------------------- /images/output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/output.jpg -------------------------------------------------------------------------------- /images/photorestore.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/photorestore.JPG -------------------------------------------------------------------------------- /images/rot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/rot.jpg -------------------------------------------------------------------------------- /images/scan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/scan.jpg -------------------------------------------------------------------------------- /images/shapes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/shapes.jpg -------------------------------------------------------------------------------- /images/shapes_donut.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/shapes_donut.jpg -------------------------------------------------------------------------------- /images/shapestomatch.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/shapestomatch.jpg -------------------------------------------------------------------------------- /images/soduku.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/soduku.jpg -------------------------------------------------------------------------------- /images/someshapes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/someshapes.jpg -------------------------------------------------------------------------------- /images/tobago.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/tobago.jpg -------------------------------------------------------------------------------- /images/truck.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/truck.jpg -------------------------------------------------------------------------------- /images/vinod_kumar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/vinod_kumar.jpg -------------------------------------------------------------------------------- /images/walking.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/images/walking.avi -------------------------------------------------------------------------------- /output_shape_number_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/output_shape_number_1.jpg -------------------------------------------------------------------------------- /output_shape_number_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/output_shape_number_2.jpg -------------------------------------------------------------------------------- /output_shape_number_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/output_shape_number_3.jpg -------------------------------------------------------------------------------- /output_shape_number_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imvinod/Computer-Vision-OpenCV3-Udemy/48b60893b2d520aa941370f0817449370d6c676e/output_shape_number_4.jpg --------------------------------------------------------------------------------