Skip to content

Removing calls to skimage.viewer and switching to "lazy importing" module import #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 12, 2021
Merged
53 changes: 14 additions & 39 deletions _episodes/03-skimage-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,11 @@ named `image`.
Next, we will do something with the image:

~~~
import skimage.viewer

# display image
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Once we have the image in the program, we next import the `viewer` module of skimage
(`skimage.viewer`) and display it using `skimage.viewer.ImageViewer()`, which
returns a `ImageViewer` object we store in the `viewer` variable.
We then call `viewer.show()` in order to display the image.
Once we have the image in the program, we next call `skimage.io.imshow()` in order to display the image.

Next, we will save the image in another format:

Expand Down Expand Up @@ -259,14 +252,12 @@ We will start by reading the image and displaying it.
"""
import sys
import skimage.io
import skimage.viewer

# read input image, based on filename parameter
image = skimage.io.imread(fname=sys.argv[1])

# display original image
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -304,8 +295,7 @@ Now we can threshold the image and display the result.
image[image < 128] = 0

# display modified image
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -347,21 +337,18 @@ extraneous background detail has been removed.
> > """
> > import sys
> > import skimage.io
> > import skimage.viewer
> >
> > # read input image, based on filename parameter
> > image = skimage.io.imread(fname=sys.argv[1])
> >
> > # display original image
> > viewer = skimage.viewer.ImageViewer(image)
> > viewer.view()
> > skimage.io.imshow(img)
> >
> > # change high intensity pixels to gray
> > image[image > 200] = 64
> >
> > # display modified image
> > viewer = skimage.viewer.ImageViewer(image)
> > viewer.view()
> > skimage.io.imshow(img)
> > ~~~
> > {: .python}
> {: .solution}
Expand Down Expand Up @@ -389,20 +376,17 @@ because using floating point numbers is numerically more stable.
"""
import sys
import skimage.io
import skimage.viewer
import skimage.color

# read input image, based on filename parameter
image = skimage.io.imread(fname=sys.argv[1])

# display original image
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)

# convert to grayscale and display
gray_image = skimage.color.rgb2gray(image)
viewer = skimage.viewer.ImageViewer(gray_image)
viewer.show()
skimage.io.imshow(gray_image)
~~~
{: .python}

Expand All @@ -417,15 +401,13 @@ We can also load color images as grayscale directly by passing the argument `as_
"""
import sys
import skimage.io
import skimage.viewer
import skimage.color

# read input image, based on filename parameter
image = skimage.io.imread(fname=sys.argv[1], as_gray=True)

# display grayscale image
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -472,12 +454,10 @@ A program to create the subimage would start by loading the image:
* NumPy array slicing.
"""
import skimage.io
import skimage.viewer

# load and display original image
image = skimage.io.imread(fname="board.jpg")
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand All @@ -487,8 +467,7 @@ 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, :]
viewer = skimage.viewer.ImageViewer(clip)
viewer.show()
skimage.io.imshow(clip)
skimage.io.imsave(fname="clip.tif", arr=clip)
~~~
{: .python}
Expand All @@ -499,8 +478,7 @@ 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
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -535,18 +513,15 @@ the program:
> > * roots in an existing image.
> > """
> > import skimage.io
> > import skimage.viewer
> >
> > # load and display original image
> > image = skimage.io.imread(fname="roots.jpg")
> > viewer = skimage.viewer.ImageViewer(image)
> > viewer.show()
> > skimage.io.imshow(image)
> >
> > # extract, display, and save sub-image
> > # WRITE YOUR CODE TO SELECT THE SUBIMAGE NAME clip HERE:
> > clip = image[0:1999, 1410:2765, :]
> > viewer = skimage.viewer.ImageViewer(clip)
> > viewer.show()
> > skimage.io.imshow(clip)
> >
> >
> > # WRITE YOUR CODE TO SAVE clip HERE
Expand Down
47 changes: 20 additions & 27 deletions _episodes/04-drawing.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ image. (Note that the display portion is used here for pedagogical purposes; it
* Python program to use skimage drawing tools to create a mask.
*
"""
import skimage
from skimage.viewer import ImageViewer
import skimage.io
import skimage.draw
import numpy as np

# Load and display the original image
image = skimage.io.imread("maize-roots.tif")
viewer = ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

As before, we first import `skimage`. We also import the NumPy library, and give
it an alias of `np`. NumPy is necessary when we create the initial mask image,
and the alias saves us a little typing. Then, we load and display the initial
As before, we first import the `io`submodule of `skimage` (`skimage.io`). This time, we will also import the `draw` submodule. We also import the NumPy library, and give it an alias of `np`. NumPy is necessary when we create the initial mask image, and the alias saves us a little typing. Then, we load and display the initial
image in the same way we have done before.

NumPy allows indexing of images/arrays with "boolean" arrays of the same size.
Expand Down Expand Up @@ -142,8 +139,7 @@ The final section of the program displays the mask we just created:

~~~
# Display constructed mask
viewer = ImageViewer(mask)
viewer.show()
skimage.io.imshow(mask)
~~~
{: .python}

Expand Down Expand Up @@ -190,8 +186,8 @@ Here is what our constructed mask looks like:
> > """
> > import random
> > import numpy as np
> > import skimage
> > from skimage.viewer import ImageViewer
> > import skimage.draw
> > import skimage.io
> >
> > # create the black canvas
> > image = np.zeros(shape=(600, 800, 3), dtype="uint8")
Expand All @@ -200,9 +196,9 @@ Here is what our constructed mask looks like:
> > for i in range(15):
> > x = random.random()
> > if x < 0.33:
> > rr, cc = skimage.draw.circle(
> > rr, cc = skimage.draw.disk((
> > random.randrange(600),
> > random.randrange(800),
> > random.randrange(800)),
> > radius=50,
> > shape=image.shape[0:2],
> > )
Expand All @@ -226,8 +222,7 @@ Here is what our constructed mask looks like:
> > image[rr, cc] = color
> >
> > # display the results
> > viewer = ImageViewer(image)
> > viewer.show()
> > skimage.io.imshow(image)
> > ~~~
> > {: .python}
> {: .solution}
Expand Down Expand Up @@ -266,8 +261,8 @@ original image and create the mask in the same way as before:
*
"""
import numpy as np
import skimage
from skimage.viewer import ImageViewer
import skimage.io
import skimage.draw

# Load the original image
image = skimage.io.imread("maize-roots.tif")
Expand All @@ -293,8 +288,7 @@ image[mask] = 0
Then, we display the masked image.

~~~
viewer = ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -330,8 +324,8 @@ The resulting masked image should look like this:
> > *
> > """
> > import numpy as np
> > import skimage
> > from skimage.viewer import ImageViewer
> > import skimage.io
> > import skimage.draw
> >
> > # Load the original image
> > image = skimage.io.imread("./fig/03-remote-control.jpg")
Expand All @@ -345,8 +339,7 @@ The resulting masked image should look like this:
> >
> > # Apply the mask and display the result
> > image[mask] = 0
> > viewer = ImageViewer(image)
> > viewer.show()
> > skimage.io.imshow(image)
> > ~~~
> > {: .python}
> {: .solution}
Expand Down Expand Up @@ -388,8 +381,8 @@ The resulting masked image should look like this:
> > * in a standardized scanned 96-well plate image.
> > """
> > import numpy as np
> > import skimage
> > from skimage.viewer import ImageViewer
> > import skimage.io
> > import skimage.draw
> > import sys
> >
> > # read in original image
Expand Down Expand Up @@ -449,8 +442,8 @@ The resulting masked image should look like this:
> > * using a file with well center location.
> > """
> > import numpy as np
> > import skimage
> > from skimage.viewer import ImageViewer
> > import skimage.io
> > import skimage.draw
> > import sys
> >
> > # read in original image
Expand Down
21 changes: 6 additions & 15 deletions _episodes/05-creating-histograms.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ import sys
import numpy as np
import skimage.color
import skimage.io
import skimage.viewer
from matplotlib import pyplot as plt

# read image, based on command line filename argument;
# read the image as grayscale from the outset
image = skimage.io.imread(fname=sys.argv[1], as_gray=True)

# display the image
viewer = skimage.viewer.ImageViewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -195,16 +193,14 @@ the program produces this histogram:
> > import numpy as np
> > import skimage.draw
> > import skimage.io
> > import skimage.viewer
> > from matplotlib import pyplot as plt
> >
> > # read image, based on command line filename argument;
> > # read the image as grayscale from the outset
> > img = skimage.io.imread(fname=sys.argv[1], as_gray=True)
> >
> > # display the image
> > viewer = skimage.viewer.ImageViewer(img)
> > viewer.show()
> > skimage.io.imshow(img)
> >
> > # create mask here, using np.zeros() and skimage.draw.rectangle()
> > mask = np.zeros(shape=img.shape, dtype="bool")
Expand Down Expand Up @@ -249,16 +245,14 @@ color histograms starts in a familiar way:
"""
import sys
import skimage.io
import skimage.viewer
from matplotlib import pyplot as plt

# read original image, in full color, based on command
# line argument
image = skimage.io.imread(fname=sys.argv[1])

# display the image
viewer = skimage.viewer.Viewer(image)
viewer.show()
skimage.io.imshow(image)
~~~
{: .python}

Expand Down Expand Up @@ -413,7 +407,6 @@ Finally we label our axes and display the histogram, shown here:
> > """
> > import sys
> > import skimage.io
> > import skimage.viewer
> > import skimage.draw
> > import numpy as np
> > from matplotlib import pyplot as plt
Expand All @@ -423,13 +416,12 @@ Finally we label our axes and display the histogram, shown here:
> > image = skimage.io.imread(fname=sys.argv[1])
> >
> > # display the original image
> > viewer = skimage.viewer.ImageViewer(image)
> > viewer.show()
> > skimage.io.imshow(image)
> >
> > # create a circular mask to select the 7th well in the first row
> > # WRITE YOUR CODE HERE
> > mask = np.zeros(shape=image.shape[0:2], dtype="bool")
> > circle = skimage.draw.circle(240, 1053, radius=49, shape=image.shape[:2])
> > circle = skimage.draw.disk((240, 1053), radius=49, shape=image.shape[:2])
> > mask[circle] = 1
> >
> > # just for display:
Expand All @@ -442,8 +434,7 @@ Finally we label our axes and display the histogram, shown here:
> > # create a new window and display maskedImg, to verify the
> > # validity of your mask
> > # WRITE YOUR CODE HERE
> > viewer = skimage.viewer.ImageViewer(masked_img)
> > viewer.show()
> > skimage.io.imshow(masked_img)
> >
> > # list to select colors of each channel line
> > colors = ("r", "g", "b")
Expand Down
Loading