ISYE 8803 - Kamran - M2 - Image Processing
ISYE 8803 - Kamran - M2 - Image Processing
Dimensional Data
Analytics
Image Analysis
Kamran Paynabar, Ph.D.
Associate Professor
School of Industrial and Systems Engineering
Introduction to Image
Processing
Learning Objectives
• How to quantify an image
• Define difference between, RGB, Gray
scale and BW and their conversions
• How to control size and resolution
• Use basic functions in Matlab
Image Analysis Levels
• Image analysis is the process of processing raw images and extracting useful
information for decision making.
• Show an Image
imshow(I)
• Save an Image
imwrite(I,'rice.jpg','jpg')
• RGB/Gray to BW
I= imread('Einstein.jpg');
BW = im2bw(I,level)
Image Transformation
Learning Objectives
• Use image transformation techniques
• Define histogram of an image and how to
shift and stretch it
• How to enhance an image using
transformation functions
Image Histogram
• Histogram represents the distribution of gray levels
• For all pixels x[m,n], count all x[m,n] = I
• The x axis of the histogram shows the range of
pixel values and the y axis is the frequency.
• Matlab: imhist(I,N)
• Display a histogram for the image I using N bins
• Default: N = 256
MATLAB Example
I = imread('coins.png');
imhist(I)
Image Transformation
• Images can be transformed by applying a function on the image matrix,
1 𝑓𝑓 𝑥𝑥, 𝑦𝑦 > 𝑝𝑝
𝑔𝑔 𝑥𝑥, 𝑦𝑦 = 𝑇𝑇 𝑓𝑓 𝑥𝑥, 𝑦𝑦 =�
0 𝑓𝑓 𝑥𝑥, 𝑦𝑦 ≤ 𝑝𝑝
Transformation –
Shifting Histogram
• The brightness of an image can be
changed by shifting its histogram.
• Suppose the pixel values are ranged
between L and U (0 and 255 for gray-
scale). The transformation function is
defined by
𝑔𝑔 𝑥𝑥, 𝑦𝑦 = 𝑇𝑇 𝑓𝑓 𝑥𝑥, 𝑦𝑦 =
𝑈𝑈 𝑓𝑓 𝑥𝑥, 𝑦𝑦 > 𝑈𝑈 − 𝑠𝑠
�𝑓𝑓 𝑥𝑥, 𝑦𝑦 + 𝑠𝑠 otherwise
𝐿𝐿 𝑓𝑓 𝑥𝑥, 𝑦𝑦 ≤ 𝐿𝐿 − 𝑠𝑠
𝑠𝑠 = 0 𝑠𝑠 = 50 𝑠𝑠 = −50
Transformation –
Stretching Histogram
• The contrast of an image is defined by the difference between maximum and
minimum pixel intensity.
• It can be changed by stretching the histogram. Suppose the pixel values are
ranged between L and U. The transformation function is defined by
D=double(I);
imshow(I1=uint8((D-min(min(D)))/(max(max(D))-min(min(D)))*205))
• The number of different colors in an image is depends on bits per pixel (bpp).
𝐿𝐿 = 2𝑏𝑏𝑏𝑏𝑏𝑏
𝐿𝐿 = 28 𝐿𝐿 = 21
Gray Level Transformation
• Gray level transformation is often used for
image enchantment.
• Three typical transformation functions are
• Linear (negative image)
𝑔𝑔 𝑥𝑥, 𝑦𝑦 = 𝑇𝑇 𝑓𝑓 𝑥𝑥, 𝑦𝑦 = 𝐿𝐿 − 1 − 𝑓𝑓 𝑥𝑥, 𝑦𝑦
• Log
𝑔𝑔 𝑥𝑥, 𝑦𝑦 = 𝑇𝑇 𝑓𝑓 𝑥𝑥, 𝑦𝑦 = 𝑐𝑐 log(𝑓𝑓 𝑥𝑥, 𝑦𝑦 + 1)
• Power-Law
𝑔𝑔 𝑥𝑥, 𝑦𝑦 = 𝑇𝑇 𝑓𝑓 𝑥𝑥, 𝑦𝑦 = 𝑐𝑐𝑓𝑓 𝑥𝑥, 𝑦𝑦 𝛾𝛾
𝑐𝑐 = 45 𝑐𝑐 = 0.1
𝛾𝛾 = 1.5
Topics on High-
Dimensional Data
Analytics
Image Analysis
Kamran Paynabar, Ph.D.
Associate Professor
School of Industrial and Systems Engineering
Continuous Discrete
Sharpening
Edge
Detection
Blurring
Image Convolution with a Mask
• Flip the mask (kernel) both horizontally and vertically.
• Put the center element of the mask at every pixel of the image. Multiply the
corresponding elements and then add them up. Replace the pixel value
corresponding to the center of the mask with the resulting sum.
Image Convolution with a Mask
• For the pixels on the border of image matrix, some elements of the mask might
fall out of the image matrix. In this case, we can extend the image by adding
zeros. This is known as padding.
Example in Matlab
Y = imread('einstein.jpg');
K = cell(6,1);
Yk = cell(6,1);
for i = 1:6
Yk{i} = imfilter(Y,K{i});
subplot(2,3,i)
imshow(Yk{i})
title(num2str(round(K{i},1)))
end More types of masks can be found in: https://lodev.org/cgtutor/filtering.html
Denoising using Blurring Mask
Image Segmentation
Learning Objectives
• Define image segmentation
• Apply the Otsu’s model for image
segmentation
• Use K-means for partitioning images
Image Segmentation
• The main goal of image segmentation is to partition an image into multiple
sets of pixels (segments).
• Image segmentation has been widely used for object detection, face and
fingerprint recognition, medical imaging, video surveillance, etc.
t is the threshold
Otsu’s Method
• The goal is to automatically determine the threshold t given an image histogram.
• Formulation:
Determine t by minimizing (maximizing) the intra-class variance (inter-class
variance), defined by
𝜎𝜎𝜔𝜔2 𝑡𝑡 = 𝜔𝜔1 𝑡𝑡 𝜎𝜎12 𝑡𝑡 + 𝜔𝜔2 𝑡𝑡 𝜎𝜎22 𝑡𝑡
Weight 𝜔𝜔𝑖𝑖 𝑡𝑡 are the probabilities of the two classes separated by threshold
𝑡𝑡 and 𝜎𝜎𝑖𝑖2 𝑡𝑡 variance of these classes.
Class probability: 𝜔𝜔1 𝑡𝑡 = ∑𝑡𝑡𝑖𝑖=0 𝑝𝑝(𝑖𝑖) , 𝜔𝜔2 𝑡𝑡 = 1 − 𝜔𝜔1 𝑡𝑡
∑𝑡𝑡𝑖𝑖=0 𝑖𝑖𝑝𝑝 𝑖𝑖 ∑255
𝑖𝑖=𝑡𝑡 𝑖𝑖𝑖𝑖 𝑖𝑖
Class mean: 𝜇𝜇1 𝑡𝑡 = 𝜔𝜔1
; 𝜇𝜇2 𝑡𝑡 = 𝜔𝜔2
2
Inter class variance: 𝜎𝜎𝑏𝑏2 𝑡𝑡 = 𝜎𝜎 2 − 𝜎𝜎𝜔𝜔2 𝑡𝑡 = 𝜔𝜔1 𝑡𝑡 𝜔𝜔2 𝑡𝑡 𝜇𝜇1 𝑡𝑡 − 𝜇𝜇2 𝑡𝑡
Otsu’s Method in MATLAB
• Step 1: get the histogram of image
Y = imread('coins.png');
I = im2uint8(Y(:));
num_bins = 256;
counts = imhist(I,num_bins);
Attribution: Creative Commons Attribution-Share Alike 2.0 Generic license, Keith Allison,
File:Gasolwizards.jpg, https://commons.wikimedia.org/wiki/File:Gasolwizards.jpg#filelinks
MATLAB Example - K-Means
Clustering
for i = 1:max_iter
I = imread('coin.png'); % step 1: optimize the labels
imshow(I) dist = zeros(N,K);
X=reshape(I,size(I,1)*size(I,2),size(I,3)); for j = 1:N
X=double(X); for k = 1:K
K=2; dist(j,k) = norm(X(j,:)-C(k,:))^2;
max_iter = 100;
%Clustering
end K=2
end
[N, d] = size(X); [disto, index] = sort(dist,2);
L = zeros(N, 1); L = index(:,1);
C = zeros(K, d); % centers matrix % step 2: optimize the centers
for k = 1:K
if sum(L == k)~= 0
C(k,:) = sum(X(L == k, :),1)/sum(L == k);
end K=3
end
end
Y = reshape(L,size(I,1),size(I,2));
BW = Y == 1;
figure, imshow(BW)
MATLAB Example using built-in
function
% input image
I = imread('CS.png');
imshow(I)
X=reshape(I,size(I,1)*size(I,2),size(I,3));
Edge Detection
Learning Objectives
• How to perform edge detection methods
using image derivatives
• Use Sobel edge detection method
• Use Matlab for edge detection
Edge Detection Using Derivatives
• Edges are significant local changes of intensity in an
image.
3 x 3 neighborhood:
• The constant c is the weight given to pixels closer to the center of the mask.
Sobel Operator
• Setting c = 2, we get the Sobel operator:
fx fy
Sobel Operator f = fx.*fx + fy.*fy
fx fy
Example in MATLAB
f = fx.*fx + fy.*fy; f = b > cutoff;
Imagesc(f) imshow((fx.*fx+fy.*fy)>cutoff) edge(a,'sobel')
Krisch Operator
• Krisch is a another derivative mask that finds the maximum edge strength in eight
directions of a compass. 5 5 5
−3 0 −3
−3 −3 −3
5 5 −3 −3 5 5
5 0 −3 −3 0 5
−3 −3 −3 −3 −3 −3
5 −3 −3 −3 −3 5
5 0 −3 −3 0 5
5 −3 −3 −3 −3 5
−3 −3 −3
−3 −3 −3
−3 0 5
5 0 −3
−3 5 5
5 5 −3
−3 −3 −3
−3 0 −3
5 5 5
Prewitt Mask
• Prewitt is very similar to Sobel but with different masks.
−1 0 1
𝑀𝑀𝑥𝑥 = −1 0 1
−1 0 1
1 1 1
𝑀𝑀𝑦𝑦 = 0 0 0
−1 −1 −1
edge(a, 'prewitt')
Laplacian and Laplacian of
Gaussian Mask
• Laplacian mask is a second order derivative mask.
• For noisy images, is combined with a Gaussian mask to reduce the noise.
0 −1 0
−1 4 −1
0 −1 0
−1 −1 −1
−1 8 −1
−1 −1 −1
Laplacian
edge(a,'log')