Skip to content

Commit 970a901

Browse files
authored
Merge pull request #1 from Yathsih/Image-Processing
Image Processing
2 parents ebac61f + f2b6c63 commit 970a901

10 files changed

+2361
-0
lines changed

00-Opening-Image-Files-in-a-Notebook.ipynb

+673
Large diffs are not rendered by default.

01-Opening-Image-Files-OpenCV.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import cv2
2+
3+
img = cv2.imread('DATA/00-puppy.jpg')
4+
5+
while True:
6+
7+
cv2.imshow('Puppy',img)
8+
9+
# EXPLANATION FOR THIS LINE OF CODE:
10+
# https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1/39201163
11+
12+
# IF we've waited at least 1 ms AND we've pressed the Esc
13+
if cv2.waitKey(1) & 0xFF == 27:
14+
break
15+
16+
cv2.destroyAllWindows()

01-Opening-Images-with-OpenCV.ipynb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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>\n",
8+
"\n",
9+
"# Opening Image Files with OpenCV"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"OpenCV can open images in separate outside windows when you run a .py script. Keep in mind, at the moment, this often doesn't work with the iPython kernel in a Jupyter notebook for some complex reasons. (It's an open issue on the github page since solutions stil vary drastically depending on the user's OS).\n",
17+
"\n",
18+
"Let's explore how we can use Open CV to open files in a separate window. Typically you would run these sort of commands as a script! So we've also included a .py script that goes along with this lecture."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 6,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"# MUST BE RUN AS .py SCRIPT IN ORDER TO WORK.\n",
28+
"# PLEASE MAKE SURE TO WATCH THE FULL VIDEO FOR THE EXPLANATION TO THIS NOTEBOOK\n",
29+
"# TO BE CLEAR: RUNNING THIS CELL WILL KILL THE KERNEL IF YOU USE JUPYTER DIRECTLY\n",
30+
"\n",
31+
"import cv2\n",
32+
"\n",
33+
"img = cv2.imread('../DATA/00-puppy.jpg',cv2.IMREAD_GRAYSCALE)\n",
34+
"# Show the image with OpenCV\n",
35+
"cv2.imshow('window_name',img)\n",
36+
"# Wait for something on keyboard to be pressed to close window.\n",
37+
"cv2.waitKey()"
38+
]
39+
}
40+
],
41+
"metadata": {
42+
"kernelspec": {
43+
"display_name": "Python 3",
44+
"language": "python",
45+
"name": "python3"
46+
},
47+
"language_info": {
48+
"codemirror_mode": {
49+
"name": "ipython",
50+
"version": 3
51+
},
52+
"file_extension": ".py",
53+
"mimetype": "text/x-python",
54+
"name": "python",
55+
"nbconvert_exporter": "python",
56+
"pygments_lexer": "ipython3",
57+
"version": "3.6.6"
58+
}
59+
},
60+
"nbformat": 4,
61+
"nbformat_minor": 2
62+
}

02-Drawing-on-Images.ipynb

+545
Large diffs are not rendered by default.

03-Direct-Drawing-with-Mouse.ipynb

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

Comments
 (0)