|
| 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 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "# Press CTRL + ENTER to run this line\n", |
| 26 | + "# You should see an * between the [ ] on the left\n", |
| 27 | + "# OpenCV takes a couple seconds to import the first time\n", |
| 28 | + "\n", |
| 29 | + "import cv2" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": null, |
| 35 | + "metadata": { |
| 36 | + "collapsed": true |
| 37 | + }, |
| 38 | + "outputs": [], |
| 39 | + "source": [] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 2, |
| 44 | + "metadata": { |
| 45 | + "collapsed": true |
| 46 | + }, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "# Now let's import numpy\n", |
| 50 | + "# We use as np, so that everything we call on numpy, we can type np instead\n", |
| 51 | + "# It's short and looks neater\n", |
| 52 | + "\n", |
| 53 | + "import numpy as np " |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "Let's now load our first image" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 2, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "# We don't need to do this again, but it's a good habit\n", |
| 70 | + "import cv2 \n", |
| 71 | + "\n", |
| 72 | + "# Load an image using 'imread' specifying the path to image\n", |
| 73 | + "input = cv2.imread('./images/input.jpg')\n", |
| 74 | + "\n", |
| 75 | + "# Our file 'input.jpg' is now loaded and stored in python \n", |
| 76 | + "# as a varaible we named 'image'\n", |
| 77 | + "\n", |
| 78 | + "# To display our image variable, we use 'imshow'\n", |
| 79 | + "# The first parameter will be title shown on image window\n", |
| 80 | + "# The second parameter is the image varialbe\n", |
| 81 | + "cv2.imshow('Hello World', input)\n", |
| 82 | + "\n", |
| 83 | + "# 'waitKey' allows us to input information when a image window is open\n", |
| 84 | + "# By leaving it blank it just waits for anykey to be pressed before \n", |
| 85 | + "# continuing. By placing numbers (except 0), we can specify a delay for\n", |
| 86 | + "# how long you keep the window open (time is in milliseconds here)\n", |
| 87 | + "cv2.waitKey()\n", |
| 88 | + "\n", |
| 89 | + "# This closes all open windows \n", |
| 90 | + "# Failure to place this will cause your program to hang\n", |
| 91 | + "cv2.destroyAllWindows()" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "metadata": { |
| 98 | + "collapsed": true |
| 99 | + }, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "# Same as above without the extraneous comments\n", |
| 103 | + "\n", |
| 104 | + "import cv2 \n", |
| 105 | + "\n", |
| 106 | + "input = cv2.imread('./images/input.jpg')\n", |
| 107 | + "\n", |
| 108 | + "cv2.imshow('Hello World', input)\n", |
| 109 | + "cv2.waitKey(0)\n", |
| 110 | + "cv2.destroyAllWindows()" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "### Let's take a closer look at how images are stored" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 16, |
| 123 | + "metadata": { |
| 124 | + "collapsed": true |
| 125 | + }, |
| 126 | + "outputs": [], |
| 127 | + "source": [ |
| 128 | + "# Import numpy\n", |
| 129 | + "import numpy as np" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 15, |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [ |
| 137 | + { |
| 138 | + "name": "stdout", |
| 139 | + "output_type": "stream", |
| 140 | + "text": [ |
| 141 | + "(830L, 1245L, 3L)\n" |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "print input.shape" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "#### Shape gives the dimensions of the image array\n", |
| 154 | + "\n", |
| 155 | + "The 2D dimensions are 830 pixels in high bv 1245 pixels wide.\n", |
| 156 | + "The '3L' means that there are 3 other components (RGB) that make up this image." |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 17, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "name": "stdout", |
| 166 | + "output_type": "stream", |
| 167 | + "text": [ |
| 168 | + "Height of Image: 830 pixels\n", |
| 169 | + "Width of Image: 1245 pixels\n" |
| 170 | + ] |
| 171 | + } |
| 172 | + ], |
| 173 | + "source": [ |
| 174 | + "# Let's print each dimension of the image\n", |
| 175 | + "\n", |
| 176 | + "print 'Height of Image:', int(input.shape[0]), 'pixels'\n", |
| 177 | + "print 'Width of Image: ', int(input.shape[1]), 'pixels'" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "### How do we save images we edit in OpenCV?" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": 18, |
| 190 | + "metadata": {}, |
| 191 | + "outputs": [ |
| 192 | + { |
| 193 | + "data": { |
| 194 | + "text/plain": [ |
| 195 | + "True" |
| 196 | + ] |
| 197 | + }, |
| 198 | + "execution_count": 18, |
| 199 | + "metadata": {}, |
| 200 | + "output_type": "execute_result" |
| 201 | + } |
| 202 | + ], |
| 203 | + "source": [ |
| 204 | + "# Simply use 'imwrite' specificing the file name and the image to be saved\n", |
| 205 | + "cv2.imwrite('output.jpg', input)\n", |
| 206 | + "cv2.imwrite('output.png', input)" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "code", |
| 211 | + "execution_count": null, |
| 212 | + "metadata": { |
| 213 | + "collapsed": true |
| 214 | + }, |
| 215 | + "outputs": [], |
| 216 | + "source": [] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": null, |
| 221 | + "metadata": { |
| 222 | + "collapsed": true |
| 223 | + }, |
| 224 | + "outputs": [], |
| 225 | + "source": [] |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "code", |
| 229 | + "execution_count": null, |
| 230 | + "metadata": { |
| 231 | + "collapsed": true |
| 232 | + }, |
| 233 | + "outputs": [], |
| 234 | + "source": [] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": null, |
| 239 | + "metadata": { |
| 240 | + "collapsed": true |
| 241 | + }, |
| 242 | + "outputs": [], |
| 243 | + "source": [] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "code", |
| 247 | + "execution_count": null, |
| 248 | + "metadata": { |
| 249 | + "collapsed": true |
| 250 | + }, |
| 251 | + "outputs": [], |
| 252 | + "source": [] |
| 253 | + } |
| 254 | + ], |
| 255 | + "metadata": { |
| 256 | + "kernelspec": { |
| 257 | + "display_name": "Python 2", |
| 258 | + "language": "python", |
| 259 | + "name": "python2" |
| 260 | + }, |
| 261 | + "language_info": { |
| 262 | + "codemirror_mode": { |
| 263 | + "name": "ipython", |
| 264 | + "version": 2 |
| 265 | + }, |
| 266 | + "file_extension": ".py", |
| 267 | + "mimetype": "text/x-python", |
| 268 | + "name": "python", |
| 269 | + "nbconvert_exporter": "python", |
| 270 | + "pygments_lexer": "ipython2", |
| 271 | + "version": "2.7.14" |
| 272 | + } |
| 273 | + }, |
| 274 | + "nbformat": 4, |
| 275 | + "nbformat_minor": 1 |
| 276 | +} |
0 commit comments