from IPython.display import Image
Image(filename='Figs_ipandcg_2014/2014_Session_2.png')
In this session we will learn how to:
All imports should be done once. Please note, whenever you reset session (%reset) you heve to reload all packages and modules.
import numpy as np
import matplotlib.pyplot as plt
import scipy.misc as misc
import Image #(Python Imaging Library - PIL)
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).
img = plt.imread('images_2014/brain.bmp')
plt.imshow(img, cmap=plt.cm.gray, vmin=10, vmax=100)
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:
from IPython.display import Image
Image(filename="Figs_ipandcg_2014/brain_windowed.png")
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):
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:
To convert between Pyton Imaging Library (PIL) and numpy.ndarray objects use functions:
im = Image.fromarray(img)
print 'old object',type(img)
print 'new object', type(im), im.mode, im.size, im.format
im2 = misc.toimage(img)
print 'old object',type(img)
print 'new object',type(im2), im2.mode, im2.size, im2.format
img2 = misc.fromimage(im2)
print 'old object',type(im2)
print 'new object',type(img2), img2.shape, img2.dtype
img3 = np.array(im2)
print 'old object',type(im2)
print 'new object',type(img2),img3.shape, img3.dtype
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.
# 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.
# 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)
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?
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
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)
# 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)
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.
from IPython.display import Image
Image(filename="Figs_ipandcg_2014/brain_rois_brighteness_contrast.png")
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])
Based on example observe the histogram for each image. Change numbers of bins.
n, bins, patches = plt.hist(img[100:230,200:300].flatten(0), 64, facecolor='green', alpha=0.75)
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.
def invert(img):
mx = img.max()
return mx-img
from IPython.display import Image
Image(filename="Figures/inverted.png")
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.
img2 = np.array(img, dtype=plt.np.float)
img2 = img2**2
print img2.max()
img2 = img2/img2.max()
print img2.max()
img3 = np.array(img, dtype=plt.np.float)
img3 = np.sqrt(img)
print img3.max()
img3 = img3/img3.max()
print img3.max()
Oryginal image
from IPython.display import Image
Image(filename="Figures/oryginal.png")
Brightness image to power of 2
from IPython.display import Image
Image(filename="Figures/nonlinear_x2.png")
Squere root of the brightness
from IPython.display import Image
Image(filename="Figures/nonlinear_sqrt.png")
Load 3D image raw file "qinp01_3000_036_3_256.raw". Read about raw data format. Display:
img3d = np.fromfile('./images_2014/qinp01_3000_036_3_256.raw', dtype=np.uint8)
img3d.shape = (256,256,256)
Slice nr 125
from IPython.display import Image
Image(filename="Figures/3d_slice.png")
Maximum Intensity PRojection (MIP)
from IPython.display import Image
Image(filename="Figures/3d_mip.png")
* Depth cooded as a brightness*
from IPython.display import Image
Image(filename="Figures/3d_argmip.png")
Change software that you have used for thresholding in such a way that you could manipulate displayed slice number with the use of slider.
from IPython.display import Image
Image(filename="Figs_ipandcg_2014/session_2_3d-images-slice-selection.png")