In [1]:
from IPython.display import Image
Image(filename='Figs_ipandcg_2014/2014_Session_2.png')
Out[1]:

In this session we will learn how to:

Remember to import all needed modules at the begening of your session

All imports should be done once. Please note, whenever you reset session (%reset) you heve to reload all packages and modules.

In [5]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.misc as misc
import Image #(Python Imaging Library - PIL)

Load and save image from/to disc file

With the use function imread (matplotlib.pyplot package) load brain.bmp image and store it into img variable. Use the path on your local drive (e.g. "D:_2014.bmp"). Display image with imshow() function. All example images are downloadable from the Image Processing and Computer Graphics webpage (2014 Examples - Images).

In [24]:
img = plt.imread('images_2014/brain.bmp')
plt.imshow(img, cmap=plt.cm.gray, vmin=10, vmax=100)
Out[24]:
<matplotlib.image.AxesImage at 0x875a208>

Check and explain the meaning of "vmin" and "vmax" parameters. Use help() function or matplotlib webpage. Observe the image for several different values of these parameters. Make an experiment and set "vmin" and "vmax" to obtain the image that displays only brain tissue (as showed belowe) whthout skull:

In [27]:
from IPython.display import Image
Image(filename="Figs_ipandcg_2014/brain_windowed.png")
Out[27]:

Save image to the disc file. Use various file format: .bmp, .jpg, .png, .pdf. You can find helpful functions in the modules (scipy.misc, matplotlib.pyplot):

In [7]:
plt.imsave('brain_plt_gray.png',img, cmap=plt.cm.gray)
plt.imsave('brain_plt_hot.png',img, cmap=plt.cm.hot)
plt.imsave('brain_plt_jet.png',img, cmap=plt.cm.jet)
plt.imsave('brain_plt_spectral.png',img, cmap=plt.cm.spectral)
misc.imsave('brain_misc.png', img)

Here is a list of file formats supperted by matplotlib.pyplot:

Please check what kind of image formats are supported by:

Convertion between PIL and Numpy objects

To convert between Pyton Imaging Library (PIL) and numpy.ndarray objects use functions:

In [12]:
im = Image.fromarray(img)
print 'old object',type(img)
print 'new object', type(im), im.mode, im.size, im.format
old object <type 'numpy.ndarray'>
new object <type 'instance'> L (512, 384) None

In [15]:
im2 = misc.toimage(img)
print 'old object',type(img)
print 'new object',type(im2), im2.mode, im2.size, im2.format
old object <type 'numpy.ndarray'>
new object <type 'instance'> L (512, 384) None

In [17]:
img2 = misc.fromimage(im2)
print 'old object',type(im2)
print 'new object',type(img2), img2.shape, img2.dtype
old object <type 'instance'>
new object <type 'numpy.ndarray'> (384L, 512L) uint8

In [18]:
img3 = np.array(im2)
print 'old object',type(im2)
print 'new object',type(img2),img3.shape, img3.dtype
old object <type 'instance'>
new object <type 'numpy.ndarray'> (384L, 512L) uint8

Save (Load) to (from) raw file

RAW files contain only raw data (pixel values). There is no meta information about size or data type. The user should remember shape of an array and bits per pixel (voxel). It can be helpful to save these information in the name of file.

To save numpy.array to file use numpy_array.tofile() method.

In [19]:
# Save numpy.array to file
name = 'brain_' + str(img.shape[0]) + '_' + str(img.shape[1]) +'_' + str(img.dtype) + '.raw'
img.tofile(name,format=str(img.dtype))

You can load raw data from file with the use of np.fromfile() method. This method loads all data into raw vector (height*width), so there is a need to reshape 1D vector to 2D array.

In [20]:
# Load and display
img4 = np.fromfile(name, dtype='uint8')
print 'shape of the loaded image is: %d' % (img4.shape)
img4.shape=(384,512)
print "after reshape: %d x %d " % (img4.shape)
imshow(img4, cmap=plt.cm.gray)
shape of the loaded image is: 196608
after reshape: 384 x 512 

Out[20]:
<matplotlib.image.AxesImage at 0x82fa860>

Basic statistical parameters of the image

Write the function that calculates a brightness and contrast of the input image according to the formula given in the lecture (by prof Strumiłło & prof. Strzelecki). What type of mathematical operation is responsible for change of brightness? What for the contrast? Calculate these parameters for the whole imge ("brain.bmp") and for some its parts (Retion Of Interests - ROI). In the next step modify brighness and contrast with the use of appropriate mathematical operation. How would you define these properties of the image?

  1. Explain the benefits of sending ndarray as an function argument.
  2. Explain each line of the function.
  3. Compare the functions based on matrix operaions (brightness_contrast(im)) and on for loops (br(im), co(im)).
