๐Ÿš€ Getting Started with scikit-image ๐ŸŒŸ#

Welcome to the world of scikit-image! This Python package lets you process and analyze images ๐Ÿ“ธ like a pro. Whether youโ€™re detecting objects ๐Ÿ”, enhancing details ๐ŸŽจ, or doing something super cool like spotting cracks in spaceship panels ๐Ÿš€, scikit-image has you covered.

Importing scikit-image ๐ŸŽ’#

First things first, you need to import scikit-image. Itโ€™s as simple as:

import skimage as ski
import matplotlib.pyplot as plt

Most of scikit-imageโ€™s magic ๐Ÿช„ lives in its submodules. For example:

camera = ski.data.camera()
plt.imshow(camera, cmap="gray")
<matplotlib.image.AxesImage at 0x7f5adcb02ed0>
../../_images/6f93208d3b87c0bcca5aa140067f7f83f9945acb2e7c90a8f13ed2ae6c7f068d.png

A full list of submodules and functions is available on the API reference webpage.

Images = NumPy Arrays ๐Ÿค–#

In scikit-image, images are just NumPy arrays. This means you can apply all the powerful tools from NumPy to manipulate and analyze them. For instance:

type(camera)

# A grayscale image with 512 rows and 512 columns
camera.shape
(512, 512)

๐Ÿ’ก Fun Fact: This structure is what makes scikit-image so versatileโ€”it doesnโ€™t care if your image is a simple black-and-white picture ๐Ÿ–ค๐Ÿค or a colorful snapshot of a nebula ๐ŸŒŒ!

Starting with Example Images ๐Ÿ“ธโœจ#

To help you hit the ground running, scikit-image provides built-in example images. No need to scramble for test data when you can start with something like this:

coins = ski.data.coins()
threshold_value = ski.filters.threshold_otsu(coins)
threshold_value
np.int64(107)

๐ŸŽฏ Engineering Insight: Otsuโ€™s method is often used in manufacturing to detect defects, such as air bubbles in car tires ๐Ÿ›ž or soldering issues on circuit boards ๐Ÿ’ป.

Loading Your Own Images ๐Ÿ“‚โžก๏ธ๐Ÿ–ผ๏ธ#

If you want to use your own images (e.g., scans of ancient manuscripts ๐Ÿ“œ or satellite snapshots ๐Ÿ›ฐ๏ธ), scikit-image makes it super easy:

import os

filename = "./assets/figures/Mario_the_Magnificent.jpg"
mario = ski.io.imread(filename)

plt.imshow(mario)
<matplotlib.image.AxesImage at 0x7f5ad6b63b10>
../../_images/baceca8999497be48729aa4682a0bed8536be2481bf9f3cb97c8a1ff458702f4.png

๐ŸŒ™ Engineering Spotlight: Satellite imaging engineers process lunar surface images like these to find safe landing sites for rovers! ๐Ÿค–๐Ÿ›ฌ

Working with Multiple Images ๐Ÿ“๐Ÿ–ผ๏ธ๐Ÿ–ผ๏ธ๐Ÿ–ผ๏ธ#

Need to load a batch of images for analysis? Letโ€™s say youโ€™re working in a factory inspection system where images are captured every millisecond to spot defects in assembly lines ๐Ÿ”ง. Use the natsort package to keep your files in the right order:

from natsort import natsorted

list_files = [
    f for f in os.listdir("../../week_3/readings/assets/figures") if f.endswith(".jpg")
]
list_files
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[6], line 1
----> 1 from natsort import natsorted
      3 list_files = [
      4     f for f in os.listdir("../../week_3/readings/assets/figures") if f.endswith(".jpg")
      5 ]
      6 list_files

ModuleNotFoundError: No module named 'natsort'
list_files = natsorted(list_files)
list_files
['Mario_the_Magnificent.jpg',
 'RubberDuck.jpg',
 'axol-regrowth.jpg',
 'axolotl_1.jpg',
 'dust-image.jpg',
 'filp-game-1.jpg',
 'filp-game.jpg',
 'kennedy.jpg',
 'mario-negative-world.jpg',
 'nala.jpg',
 'pressure-vessel-chemistry.jpg',
 'scarface_duck.jpg',
 'tacoma-bridge.jpg',
 'tipu-tiger.jpg']
image_list = []
for filename in list_files:
    image_list.append(
        ski.io.imread(os.path.join("../../week_3/readings/assets/figures", filename))
    )

# This is one of the many ways to make subplots
f, axes = plt.subplots(nrows=3, ncols=len(image_list) // 3 + 1, figsize=(20, 5))

# subplots returns the figure and an array of axes
# we use `axes.ravel()` to turn these into a list
axes = axes.ravel()

# turns all of the axis off
for ax in axes:
    ax.axis("off")

# plots all of the images in the collection
for i, image in enumerate(image_list):
    axes[i].imshow(image, cmap="gray")
    axes[i].set_title(os.path.basename(list_files[i]))

# This cleans the layout of the image
plt.tight_layout()
../../_images/425872cc1f37e00815a11309c4e9bed34777b4e3455343d89c9837de5e7c837e.png

๐ŸŽ‰ Now you have all your images loaded in order, ready for further analysis! Imagine using this in a drone ๐Ÿ›ธ that scans solar panels for cracks ๐ŸŒž๐Ÿ”.

Ready to Dive Deeper? ๐ŸŒŠ#

Youโ€™ve taken your first steps into the pixel-perfect world of scikit-image. Whether youโ€™re improving product quality ๐Ÿญ, analyzing biological samples ๐Ÿ”ฌ, or making art out of data ๐ŸŽจ, this toolkit will become your go-to companion.

Let the image adventures begin! ๐ŸŒŸ๐Ÿ“ธ