-
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
11 changed files
with
2,536 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
1_Image_Representation/2. Visualizing RGB Channels.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,105 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# RGB colorspace" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import resources" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"import matplotlib.image as mpimg\n", | ||
"\n", | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Read in an image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Read in the image\n", | ||
"image = mpimg.imread('images/wa_state_highway.jpg')\n", | ||
"\n", | ||
"plt.imshow(image)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### RGB channels \n", | ||
"\n", | ||
"Visualize the levels of each color channel. Pay close attention to the traffic signs!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Isolate RGB channels\n", | ||
"r = image[:,:,0]\n", | ||
"g = image[:,:,1]\n", | ||
"b = image[:,:,2]\n", | ||
"\n", | ||
"# Visualize the individual color channels\n", | ||
"f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20,10))\n", | ||
"ax1.set_title('R channel')\n", | ||
"ax1.imshow(r, cmap='gray')\n", | ||
"ax2.set_title('G channel')\n", | ||
"ax2.imshow(g, cmap='gray')\n", | ||
"ax3.set_title('B channel')\n", | ||
"ax3.imshow(b, cmap='gray')\n", | ||
"\n", | ||
"## Which area has the lowest value for red? What about for blue?" | ||
] | ||
} | ||
], | ||
"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 | ||
} |
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,200 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Color Threshold, Blue Screen" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import resources" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"import cv2\n", | ||
"\n", | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Read in and display the image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Read in the image\n", | ||
"image = cv2.imread('images/pizza_bluescreen.jpg')\n", | ||
"\n", | ||
"# Print out the type of image data and its dimensions (height, width, and color)\n", | ||
"print('This image is:', type(image), \n", | ||
" ' with dimensions:', image.shape)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# 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", | ||
"# Display the image copy\n", | ||
"plt.imshow(image_copy)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Define the color threshold" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"## TODO: Define the color selection boundaries in RGB values\n", | ||
"# play around with these values until you isolate the blue background\n", | ||
"lower_blue = np.array([0,0,200]) \n", | ||
"upper_blue = np.array([250,250,255])\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Create a mask" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Define the masked area\n", | ||
"mask = cv2.inRange(image_copy, lower_blue, upper_blue)\n", | ||
"\n", | ||
"# Vizualize the mask\n", | ||
"plt.imshow(mask, cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Mask the image to let the pizza show through\n", | ||
"masked_image = np.copy(image_copy)\n", | ||
"\n", | ||
"masked_image[mask != 0] = [0, 0, 0]\n", | ||
"\n", | ||
"# Display it!\n", | ||
"plt.imshow(masked_image)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Mask and add a background image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Load in a background image, and convert it to RGB \n", | ||
"background_image = cv2.imread('images/space_background.jpg')\n", | ||
"background_image = cv2.cvtColor(background_image, cv2.COLOR_BGR2RGB)\n", | ||
"\n", | ||
"# Crop it to the right size (514x816)\n", | ||
"crop_background = background_image[0:514, 0:816]\n", | ||
"\n", | ||
"# Mask the cropped background so that the pizza area is blocked\n", | ||
"crop_background[mask == 0] = [0, 0, 0]\n", | ||
"\n", | ||
"# Display the background\n", | ||
"plt.imshow(crop_background)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Create a complete image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Add the two images together to create a complete image!\n", | ||
"complete_image = masked_image + crop_background\n", | ||
"\n", | ||
"# Display the result\n", | ||
"plt.imshow(complete_image)" | ||
] | ||
}, | ||
{ | ||
"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 | ||
} |
Oops, something went wrong.