# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("4_scikit_image_q", "week_9", "readings", assignment_points = 45.0, assignment_tag = 'week9-readings')
# Initialize Otter
import otter
grader = otter.Notebook("4_scikit_image_q.ipynb")
โ๐งฉ Survive in the World into Pixels of Interest#
# Run this block of code by pressing Shift + Enter to display the question
from questions._4_scikit_image_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._4_scikit_image_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._4_scikit_image_q import Question3
Question3().show()
Question 1 (Points: 14.0): Tipuโs Tiger Mask Analysis#
Instructions:#
Tipuโs Tiger is an 18th-century mechanical automaton and pipe organ created during the reign of Tipu Sultan, the ruler of Mysore, symbolizing his resistance against British colonial forces. It depicts a tiger mauling a European soldier, with a crank-operated mechanism that produces tiger growls, the soldierโs cries, and music from a hidden organ that can be played via a keyboard. Captured after Tipuโs death in 1799 during the fall of Seringapatam, it was taken to England as a war trophy and is now housed in the Victoria and Albert Museum in London. This artifact remains a powerful symbol of Tipuโs defiance, his kingdomโs technological ingenuity, and cultural sophistication.
Suppose you are tasked with converting the background of an image of this artifact to match Drexel branding. For this you want to isolate the tiger from the background using masking techniques in scikit-image
, and replace the black background with the Drexel blue color [0, 47, 108] Complete the following:
Your job is to implement the process_tipu_tiger() function that takes the image as input and return the original image, the modified image, mask, and the modified image with the Drexel blue background. We have provided the scaffold for the function.
Load the image โtipu-tiger.jpgโ from the folder, and store it in a variable named
image
. You can use theski.io.imread
function to load the image.Create a mask: Isolate pixels with intensity in a specific range (example: RGB channel sum > 150). Store the mask in a variable named
mask
. Hint: you should use thenumpy.sum
function to sum the RGB channels, making sure to do this along the 3rd axis. You can use the logical operator>
to create the mask.Create a disk of radius 28 using the
morphology.disk
function from theskimage
module. Store the disk in a variable namedselem
. Note: selem stands for โstructuring elementโ used in morphological operations of images. We have already imported the necessary modules for you. This should provide a good hint.Apply a dilation operation to the mask, and then an erosion operation to the dilated mask using the
morphology.dilation
andmorphology.erosion
functions from theskimage
module. Store the dilated mask in a variable nameddilated_mask
and the eroded mask in a variable namederoded_mask
. The process of dilation followed by erosion is known as closing in image processing.Remove small holes using the
remove_small_holes
function from theskimage.morphology
module. Store the result in a variable namedfinal_mask
. Make sure to set thearea_threshold
parameter to 5000.Replace all pixels not in the masked region with the color black. This can be done by indexing the image with the inverse of the mask. Hint: you can use the ~ operator to invert the mask. A black image has RGB values of [0, 0, 0]. Store the modified image in a variable named
modified_image
.Replace the black background with the Drexel blue color
[0, 47, 108]
. Store the modified image with the Drexel blue background in a variable namedmodified_image_drexel
. This should be done similarly to step 6, but using the Drexel blue color instead of black.
When you run the code, you will see plots generated using the provided code.
import skimage as ski
import numpy as np
from skimage.morphology import dilation, erosion, disk, remove_small_holes
import matplotlib.pyplot as plt
def process_tipu_tiger(image_path="tipu-tiger.jpg"):
# Load the image
# 1. Load the image "tipu-tiger.jpg" from the folder, and store it in a variable named `image`.
# You can use the `ski.io.imread` function to load the image.
...
# 2. Create a mask: Isolate pixels with intensity in a specific range (example: RGB channel sum > 150)
...
# 3. Create the disk-shaped structuring element
...
# 4. Apply the dilation and erosion operations
...
# 5. Fill holes in the mask
...
# makes a copy of the image
# Do not modify this line
modified_image = image.copy()
# 6. Replace the background with black color
...
# makes a copy of the resulting image
modified_image_drexel = modified_image.copy()
# 7. Replaces the background with the Drexel blue color
...
return (
image,
modified_image,
mask,
final_mask,
modified_image_drexel,
dilated_mask,
eroded_mask,
)
# Example usage
(
image,
modified_image,
mask,
final_mask,
modified_image_drexel,
dilated_mask,
eroded_mask,
) = process_tipu_tiger()
# Plot results
plt.figure(figsize=(12, 6))
plt.subplot(1, 5, 1)
plt.imshow(image)
plt.title("Image")
plt.axis("off")
plt.subplot(1, 5, 2)
plt.imshow(modified_image)
plt.title("Masked Image")
plt.axis("off")
plt.subplot(1, 5, 3)
plt.imshow(mask, cmap="gray")
plt.title("Mask")
plt.axis("off")
plt.subplot(1, 5, 4)
plt.imshow(final_mask, cmap="gray")
plt.title("Final Mask")
plt.axis("off")
plt.subplot(1, 5, 5)
plt.imshow(modified_image_drexel)
plt.title("Recolored Image")
plt.axis("off")
plt.tight_layout()
plt.show()
grader.check("tipus-tiger-mask-calculation")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week9-readings", "4_scikit_image_q")