......................................... Jan 19, 2017
Mahotas: Python Computer Vision Library •
https://pypi.python.org/pypi/mahotas •
http://mahotas.readthedocs.io/en/latest/ins tall.html
Importing mahotas with the mh abbreviation >>> import
mahotas as mh
Mahotas: first program import mahotas as mh import numpy as np from
matplotlib import pyplot as plt
image = mh.demos.load('lena') plt.imshow(image) plt.show()
Mahotas: Thresholding threshold = mh.thresholding.otsu(image)
print('Otsu threshold is {}.'.format(thresh))
imgray = (image > thresh) plt.imshow(imgray) plt.show()
Mahotas: Gaussian blurring im16 = mh.gaussian_filter(image, 16)
plt.imshow(im16) plt.show()
Scikit-image: sample images data from skimage import data, io
image = data.coins() io.imshow(image) io.show
Scikit-image: show image information >>> type(image)
<type 'numpy.ndarray'> >>> image.__class__ <type
'numpy.ndarray'> >>> image.dtype dtype('uint8')
>>> image.shape (303, 384)
Scikit-image: imread 'lena' image >>> from skimage import
io >>> img = io.imread('lena.png') >>> img.shape
(512, 512, 3)
numpy.ndarray >>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b = [[10, 11, 12], [13, 14, 15], [16, 17, 18]]
>>> c = [[2, 2, 2], [3, 3, 3], [4, 4, 4]] >>>
type(a) <type 'list'> >>> x=a+b+c [[1, 2, 3], [4, 5,
6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [2, 2, 2],
[3, 3, 3], [4, 4, 4]]
numpy.ndarray >>> x = np.array(a)+np.array(b)+np.array(c)
>>> type(x) <type 'numpy.ndarray'> >>> x
array([[13, 15, 17], [20, 22, 24], [27, 29, 31]])
>>> x.shape # ????
numpy.ndarray: attributes • ndarray.flags – Information about the
memory layout of the array • ndarray.shape – Tuple of array
dimensions • ndarray.strides – Tuple of bytes to step in each
dimension when traversing an array
• ndarray.ndim – Number of array dimensions. • ndarray.data –
Python buffer object pointing to the start of the array’s
data
• ndarray.size – Number of elements in the array • ndarray.itemsize
– Length of one array element in bytes • ndaaray.nbytes – Total
bytes consumed by the elements of the array
• ndarray.base – Base object if memory is from some other object
https://docs.scipy.org/doc/numpy/refere
nce/arrays.ndarray.html
Basic algorithm for converting RGB image to Grayscale image • The
average method simply averages the values: (R+G+B)/3
• The luminosity method is more sophisticated version of the
average method. – It also averages the values, but it forms a
weighted average to account for human perception.
• The formula for luminosity is 0.21R + 0.72G + 0.07B
Basic algorithm for converting RGB image to Grayscale image
RGB2Gray = (0.21R + 0.72G + 0.07B)
from skimage import io from matplotlib import pyplot as plt img =
io.imread('lena.png') imgGray = (0.21*img[:,:,0]) +
(0.72*img[:,:,1]) \ + (0.07*img[:,:,2]) io.imshow(imgGray)
plt.imshow(imgGray) io.show() plt.gray() plt.show()
Basic algorithm for converting RGB image to Grayscale image
Histogram import numpy as np import matplotlib.pyplot as plt from
skimage import data
coins = data.coins() hist = np.histogram(coins, bins=np.arange(0,
256))
fig, axes = plt.subplots(1, 2, figsize=(8, 3))
axes[0].imshow(coins, cmap=plt.cm.gray, interpolation='nearest')
axes[0].axis('off') axes[1].plot(hist[1][:-1], hist[0], lw=2)
axes[1].set_title('histogram of grey values') plt.show()
Converting grayscale to black and white image • I(x,y) > thresh;
I(x,y) = 1, else 0
# manual adjust threshold value thresh = 30
plt.imshow(imgGray>thresh) plt.show()
# or io.imshow(imgGray>thresh) io.show()
Converting grayscale to black and white image plt io
Show multiple image in one figure from skimage import data from
matplotlib import pyplot as plt
img1 = data.page() img2 = data.camera()
fig, axes = plt.subplots(ncols=2) ax = axes.ravel()
plt.gray()
ax[0].imshow(img1) ax[0].set_title('page') ax[1].imshow(img2)
ax[1].set_title('camera')
for a in ax: a.axis('off')
plt.show()
Show multiple image in one figure from skimage import data from
matplotlib import pyplot as plt
img1 = data.page() img2 = data.camera()
fig, axes = plt.subplots(nrows=2) ax = axes.ravel()
plt.gray()
ax[0].imshow(img1) ax[0].set_title('page') ax[1].imshow(img1)
ax[1].set_title('page')
for a in ax: a.axis('off')
plt.show() How can we show multiple “Figures” at the same
time???
Scikit-image: converting an RGB to Grayscale image from skimage
import color, data, io # import skimage.color as color img =
data.astronaut() imgGray = color.rgb2gray(img) # import
skimage.color # imgGray = skimage.color.rgb2gray(img)
io.imshow(imgGray) io.show()
Scikit-image: converting an RGB to Grayscale image from skimage
import data, io import skimage.color as color img =
data.astronaut() imgGray = color.rgb2gray(img) io.imshow(imgGray)
io.show()
Scikit-image: converting grayscale to black and white image from
skimage import data, io import skimage.color as color import numpy
as np
img = data.astronaut() imgGray = color.rgb2gray(img) thresh =
np.mean(imgGray) imgBW = imgGray>thresh io.imshow(imgBW)
io.show()
??? ???
Scikit-image: converting grayscale to black and white image from
skimage import data, io, filters import numpy as np
img = data.camera() thresh = filters.threshold_otsu(img) imgBW =
img>thresh io.imshow(imgBW) io.show()
??? np.mean() camera thresh ???
Scikit-image: different threshold techniques • Otsu • Niblack •
Sauvola • Triangle • Mean • Minimum • Adaptive
Scikit-image: different threshold techniques from matplotlib import
pyplot as plt from skimage import data, filters
img = data.page() global_thresh = \ filters.threshold_otsu(img)
img_global = img > global_thresh
img_adaptive = \ filters.threshold_adaptive(img, \ block_size=35,
offset=10)
fig, axes = plt.subplots(ncols=3) ax = axes.ravel()
plt.gray()
ax[0].imshow(img) ax[0].set_title('page') ax[1].imshow(img_global)
ax[1].set_title('global threshold') ax[2].imshow(img_adaptive)
ax[2].set_title('adaptive threshold')
for a in ax: a.axis('off')
plt.show()
Scikit-image: different threshold techniques
Python: Viewing all defined variables • dir() - will give you the
list of in scope variables
• globals() - will give you a dictionary of global variables
• locals() - will give you a dictionary of local variables
Python: How to clear the screen? import os os.system('clear') #on
linux # or os.system('cls') # on windows
Python: Clear variable >>> a = 5 + 6 >>> del a
>>> a Traceback (most recent call last): File
"<stdin>", line 1, in <module> NameError: name 'a' is
not defined
References •
https://www.johndcook.com/blog/2009/08/24/algorithms-conver
t-color-grayscale/ •
https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.ht
ml • http://mahotas.readthedocs.io/en/latest/index.html