Image Processing Example

This notebook does....


In [56]:
%matplotlib inline
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

import PIL.Image as Image
import skimage.io as skio
import skimage as ski
import matplotlib.cm as cm
from scipy import ndimage
from skimage import exposure
from skimage.morphology import disk
from skimage.filter import rank
from skimage.filter.rank import mean_bilateral


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

In [57]:
#Importing images from dropbox
from dropbox_path import dropbox_path
dropbox_path()
file_path = dropbox_path() + '\MicroCT\\2Dimages\S_20MMFOV_HR10UM_SF4_SEG_TOP.tif'

In [58]:
#Display grayscale image
im = skio.imread(file_path)
plt.imshow(im, cmap = cm.Greys_r)
plt.colorbar()
print im.shape


(906L, 906L)

In [59]:
#Create histogram
im_flatten = im.flatten()
num_bins = 255
# the histogram of the data
n, bins, patches = plt.hist(im_flatten, num_bins)



In [60]:
# Global Histogram Equalization
img_rescale = exposure.equalize_hist(im)

In [61]:
plt.imshow(img_rescale, cmap = cm.Greys_r)
plt.colorbar()


Out[61]:
<matplotlib.colorbar.Colorbar instance at 0x0000000018A24488>

In [62]:
im_flatten_rescale = img_rescale.flatten()

num_bins = 255
# the histogram of the equalization
n, bins, patches = plt.hist(im_flatten_rescale, num_bins)



In [63]:
#Local histogram equalization
selem = disk(30)
img_eq = rank.equalize(im, selem=selem)

In [64]:
plt.imshow(img_eq, cmap = cm.Greys_r)
plt.colorbar()


Out[64]:
<matplotlib.colorbar.Colorbar instance at 0x0000000058C0E748>

In [65]:
im_flatten_eq = img_eq.flatten()

num_bins = 255
# the histogram of the data
n, bins, patches = plt.hist(im_flatten_eq, num_bins)



In [66]:
#Plotting image, global equalization and local equalization
fig = plt.figure()
plt.imshow(im, cmap = cm.Greys_r)
fig = plt.figure()
plt.imshow(img_rescale,  cmap = cm.Greys_r)
fig = plt.figure()
plt.imshow(img_eq,  cmap = cm.Greys_r)


Out[66]:
<matplotlib.image.AxesImage at 0x2e8b1a90>

In [67]:
# Sectioning the image based on grayscale values
for ii in range(10):
    img_seg = np.where(img_eq > 25 * (ii + 1), 1, 0)
    fig = plt.figure()
    plt.imshow(img_seg, cmap = cm.Greys_r)
    plt.colorbar()



In [68]:
#Edge detection
from skimage.filter import canny
edges = canny(im/255.)
plt.imshow(edges, cmap = cm.Greys_r)


Out[68]:
<matplotlib.image.AxesImage at 0x5994d320>

In [69]:
#Feature identification
segmented_image = ndimage.binary_fill_holes(edges)
plt.imshow(segmented_image, cmap = cm.Greys_r)


Out[69]:
<matplotlib.image.AxesImage at 0x278fbe80>

In [70]:
from skimage.filter import sobel
elevation_map = sobel(im)

In [70]: