|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<a href=\"https://www.pieriandata.com\"><img src=\"../DATA/Logo.jpg\"></a>" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "# Direct Drawing with Mouse\n", |
| 15 | + "\n", |
| 16 | + "## NOTE: THESE SHOULD ALL BE RUN AS A .py SCRIPT. View video for full details." |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "## SCRIPT 1: Connecting a Function for Drawing" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": { |
| 30 | + "collapsed": true |
| 31 | + }, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "import cv2\n", |
| 35 | + "import numpy as np\n", |
| 36 | + "# Create a function based on a CV2 Event (Left button click)\n", |
| 37 | + "def draw_circle(event,x,y,flags,param):\n", |
| 38 | + " if event == cv2.EVENT_LBUTTONDOWN:\n", |
| 39 | + " cv2.circle(img,(x,y),100,(0,255,0),-1)\n", |
| 40 | + "\n", |
| 41 | + "# Create a black image\n", |
| 42 | + "img = np.zeros((512,512,3), np.uint8)\n", |
| 43 | + "# This names the window so we can reference it \n", |
| 44 | + "cv2.namedWindow(winname='my_drawing')\n", |
| 45 | + "# Connects the mouse button to our callback function\n", |
| 46 | + "cv2.setMouseCallback('my_drawing',draw_circle)\n", |
| 47 | + "\n", |
| 48 | + "while True: #Runs forever until we break with Esc key on keyboard\n", |
| 49 | + " # Shows the image window\n", |
| 50 | + " cv2.imshow('my_drawing',img)\n", |
| 51 | + " # EXPLANATION FOR THIS LINE OF CODE:\n", |
| 52 | + " # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1/39201163\n", |
| 53 | + " if cv2.waitKey(20) & 0xFF == 27:\n", |
| 54 | + " break\n", |
| 55 | + "# Once script is done, its usually good practice to call this line\n", |
| 56 | + "# It closes all windows (just in case you have multiple windows called)\n", |
| 57 | + "cv2.destroyAllWindows()\n" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "## SCRIPT 2: Adding Functionality with Event Choices" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "---------" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": null, |
| 77 | + "metadata": { |
| 78 | + "collapsed": true |
| 79 | + }, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "import cv2\n", |
| 83 | + "import numpy as np\n", |
| 84 | + "\n", |
| 85 | + "\n", |
| 86 | + "# Create a function based on a CV2 Event (Left button click)\n", |
| 87 | + "def draw_circle(event,x,y,flags,param):\n", |
| 88 | + " if event == cv2.EVENT_LBUTTONDOWN:\n", |
| 89 | + " cv2.circle(img,(x,y),100,(0,255,0),-1)\n", |
| 90 | + " elif event == cv2.EVENT_RBUTTONDOWN:\n", |
| 91 | + " cv2.circle(img,(x,y),100,(0,0,255),-1)\n", |
| 92 | + " \n", |
| 93 | + "\n", |
| 94 | + "# Create a black image\n", |
| 95 | + "img = np.zeros((512,512,3), np.uint8)\n", |
| 96 | + "# This names the window so we can reference it \n", |
| 97 | + "cv2.namedWindow(winname='my_drawing')\n", |
| 98 | + "# Connects the mouse button to our callback function\n", |
| 99 | + "cv2.setMouseCallback('my_drawing',draw_circle)\n", |
| 100 | + "\n", |
| 101 | + "while True: #Runs forever until we break with Esc key on keyboard\n", |
| 102 | + " # Shows the image window\n", |
| 103 | + " cv2.imshow('my_drawing',img)\n", |
| 104 | + " # EXPLANATION FOR THIS LINE OF CODE:\n", |
| 105 | + " # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1/39201163\n", |
| 106 | + " if cv2.waitKey(20) & 0xFF == 27:\n", |
| 107 | + " break\n", |
| 108 | + "# Once script is done, its usually good practice to call this line\n", |
| 109 | + "# It closes all windows (just in case you have multiple windows called)\n", |
| 110 | + "cv2.destroyAllWindows()\n" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "# SCRIPT 3: Dragging with Mouse" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": null, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [], |
| 125 | + "source": [ |
| 126 | + "import cv2\n", |
| 127 | + "import numpy as np\n", |
| 128 | + "\n", |
| 129 | + "\n", |
| 130 | + "# Create a function based on a CV2 Event (Left button click)\n", |
| 131 | + "drawing = False # True if mouse is pressed\n", |
| 132 | + "ix,iy = -1,-1\n", |
| 133 | + "\n", |
| 134 | + "# mouse callback function\n", |
| 135 | + "def draw_rectangle(event,x,y,flags,param):\n", |
| 136 | + " global ix,iy,drawing,mode\n", |
| 137 | + "\n", |
| 138 | + " if event == cv2.EVENT_LBUTTONDOWN:\n", |
| 139 | + " # When you click DOWN with left mouse button drawing is set to True\n", |
| 140 | + " drawing = True\n", |
| 141 | + " # Then we take note of where that mouse was located\n", |
| 142 | + " ix,iy = x,y\n", |
| 143 | + "\n", |
| 144 | + " elif event == cv2.EVENT_MOUSEMOVE:\n", |
| 145 | + " # Now the mouse is moving\n", |
| 146 | + " if drawing == True:\n", |
| 147 | + " # If drawing is True, it means you've already clicked on the left mouse button\n", |
| 148 | + " # We draw a rectangle from the previous position to the x,y where the mouse is\n", |
| 149 | + " cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)\n", |
| 150 | + " \n", |
| 151 | + "\n", |
| 152 | + " elif event == cv2.EVENT_LBUTTONUP:\n", |
| 153 | + " # Once you lift the mouse button, drawing is False\n", |
| 154 | + " drawing = False\n", |
| 155 | + " # we complete the rectangle.\n", |
| 156 | + " cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)\n", |
| 157 | + " \n", |
| 158 | + " \n", |
| 159 | + "\n", |
| 160 | + "# Create a black image\n", |
| 161 | + "img = np.zeros((512,512,3), np.uint8)\n", |
| 162 | + "# This names the window so we can reference it \n", |
| 163 | + "cv2.namedWindow(winname='my_drawing')\n", |
| 164 | + "# Connects the mouse button to our callback function\n", |
| 165 | + "cv2.setMouseCallback('my_drawing',draw_rectangle)\n", |
| 166 | + "\n", |
| 167 | + "while True: #Runs forever until we break with Esc key on keyboard\n", |
| 168 | + " # Shows the image window\n", |
| 169 | + " cv2.imshow('my_drawing',img)\n", |
| 170 | + " # EXPLANATION FOR THIS LINE OF CODE:\n", |
| 171 | + " # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1/39201163\n", |
| 172 | + " \n", |
| 173 | + " # CHECK TO SEE IF ESC WAS PRESSED ON KEYBOARD\n", |
| 174 | + " if cv2.waitKey(1) & 0xFF == 27:\n", |
| 175 | + " break\n", |
| 176 | + "# Once script is done, its usually good practice to call this line\n", |
| 177 | + "# It closes all windows (just in case you have multiple windows called)\n", |
| 178 | + "cv2.destroyAllWindows()" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": null, |
| 184 | + "metadata": { |
| 185 | + "collapsed": true |
| 186 | + }, |
| 187 | + "outputs": [], |
| 188 | + "source": [] |
| 189 | + } |
| 190 | + ], |
| 191 | + "metadata": { |
| 192 | + "kernelspec": { |
| 193 | + "display_name": "Python 3", |
| 194 | + "language": "python", |
| 195 | + "name": "python3" |
| 196 | + }, |
| 197 | + "language_info": { |
| 198 | + "codemirror_mode": { |
| 199 | + "name": "ipython", |
| 200 | + "version": 3 |
| 201 | + }, |
| 202 | + "file_extension": ".py", |
| 203 | + "mimetype": "text/x-python", |
| 204 | + "name": "python", |
| 205 | + "nbconvert_exporter": "python", |
| 206 | + "pygments_lexer": "ipython3", |
| 207 | + "version": "3.6.6" |
| 208 | + } |
| 209 | + }, |
| 210 | + "nbformat": 4, |
| 211 | + "nbformat_minor": 2 |
| 212 | +} |
0 commit comments