Difference between revisions of "Pioneer21/Lecture03"
Jump to navigation
Jump to search
(Created page with "This page serves as a supplement to the third group meeting of the Summer 2021 Pioneer program for image processing. == Python Requirements == The programs from this lecture...") |
(→scikit-image commands) |
||
Line 19: | Line 19: | ||
* <code>DATA = ski.data.NAME()</code> where DATA is a variable name you give to store the information from a built-in image and NAME is the name of a built-in image from scikit-image; the built-in images can be found in the Data section of the [https://scikit-image.org/docs/stable/auto_examples/index.html General Examples] page. | * <code>DATA = ski.data.NAME()</code> where DATA is a variable name you give to store the information from a built-in image and NAME is the name of a built-in image from scikit-image; the built-in images can be found in the Data section of the [https://scikit-image.org/docs/stable/auto_examples/index.html General Examples] page. | ||
− | '''Starting on July 26, 2021, we will be using <code>AXIS.imshow(DATA)</code> instead of <code>ski.io.imshow(DATA)</code> to display an image.''' | + | '''Starting on July 26, 2021, we will be using <code>AXIS.imshow(DATA)</code> instead of <code>ski.io.imshow(DATA)</code> to display an image. It looks like ski.io.imshow is going away.''' |
Depending on the nature of an image, its DATA variable will be different shapes and contain different data types. Here is how scikit-image and imshow() interpret things: | Depending on the nature of an image, its DATA variable will be different shapes and contain different data types. Here is how scikit-image and imshow() interpret things: | ||
Line 56: | Line 56: | ||
grayimage = ski.data.coins() | grayimage = ski.data.coins() | ||
ax.imshow(grayimage, cmap=plt.cm.gray) | ax.imshow(grayimage, cmap=plt.cm.gray) | ||
− | ax.set_axis_off()</syntaxhighlight>*'''Note 1''' - the addition of the colormap is required for using the axis version of imshow versus the scikit-image version of imshow. *'''Note 2''' - by default, imshow will | + | ax.set_axis_off()</syntaxhighlight> |
+ | :::*'''Note 1''' - the addition of the colormap is required for using the axis version of imshow versus the scikit-image version of imshow. | ||
+ | :::*'''Note 2''' - by default, imshow will stretch the range of integers to cover the whole map. If you want to specifically map to a certain range - for instance, have 0 as pure black and 255 as pure white, you need to add <code>vmin</code> and <code>vmax</code> keyword arguments to explicitly give the values that map to the low and high end of the volormap. For instance:<syntaxhighlight lang=python> | ||
fig, ax = plt.subplots(num=1, clear=True) | fig, ax = plt.subplots(num=1, clear=True) | ||
grayimage = ski.data.coins() | grayimage = ski.data.coins() | ||
− | ax.imshow(grayimage, cmap=plt.cm.gray, vmin=0, vmax=255) | + | ax.imshow(grayimage, cmap=plt.cm.gray, vmin=0, vmax=255))</syntaxhighlight> will produce the exact same image that ski.io.imshow(grayimage) would have produced. |
− | + | ||
+ | ** An array of floating-point values will also produce an image with values mapped to the default Viridis (purple - green - blue - yellow) colormap. You can add some keyword arguments to the command to get shades of gray as above. Assume that the code:<syntaxhighlight lang=python> | ||
+ | x, y = np.meshgrid(np.linspace(-2, 2, 201), np.linspace(-1, 1, 101)) | ||
+ | z = np.exp(-np.sqrt(x**2+y**2))*np.cos(2*np.pi*x)*np.sin(4*np.pi*y) | ||
+ | </syntaxhighlight> has been run already for each of the cases below. | ||
+ | *** Default case with floats<syntaxhighlight lang=python> | ||
+ | fig, ax = plt.subplots(num=1, clear=True) | ||
+ | ax.imshow(z)</syntaxhighlight> | ||
+ | *** Gray with floats<syntaxhighlight lang=python> | ||
+ | fig, ax = plt.subplots(num=1, clear=True) | ||
+ | grayimage = ski.data.coins() | ||
+ | ax.imshow(grayimage, cmap=plt.cm.gray)</syntaxhighlight> | ||
+ | *** Gray and no axis ticks with floats<syntaxhighlight lang=python> | ||
+ | fig, ax = plt.subplots(num=1, clear=True) | ||
+ | grayimage = ski.data.coins() | ||
+ | ax.imshow(grayimage, cmap=plt.cm.gray) | ||
+ | ax.set_axis_off()</syntaxhighlight> | ||
+ | :::*'''Note''' - once again, by default, imshow will stretch the range of floats to cover the whole map. If you want to specifically map to a certain range - for instance, have 0 as pure black and 0.2 as pure white, you need to add <code>vmin</code> and <code>vmax</code> keyword arguments to explicitly give the values that map to the low and high end of the colormap. Anything below vmin will be mapped to the low color and anything above vmax will be mapped to the high. For instance:<syntaxhighlight lang=python> | ||
+ | fig, ax = plt.subplots(num=1, clear=True) | ||
+ | ax.imshow(z, vmin=0, vmax=255))</syntaxhighlight> will produce an image with yellow rounded rectangles on a purple background with greenish-blue edges the further you get away from the center. The large all-yellow rectangles contain many values that are above 0.2 and the purple parts are all the negative parts of the array. | ||
<!-- | <!-- |
Revision as of 01:27, 27 July 2021
This page serves as a supplement to the third group meeting of the Summer 2021 Pioneer program for image processing.
Python Requirements
The programs from this lecture will require several modules:
- NumPy - https://numpy.org/
- Matplotlib - https://matplotlib.org/ (and specifically matplotlib.pyplot)
- scikit-image - https://scikit-image.org/
- SciPy - https://www.scipy.org/ (and specifically scipy.signal)
The start of any script we use in this lecture will be:
import numpy as np
import matplotlib.pyplot as plt
import skimage as ski
import scipy.signal as sig
scikit-image commands
The main command for this session is:
DATA = ski.data.NAME()
where DATA is a variable name you give to store the information from a built-in image and NAME is the name of a built-in image from scikit-image; the built-in images can be found in the Data section of the General Examples page.
Starting on July 26, 2021, we will be using AXIS.imshow(DATA)
instead of ski.io.imshow(DATA)
to display an image. It looks like ski.io.imshow is going away.
Depending on the nature of an image, its DATA variable will be different shapes and contain different data types. Here is how scikit-image and imshow() interpret things:
- A 2D-array will either be black and white, grayscale, or it will be mapped to a colormap
- A 3D-array will be interpreted as an RGB-based color image
- For a 2D-array, the following data types and sizes might be useful:
- An array of boolean (True or False) values or integer values with only integers 0 or 1 will produce an image with values at the extreme edges of the colormap. By default in Python with matplotlib this is purple and gold. You can add some keyword arguments to the command to get black and white as follows:
- Default case with booleans
fig, ax = plt.subplots(num=1, clear=True) bwimage = ski.data.binary_blobs() ax.imshow(bwimage)
- Black and white with booleans
fig, ax = plt.subplots(num=1, clear=True) bwimage = ski.data.binary_blobs() ax.imshow(bwimage, cmap=plt.cm.gray)
- Black and white and no axis ticks with booleansFor each of the above, you can replace the array of booleans with an array of integers 0 or 1 by replacing the
fig, ax = plt.subplots(num=1, clear=True) bwimage = ski.data.binary_blobs() ax.imshow(bwimage, cmap=plt.cm.gray) ax.set_axis_off()
bwimage
line with:orbwimage = np.random.choice([0,1], (50,100))
bwimage = np.random.randint(0, 2, (50, 100))
- Default case with booleans
- An array of integer values will produce an image with values mapped to the default Viridis (purple - green - blue - yellow) colormap. You can add some keyword arguments to the command to get shades of gray as above:
- Default case with integers
fig, ax = plt.subplots(num=1, clear=True) grayimage = ski.data.coins() ax.imshow(grayimage)
- Gray with integers
fig, ax = plt.subplots(num=1, clear=True) grayimage = ski.data.coins() ax.imshow(grayimage, cmap=plt.cm.gray)
- Gray and no axis ticks with integers
fig, ax = plt.subplots(num=1, clear=True) grayimage = ski.data.coins() ax.imshow(grayimage, cmap=plt.cm.gray) ax.set_axis_off()
- Default case with integers
- An array of boolean (True or False) values or integer values with only integers 0 or 1 will produce an image with values at the extreme edges of the colormap. By default in Python with matplotlib this is purple and gold. You can add some keyword arguments to the command to get black and white as follows:
- Note 1 - the addition of the colormap is required for using the axis version of imshow versus the scikit-image version of imshow.
- Note 2 - by default, imshow will stretch the range of integers to cover the whole map. If you want to specifically map to a certain range - for instance, have 0 as pure black and 255 as pure white, you need to add
vmin
andvmax
keyword arguments to explicitly give the values that map to the low and high end of the volormap. For instance:will produce the exact same image that ski.io.imshow(grayimage) would have produced.fig, ax = plt.subplots(num=1, clear=True) grayimage = ski.data.coins() ax.imshow(grayimage, cmap=plt.cm.gray, vmin=0, vmax=255))
- An array of floating-point values will also produce an image with values mapped to the default Viridis (purple - green - blue - yellow) colormap. You can add some keyword arguments to the command to get shades of gray as above. Assume that the code:has been run already for each of the cases below.
x, y = np.meshgrid(np.linspace(-2, 2, 201), np.linspace(-1, 1, 101)) z = np.exp(-np.sqrt(x**2+y**2))*np.cos(2*np.pi*x)*np.sin(4*np.pi*y)
- Default case with floats
fig, ax = plt.subplots(num=1, clear=True) ax.imshow(z)
- Gray with floats
fig, ax = plt.subplots(num=1, clear=True) grayimage = ski.data.coins() ax.imshow(grayimage, cmap=plt.cm.gray)
- Gray and no axis ticks with floats
fig, ax = plt.subplots(num=1, clear=True) grayimage = ski.data.coins() ax.imshow(grayimage, cmap=plt.cm.gray) ax.set_axis_off()
- Default case with floats
- An array of floating-point values will also produce an image with values mapped to the default Viridis (purple - green - blue - yellow) colormap. You can add some keyword arguments to the command to get shades of gray as above. Assume that the code:
- Note - once again, by default, imshow will stretch the range of floats to cover the whole map. If you want to specifically map to a certain range - for instance, have 0 as pure black and 0.2 as pure white, you need to add
vmin
andvmax
keyword arguments to explicitly give the values that map to the low and high end of the colormap. Anything below vmin will be mapped to the low color and anything above vmax will be mapped to the high. For instance:will produce an image with yellow rounded rectangles on a purple background with greenish-blue edges the further you get away from the center. The large all-yellow rectangles contain many values that are above 0.2 and the purple parts are all the negative parts of the array.fig, ax = plt.subplots(num=1, clear=True) ax.imshow(z, vmin=0, vmax=255))
- Note - once again, by default, imshow will stretch the range of floats to cover the whole map. If you want to specifically map to a certain range - for instance, have 0 as pure black and 0.2 as pure white, you need to add