In [22]:
def brightness_contrast(im):
    img = im.copy()
    img = img.astype(np.float)
    br = img.sum() / img.size
    j = zeros_like(img) + br
    c = np.sqrt( ((img-j)**2).sum()/img.size )
    return br, c
In [23]:
def br(im):
    img = im.copy()
    row, col = img.shape
    sum = 0
    for r in range(row):
        for c in range(col):
            sum += img[r,c]
    return float(sum)/img.size
    
def co(im, b):
    img = im.copy()
    img = im.astype(np.float)
    row, col = img.shape
    con = 0.0
    for r in range(row):
        for c in range(col):
            con += ((img[r,c] - b)**2)
            
    return np.sqrt(con/img.size)
            
            
In [39]:
# load the image and store it in varibale 'a'
a = plt.imread('./images_2014/brain.bmp')

print 'Brightness and contrast (matrix operations):', brightness_contrast(a)
bright = br(a)
print 'Brightness and contrast (for loops):',bright, co(a, bright)
Brightness and contrast (matrix operations): (47.037094116210937, 69.661535915950921)
Brightness and contrast (for loops): 47.0370941162 69.661535916

Calculate brightness and congrast for three different regions of interest (ROIs). Try to define ROIs quite the same as in the image below. Explain resluts.

In [29]:
from IPython.display import Image
Image(filename="Figs_ipandcg_2014/brain_rois_brighteness_contrast.png")
Out[29]:
In [88]:
print brightness_contrast(img)
print brightness_contrast(img[:50,:150])
print brightness_contrast(img[100:230,200:300])
print brightness_contrast(img[250:330,330:480])
(47, 11.61895003862225)
(1, 2.6457513110645907)
(138, 10.099504938362077)
(20, 11.313708498984761)

Based on example observe the histogram for each image. Change numbers of bins.

In [40]:
n, bins, patches = plt.hist(img[100:230,200:300].flatten(0), 64, facecolor='green', alpha=0.75)

Grayscale invertion

Based on the lecture (by prof. Strumiłło adn prof. Strzelecki) implement a function that invert grayscale levels. Try to write the same function with the for loop.

In [90]:
def invert(img):
    mx = img.max()
    return mx-img
In [91]:
from IPython.display import Image
Image(filename="Figures/inverted.png")
Out[91]:

Nonlinear grayscale transformation

Based on the lecture implement a function of nonlinear graysacle transformations. Compare with oryginal image. Using nonlinear transformation you should change type of your image to e.g. float32. Remember about NORMALISATION (!)

img2 = np.array(img, dtype=np.float)

Observe the histogram for each image.

In [43]:
img2 = np.array(img, dtype=plt.np.float)
img2 = img2**2
print img2.max()
img2 = img2/img2.max()
print img2.max()
65025.0
1.0

In [42]:
img3 = np.array(img, dtype=plt.np.float)
img3 = np.sqrt(img)
print img3.max()
img3 = img3/img3.max()
print img3.max()
15.9687
1.0

Oryginal image

In [94]:
from IPython.display import Image
Image(filename="Figures/oryginal.png")
Out[94]:

Brightness image to power of 2

In [95]:
from IPython.display import Image
Image(filename="Figures/nonlinear_x2.png")
Out[95]:

Squere root of the brightness

In [96]:
from IPython.display import Image
Image(filename="Figures/nonlinear_sqrt.png")
Out[96]:

3D image(s)

Load 3D image raw file "qinp01_3000_036_3_256.raw". Read about raw data format. Display:

In [55]:
img3d = np.fromfile('./images_2014/qinp01_3000_036_3_256.raw', dtype=np.uint8)
img3d.shape = (256,256,256)

Slice nr 125

In [98]:
from IPython.display import Image
Image(filename="Figures/3d_slice.png")
Out[98]:

Maximum Intensity PRojection (MIP)

In [99]:
from IPython.display import Image
Image(filename="Figures/3d_mip.png")
Out[99]:

* Depth cooded as a brightness*

In [100]:
from IPython.display import Image
Image(filename="Figures/3d_argmip.png")
Out[100]:

Software for visualization consecutive slides from 3D image

Change software that you have used for thresholding in such a way that you could manipulate displayed slice number with the use of slider.

In [2]:
from IPython.display import Image
Image(filename="Figs_ipandcg_2014/session_2_3d-images-slice-selection.png")
Out[2]:
In []: