Skip to content
Snippets Groups Projects
Commit 8c8e42e4 authored by bepi's avatar bepi
Browse files

test

parent 6356114a
No related branches found
No related tags found
1 merge request!7modified to inline for figure to be visible
%% Cell type:markdown id: tags:
 
# Segmentation exercise - tools
 
[1. Load libraries](#1)
 
[2. Load, display and investgate image](#2)
 
[3. Image thresholding](#3)
 
The pupose of this exercise is to give some examples of tools for image processing that are relevant in relation to image segmentation. Some of these examples will be useful when you solve the segmentation exercise.
 
%% Cell type:markdown id: tags:
 
---
<a id='1'>
 
%% Cell type:markdown id: tags:
 
## 1. Load libraries
You will need to use differnet libraries to perform image analysis. Here we have chosen OpenCV as a computer vision library that contains many functions for image processing and analysis. We also need functions for displaying images and plot results, and for this we use matplotlib. Numpy is a library for nummerical analysis and linear algebra. Finally we import the os library, which we will use for getting names of files in a directory.
 
%% Cell type:code id: tags:
 
``` python
# Load relevant libraries
# scikit-image for image processing
import skimage as sk
import skimage.io as skio
 
import scipy.ndimage as snd
 
# Plot functions
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib notebook
 
# Nummerical library
import numpy as np
 
# System library e.g. for opening files in a library
import os
```
 
%% Cell type:markdown id: tags:
 
---
<a id='2'>
 
%% Cell type:markdown id: tags:
 
## 2. Load, display and investigate image
The first thing you typically do, when you start analyzing an image is to get a visual impression of the image, see what the intensity values the image is made of, what data type it has, etc. These properties will give some ideas of how you can build up your analysis.
 
%% Cell type:code id: tags:
 
``` python
# Directory containing image
in_dir = "data/data_examples/"
 
# Name of image file
im_name = "dino_recon.png"
 
# Read the image. Here the directory and the image name is concatenated by "+" to give the full path to the image.
im_org = skio.imread(in_dir + im_name)
 
# Matplotlib is used for displaying the image. The colormap is given by cmap = "gray". You can try differnet colormaps
# e.g. jet or summer
plt.figure(figsize=(10,10))
plt.imshow(im_org, cmap="gray")
plt.show()
plt.show();
```
 
%% Output
 
 
 
%% Cell type:code id: tags:
 
``` python
# You can determine the data type of the image by writing
print(type(im_org)) # This just shows that the image is stored as a numpy.ndarray
 
# If you want to determine the data type you can print the dtype-field by writing
print(im_org.dtype)
 
# Alternatively you can look at the first element in the array
print(type(im_org[0,0]))
 
# Check the size of the image
print(im_org.shape)
```
 
%% Output
 
<class 'numpy.ndarray'>
uint16
<class 'numpy.uint16'>
(512, 512)
 
%% Cell type:markdown id: tags:
 
### Display image histogram
We will display the histogram to inspect the intensity distribution of the image.
 
%% Cell type:code id: tags:
 
``` python
# Compute the histogram
plt.figure(figsize=(12,5))
# The function hist takes a 1D array as input, and therefore the numpy function is called to convert the image to a
# 1D array.
plt.hist(im_org.ravel(),100)
plt.show()
plt.show();
```
 
%% Output
 
 
 
%% Cell type:markdown id: tags:
 
You can also extract the value of the histogram.
 
%% Cell type:code id: tags:
 
``` python
# The hist function returns a tuple containing the values of the histogram where the first element is the bin counts
# and the second element is the bin edges.
h = plt.hist(im_org.ravel(),100)
print("Data type:", type(h))
bin_no = 1
print("Count in bin number %d: %d" % (bin_no, h[0][bin_no]))
print("Bin edges for value in bin number %d: %d to %d" % (bin_no, h[1][bin_no], h[1][bin_no+1]))
```
 
%% Output
 
Data type: <class 'tuple'>
Count in bin number 1: 3576
Bin edges for value in bin number 1: 655 to 1310
 
%% Cell type:markdown id: tags:
 
---
<a id='3'>
 
%% Cell type:markdown id: tags:
 
## 3. Image thresholding
The image is segmented by setting a threshold. You can try different values, but looking at the histogram or at values in different parts of the image shoudl give you an idea of a good choice.
 
%% Cell type:code id: tags:
 
``` python
# Select a threshold value
thres = 25000
 
# The thresholded image can be obtained using a simple logical expression.
im_thres = im_org > thres
# Using a logical expression gives a boolean image, and you can change the data type using the numpy function astype()
# Print the data type of the threshold
print("Data type from logical expression: %s" % im_thres.dtype)
# Change the data type
im_thres_float = im_thres.astype(float)
print("Data type changed: %s" % im_thres_float.dtype)
 
# Show the thresholded image
plt.figure(figsize=(12,7))
plt.subplot(1,2,1)
plt.imshow(im_thres, cmap = "gray")
plt.title('Threshold segmented image - boolean')
 
plt.subplot(1,2,2)
plt.imshow(im_thres_float, cmap = "gray")
plt.title('Threshold segmented image - float')
plt.show()
plt.show();
```
 
%% Output
 
Data type from logical expression: bool
Data type changed: float64
 
 
 
%% Cell type:markdown id: tags:
 
Thresholding using a logical expression will give an image of booleans, that is values true and false. This a very compact representation, becasue this format only takes up one bit per pixel. We can look at a value of a pixel by using the function print().
 
%% Cell type:code id: tags:
 
``` python
# We should not point outside the image, so we can get the image size by
(r, c) = im_thres.shape
print("Number of rows %d, number of columns %d" % (r, c))
 
# Show a pixel value at a location from 0 to r-1 or c-1 respectively
print(im_thres[0,0])
 
# Let us try to go outside the image range. This will result in an index out of bounds error
print(im_thres[0,c])
 
 
```
 
%% Output
 
Number of rows 512, number of columns 512
False
 
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-2b5ebf5825ae> in <module>
7
8 # Let us try to go outside the image range. This will result in an index out of bounds error
----> 9 print(im_thres[0,c])
10
11
IndexError: index 512 is out of bounds for axis 1 with size 512
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment