-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
818 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
2_Convolutional_Filters_Edge_Detection/2. Finding Edges and Custom Kernels.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Creating a Filter, Edge Detection" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import resources and display image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"import matplotlib.image as mpimg\n", | ||
"\n", | ||
"import cv2\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"%matplotlib inline\n", | ||
"\n", | ||
"# Read in the image\n", | ||
"image = mpimg.imread('images/curved_lane.jpg')\n", | ||
"\n", | ||
"plt.imshow(image)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Convert the image to grayscale" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Convert to grayscale for filtering\n", | ||
"gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n", | ||
"\n", | ||
"plt.imshow(gray, cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### TODO: Create a custom kernel\n", | ||
"\n", | ||
"Below, you've been given one common type of edge detection filter: a Sobel operator.\n", | ||
"\n", | ||
"The Sobel filter is very commonly used in edge detection and in finding patterns in intensity in an image. Applying a Sobel filter to an image is a way of **taking (an approximation) of the derivative of the image** in the x or y direction, separately. The operators look as follows.\n", | ||
"\n", | ||
"<img src=\"images/sobel_ops.png\" width=200 height=200>\n", | ||
"\n", | ||
"**It's up to you to create a Sobel x operator and apply it to the given image.**\n", | ||
"\n", | ||
"For a challenge, see if you can put the image through a series of filters: first one that blurs the image (takes an average of pixels), and then one that detects the edges." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create a custom kernel\n", | ||
"\n", | ||
"# 3x3 array for edge detection\n", | ||
"sobel_y = np.array([[ -1, -2, -1], \n", | ||
" [ 0, 0, 0], \n", | ||
" [ 1, 2, 1]])\n", | ||
"\n", | ||
"## TODO: Create and apply a Sobel x operator\n", | ||
"\n", | ||
"\n", | ||
"# Filter the image using filter2D, which has inputs: (grayscale image, bit-depth, kernel) \n", | ||
"filtered_image = cv2.filter2D(gray, -1, sobel_y)\n", | ||
"\n", | ||
"plt.imshow(filtered_image, cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Test out other filters!\n", | ||
"\n", | ||
"You're encouraged to create other kinds of filters and apply them to see what happens! As an **optional exercise**, try the following:\n", | ||
"* Create a filter with decimal value weights.\n", | ||
"* Create a 5x5 filter\n", | ||
"* Apply your filters to the other images in the `images` directory.\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"anaconda-cloud": {}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
144 changes: 144 additions & 0 deletions
144
2_Convolutional_Filters_Edge_Detection/3. Gaussian Blur.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Gaussian Blur, Medical Images" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import resources and display image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import cv2\n", | ||
"\n", | ||
"%matplotlib inline\n", | ||
"\n", | ||
"# Read in the image\n", | ||
"image = cv2.imread('images/brain_MR.jpg')\n", | ||
"\n", | ||
"# Make a copy of the image\n", | ||
"image_copy = np.copy(image)\n", | ||
"\n", | ||
"# Change color to RGB (from BGR)\n", | ||
"image_copy = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)\n", | ||
"\n", | ||
"plt.imshow(image_copy)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Gaussian blur the image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Convert to grayscale for filtering\n", | ||
"gray = cv2.cvtColor(image_copy, cv2.COLOR_RGB2GRAY)\n", | ||
"\n", | ||
"# Create a Gaussian blurred image\n", | ||
"gray_blur = cv2.GaussianBlur(gray, (9, 9), 0)\n", | ||
"\n", | ||
"f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))\n", | ||
"\n", | ||
"ax1.set_title('original gray')\n", | ||
"ax1.imshow(gray, cmap='gray')\n", | ||
"\n", | ||
"ax2.set_title('blurred image')\n", | ||
"ax2.imshow(gray_blur, cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Test performance with a high-pass filter" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# High-pass filter \n", | ||
"\n", | ||
"# 3x3 sobel filters for edge detection\n", | ||
"sobel_x = np.array([[ -1, 0, 1], \n", | ||
" [ -2, 0, 2], \n", | ||
" [ -1, 0, 1]])\n", | ||
"\n", | ||
"\n", | ||
"sobel_y = np.array([[ -1, -2, -1], \n", | ||
" [ 0, 0, 0], \n", | ||
" [ 1, 2, 1]])\n", | ||
"\n", | ||
"\n", | ||
"# Filter the orginal and blurred grayscale images using filter2D\n", | ||
"filtered = cv2.filter2D(gray, -1, sobel_x)\n", | ||
"\n", | ||
"filtered_blurred = cv2.filter2D(gray_blur, -1, sobel_y)\n", | ||
"f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))\n", | ||
"\n", | ||
"ax1.set_title('original gray')\n", | ||
"ax1.imshow(filtered, cmap='gray')\n", | ||
"\n", | ||
"ax2.set_title('blurred image')\n", | ||
"ax2.imshow(filtered_blurred, cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create threshold that sets all the filtered pixels to white\n", | ||
"# Above a certain threshold\n", | ||
"\n", | ||
"retval, binary_image = cv2.threshold(filtered_blurred, 50, 255, cv2.THRESH_BINARY)\n", | ||
"\n", | ||
"plt.imshow(binary_image, cmap='gray')\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
Oops, something went wrong.