Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions _episodes/02-image-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ natively (think of volumes, movies).
{: .callout}

~~~
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
eight = iio.imread(uri="data/eight.tif")
plt.imshow(eight)
~~~
{: .language-python}

Expand Down Expand Up @@ -229,8 +229,8 @@ and see the matrix by printing our image variable to the screen.


~~~
print(image.shape)
print(image)
print(eight.shape)
print(eight)
~~~
{: .language-python}

Expand Down
93 changes: 47 additions & 46 deletions _episodes/03-skimage-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Here are the first few lines:
*
"""
# read image
image = iio.imread(uri="data/chair.jpg")
chair = iio.imread(uri="data/chair.jpg")
~~~
{: .language-python}

Expand All @@ -73,7 +73,7 @@ Next, we will do something with the image:

~~~
fig, ax = plt.subplots()
plt.imshow(image)
plt.imshow(chair)
~~~
{: .language-python}

Expand All @@ -86,11 +86,11 @@ Now, we will save the image in another format:

~~~
# save a new version in .tif format
iio.imwrite(uri="data/chair.tif", image=image)
iio.imwrite(uri="data/chair.tif", image=chair)
~~~
{: .language-python}

The final statement in the program, `iio.imwrite(uri="data/chair.tif", image=image)`,
The final statement in the program, `iio.imwrite(uri="data/chair.tif", image=chair)`,
writes the image to a file named `chair.tif` in the `data/` directory.
The `imwrite()` function automatically determines the type of the file,
based on the file extension we provide.
Expand Down Expand Up @@ -150,19 +150,19 @@ In this case, the `.tif` extension causes the image to be saved as a TIFF.
>
> Add `import skimage.transform` and `import skimage.util` to your list of imports.
> Using the `chair.jpg` image located in the data folder,
> write a Python script to read your image into a variable named `image`.
> write a Python script to read your image into a variable named `chair`.
> Then, resize the image to 10 percent of its current size using these lines of code:
>
> ~~~
> new_shape = (image.shape[0] // 10, image.shape[1] // 10, image.shape[2])
> small = skimage.transform.resize(image=image, output_shape=new_shape)
> small = skimage.util.img_as_ubyte(small)
> new_shape = (chair.shape[0] // 10, chair.shape[1] // 10, chair.shape[2])
> resized_chair = skimage.transform.resize(image=chair, output_shape=new_shape)
> resized_chair = skimage.util.img_as_ubyte(resized_chair)
> ~~~
> {: .language-python}
>
> As it is used here,
> the parameters to the `skimage.transform.resize()` function are
> the image to transform, `image`,
> the image to transform, `chair`,
> the dimensions we want the new image to have, `new_shape`.
>
> > Note that the pixel values in the new image are an approximation of
Expand Down Expand Up @@ -207,26 +207,26 @@ In this case, the `.tif` extension causes the image to be saved as a TIFF.
> > """
> >
> > # read in image
> > image = iio.imread(uri="data/chair.jpg")
> > chair = iio.imread(uri="data/chair.jpg")
> >
> > # resize the image
> > new_shape = (image.shape[0] // 10, image.shape[1] // 10, image.shape[2])
> > small = skimage.transform.resize(image=image, output_shape=new_shape)
> > small = skimage.util.img_as_ubyte(small)
> > new_shape = (chair.shape[0] // 10, chair.shape[1] // 10, chair.shape[2])
> > resized_chair = skimage.transform.resize(image=chair, output_shape=new_shape)
> > resized_chair = skimage.util.img_as_ubyte(resized_chair)
> >
> > # write out image
> > iio.imwrite(uri="data/resized.jpg", image=small)
> > iio.imwrite(uri="data/resized_chair.jpg", image=resized_chair)
> >
> > # display images
> > fig, ax = plt.subplots()
> > plt.imshow(image)
> > plt.imshow(chair)
> > fig, ax = plt.subplots()
> > plt.imshow(small)
> > plt.imshow(resized_chair)
> > ~~~
> > {: .language-python}
> >
> > The script resizes the `data/chair.jpg` image by a factor of 10 in both dimensions,
> > saves the result to the `data/resized.jpg` file,
> > saves the result to the `data/resized_chair.jpg` file,
> > and displays original and resized for comparision.
> {: .solution}
{: .challenge}
Expand Down Expand Up @@ -269,12 +269,12 @@ When loading an image with `imageio`, in certain situations the image is stored
"""

# read input image
image = iio.imread(uri="data/maize-root-cluster.jpg")
image = np.array(image)
maize_roots = iio.imread(uri="data/maize-root-cluster.jpg")
maize_roots = np.array(maize_roots)

# display original image
fig, ax = plt.subplots()
plt.imshow(image)
plt.imshow(maize_roots)
~~~
{: .language-python}

Expand All @@ -283,15 +283,15 @@ Now we can threshold the image and display the result.

~~~
# keep only high-intensity pixels
image[image < 128] = 0
maize_roots[maize_roots < 128] = 0

# display modified image
fig, ax = plt.subplots()
plt.imshow(image)
plt.imshow(maize_roots)
~~~
{: .language-python}

The NumPy command to ignore all low-intensity pixels is `image[image < 128] = 0`.
The NumPy command to ignore all low-intensity pixels is `roots[roots < 128] = 0`.
Every pixel colour value in the whole 3-dimensional array with a value less
that 128 is set to zero.
In this case,
Expand Down Expand Up @@ -334,16 +334,16 @@ because using floating point numbers is numerically more stable.
"""

# read input image
image = iio.imread(uri="data/chair.jpg")
chair = iio.imread(uri="data/chair.jpg")

# display original image
fig, ax = plt.subplots()
plt.imshow(image)
plt.imshow(chair)

# convert to grayscale and display
gray_image = skimage.color.rgb2gray(image)
gray_chair = skimage.color.rgb2gray(chair)
fig, ax = plt.subplots()
plt.imshow(gray_image, cmap="gray")
plt.imshow(gray_chair, cmap="gray")
~~~
{: .language-python}

Expand All @@ -357,11 +357,11 @@ passing the argument `mode="L"` to `iio.imread()`.
"""

# read input image, based on filename parameter
image = iio.imread(uri="data/chair.jpg", mode="L")
gray_chair = iio.imread(uri="data/chair.jpg", mode="L")

# display grayscale image
fig, ax = plt.subplots()
plt.imshow(image, cmap="gray")
plt.imshow(gray_chair, cmap="gray")
~~~
{: .language-python}

Expand Down Expand Up @@ -401,23 +401,23 @@ pass `plugin="pillow"`. If the backend is not specified explicitly, `iio.imread(
> >
> > ~~~
> >
> > image = iio.imread(uri="data/sudoku.png", mode="L")
> > image = np.array(image)
> > sudoku = iio.imread(uri="data/sudoku.png")
> > ~~~
> > {: .language-python }
> >
> > Then change all bright pixel values greater than 192 to 192:
> >
> > ~~~
> > image[image > 192] = 192
> > sudoku = sudoku.copy()
> > sudoku[sudoku > 125] = 125
> > ~~~
> > {: .language-python }
> >
> > Finally, display the modified image. Note that we have to specify `vmin=0` and `vmax=255` as the range of the colorscale because it would otherwise automatically adjust to the new range 0-192.
> >
> > ~~~
> > fig, ax = plt.subplots()
> > plt.imshow(image, cmap="gray", vmin=0, vmax=255)
> > plt.imshow(sudoku, cmap="gray", vmin=0, vmax=1)
> > ~~~
> > {: .language-python}
> {: .solution}
Expand Down Expand Up @@ -493,9 +493,10 @@ A script to create the subimage would start by loading the image:
"""

# load and display original image
image = iio.imread(uri="data/board.jpg")
board = iio.imread(uri="data/board.jpg")
board = np.array(board)
fig, ax = plt.subplots()
plt.imshow(image)
plt.imshow(board)
~~~
{: .language-python}

Expand All @@ -504,21 +505,21 @@ create a new image with our selected area and then display the new image.

~~~
# extract, display, and save sub-image
clip = image[60:151, 135:481, :]
clipped_board = board[60:151, 135:481, :]
fig, ax = plt.subplots()
plt.imshow(clip)
iio.imwrite(uri="data/clip.tif", image=clip)
plt.imshow(clipped_board)
iio.imwrite(uri="data/clipped_board.tif", image=clipped_board)
~~~
{: .language-python}

We can also change the values in an image, as shown next.

~~~
# replace clipped area with sampled color
color = image[330, 90]
image[60:151, 135:481] = color
color = board[330, 90]
board[60:151, 135:481] = color
fig, ax = plt.subplots()
plt.imshow(image)
plt.imshow(board)
~~~
{: .language-python}

Expand Down Expand Up @@ -555,19 +556,19 @@ as shown in the final image produced by the program:
> > """
> >
> > # load and display original image
> > image = iio.imread(uri="data/maize-root-cluster.jpg")
> > maize_roots = iio.imread(uri="data/maize-root-cluster.jpg")
> > fig, ax = plt.subplots()
> > plt.imshow(image)
> > plt.imshow(maize_roots)
> >
> > # extract, display, and save sub-image
> > # WRITE YOUR CODE TO SELECT THE SUBIMAGE NAME clip HERE:
> > clip = image[0:400, 275:550, :]
> > clipped_maize = maize_roots[0:400, 275:550, :]
> > fig, ax = plt.subplots()
> > plt.imshow(clip)
> > plt.imshow(clipped_maize)
> >
> >
> > # WRITE YOUR CODE TO SAVE clip HERE
> > iio.imwrite(uri="data/clip.jpg", image=clip)
> > iio.imwrite(uri="data/clipped_maize.jpg", image=clipped_maize)
> > ~~~
> > {: .language-python}
> {: .solution}
Expand Down
Loading