Lecture - 4 Medical Image Analysis
Lecture - 4 Medical Image Analysis
ANALYSIS
DR. ZOBIA SUHAIL
PHD IN MEDICAL IMAGE PROCESSING
A B E R Y S T W Y T H U N I V E R S I T Y, WA L E S , U K ( 2 0 1 9 )
MEDICAL IMAGE ANALYSIS
• Course overview
• Python Introduction using common libraries
• How to read and analyze image in Python
• Image Intensities
• Difference in intensity levels for Grayscale and Color images
• Image Histograms
• Some Discussion on the Application areas of Image Histograms
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
An image histogram can be used to threshold the image to convert it into binary image.
If the image intensities are well separated into two defined objects, the histogram is well
Suited to define the threshold of the image.
The histogram will be bi-modal and the pixel intensities can be separated into two well
Defined groups.
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
Image Source: Optimal Threshold Computing in Automatic Image Thresholding using Adaptive Particle Swarm Optimization
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
If T is the obtained threshold, then the image can be binarized using the following relation:
Import cv2 as cv
Import imageio as io
Im=io.imread(‘original.png’)
thresh=190
r,c=m_img.shape
i=0
j=0
new_im=np.zeros((r,c),dtype=np.uint8)
for i in range(r):
for j in range(c):
if(m_img[i,j]>=thresh):
new_im[i,j]=1
else:
new_im[i,j]=0
plt.imshow(new_im,cmap="gray")
plt.show()
plt.close()
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Discussion:
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType)
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType)
Ret,thresh=cv2.threshold(im,120,256,cv2.Threshold_Binary)
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType)
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType)
cv2.THRESH_BINARY
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType)
cv2.THRESH_BINARY_INV
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType
cv2.THRESH_TRUNC
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType
cv2.THRESH_TOZERO
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
PYTHON
Cv2.threshold
Ret,dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType
cv2.THRESH_TOZERO_INV
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
PYTHON
Thresholding using Python libraries:
Cv2 library in Python for Thresholding
Cv2.threshold
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
PYTHON
Thresholding using Python libraries:
Cv2 library in Python for Thresholding
Cv2.threshold
Ret,dest=cv.threshold(im1,190,255,cv.THRESH_BINARY)
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
PYTHON
Thresholding using Python libraries:
Cv2.threshold
Ret,dest=cv.threshold(im1,190,255,cv.THRESH_BINARY_INV)
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
PYTHON
Thresholding using Python libraries:
Cv2 library in Python for Thresholding
Cv2.threshold
Ret,dest=cv.threshold(im1,190,255,cv.THRESH_TRUNC)
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
PYTHON
Thresholding using Python libraries:
Cv2.threshold
Ret,dest=cv.threshold(im1,190,255,cv.THRESH_TOZERO)
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
PYTHON
Thresholding using Python libraries:
Cv2.threshold
Import cv2 as cv
Thresh=190
Ret,dest=cv.threshold(im,thresh,255,cv.THRESH_TOZERO_INV)
plt.imshow(dest,cmap="gray")
plt.show()
plt.close()
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding:
The thresholds that we used so far are arbitrary chosen threshold values , that is not a good choice.
Ret, dest=cv2.threshold(im,thresh,max_value,cv2.ThresholdType+cv2.Thresh_Otsu)
Discussion:
MEDICAL IMAGE ANALYSIS
Histogram: [Applications]
Thresholding: [Otsu Method ---- Continued ]
And find the spread of pixels on each side of the threshold i.e. for foreground and for background.
• Grid of Cells
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('original.png',0)
ret,im1 = cv.threshold(img,180,255,cv.THRESH_BINARY)
im2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY,11,2)
plt.imshow(im1,cmap="gray")
plt.show()
plt.close()
plt.imshow(im2,cmap="gray")
plt.show()
plt.close()
Discussion:
MEDICAL IMAGE ANALYSIS
Applications of Histograms: [Image Enhancement - Histogram Equalization ]
Original Image
Enhanced Image
MEDICAL IMAGE ANALYSIS
Image Enhancement:
CV2.EQUALIZEHIST(IMG)
Steps:
1. Calculate the frequency against each intensity value
2. Compute the probability of each frequency
3. Compute Cumulative Distribution Function (CDF)
4. Multiple the CDF with the required intensity levels
5. Round the resultant value to Floor
MEDICAL IMAGE ANALYSIS
3 2 4 5
7 7 8 2
Suppose this matrix is representing pixel intensities.
3 1 2 3
5 4 6 7
MEDICAL IMAGE ANALYSIS
3 2 4 5
7 7 8 2 Intensity if Pixels range between 1 – 8
3 1 2 3 Suppose we want to apply histogram equalization
5 4 6 7 And scale the intensity to 1 - 20
MEDICAL IMAGE ANALYSIS
3 2 4 5
7 7 8 2 Pixel 1 2 3 4 5 6 7 8
Intensities
3 1 2 3
No. of Pixels 1 3 3 2 2 1 3 1
5 4 6 7
MEDICAL IMAGE ANALYSIS
3 2 4 5 Pixel Intensities 1 2 3 4 5 6 7 8
7 7 8 2 No. of Pixels 1 3 3 2 2 1 3 1
3 1 2 3 Probability .625 .1875 .1875 .125 .125 .0625 .1875 .0625
5 4 6 7
MEDICAL IMAGE ANALYSIS
3 2 4 5
7 7 8 2
Next step is to compute the cumulative probability
3 1 2 3
5 4 6 7
MEDICAL IMAGE ANALYSIS
Pixel Intensities 1 2 3 4 5 6 7 8
No. of Pixels 1 3 3 2 2 1 3 1
Probability .0625 .1875 .1875 .125 .125 .0625 .1875 .0625
3 2 4 5
7 7 8 2
As we want to increase the intensity to the reange1 -20,
3 1 2 3
5 4 6 7 Multiply each Cumulative Probability by 20
MEDICAL IMAGE ANALYSIS
Pixel Intensities 1 2 3 4 5 6 7 8
No. of Pixels 1 3 3 2 2 1 3 1
Probability .0625 .1875 .1875 .125 .125 .0625 .1875 .0625
3 2 4 5
7 7 8 2
Last step in to round the decimal values (floor rounding)
3 1 2 3
5 4 6 7
MEDICAL IMAGE ANALYSIS
Floor Rounding 1 5 8 11 13 15 18 20
MEDICAL IMAGE ANALYSIS
3 2 4 5 8 5 11 13
7 7 8 2 18 18 20 5
3 1 2 3 8 1 5 8
5 4 6 7 13 11 15 18
Original Image has been transformed to equalized image with different intensities
MEDICAL IMAGE ANALYSIS
CV2.equalizeHist(IMG)
Discussion:
MEDICAL IMAGE ANALYSIS
import cv2 as cv
im1=cv.imread('original.png',0)
equ_img= cv.equalizeHist(im1)
cv.imshow('Binary Image', equ_img)
cv.waitKey(0)
cv.destroyAllWindows()
Discussion:
MEDICAL IMAGE ANALYSIS