Image Processing using Matlab

Bikash Santra

Research Scholar, Indian Statistical Institute, Kolkata

Topics Covered:

1) Intensity Transformations
2) Spatial Filtering
3) Geometric Transformations and Image Registration

1. Intensity Transformations

Image Arithmetic

In [ ]:
# Addition of two images
Z = imadd(X,Y)

# Case 1:
I = imread('rice.png');
J = imread('cameraman.tif');

K = imadd(I,J,'uint16');
imshow(K,[])

# Case 2: 
K1 = imadd(I,50);
imshow(K1)
imshow(I)
In [ ]:
# Other arithmatic operations
imabsdiff, imapplymatrix, imcomplement, imdivide, imlincomb, immultiply, imsubtract

Contrast Adjustment

In [ ]:
# Default imadjust function to adjust image intensity values
J = imadjust(I);  # imadjust saturates the bottom 1% and the top 1% of all pixel values.

I = imread('pout.tif');
imshow(I);

J = imadjust(I);
figure
imshow(J);

In [ ]:
# Direct manipulation of pixels using imadjust
# s = T(r), where r denotes the intensity of f and s the intensity of g, both at the same coordinates
# (x, y) in the images.

g = imadjust(f, [low_in, high_in], [low_out, high_out], gamma); # gamma specifies the shape of the curve
g1 = imadjust(f, [0, 1], [1, 0]); # [alternate code: g = imcomplement(f)]
g2 = imadjust(f, [0.5 0.75], [0 1]);
In [ ]:
# Other matlab functions for contrast adjustment
imcontrast, imsharpen, localcontrast, imnoise, etc.

Add Noise to Image

In [ ]:
I = imread('cameraman.tif');

J = imnoise(I,'gaussian');
J = imnoise(I,'gaussian',m,var_gauss);

J = imnoise(I,'poisson');

J = imnoise(I,'salt & pepper');
J = imnoise(I,'salt & pepper',d);

Deblurring

In [ ]:
# Set the random number generator back to its default settings for
# consistency in results.
rng default;

# First make blurred image
I = checkerboard(8);
PSF = fspecial('gaussian',7,10); # point-spread function (PSF)
V = .0001;
BlurredNoisy = imnoise(imfilter(I,PSF),'gaussian', 0, V); 

WT = zeros(size(I));
WT(5:end-4,5:end-4) = 1;
INITPSF = ones(size(PSF));

[J P] = deconvblind(BlurredNoisy,INITPSF,20,10*sqrt(V),WT); # WT is the additive noise

subplot(221);
imshow(BlurredNoisy);
title('A = Blurred and Noisy');
subplot(222);
imshow(PSF,[]);
title('True PSF');
subplot(223);
imshow(J);
title('Deblurred Image');
subplot(224);
imshow(P,[]);
title('Recovered PSF');

2. Spatial Filtering

Linear Spatial Filters

In [ ]:
w = fspecial('type', parameters)

In [ ]:
# Example
w = fspecial('laplacian', 0);
w = [0 1 0; 1 -4 1; 0 1 0]; # Alternate

g1 = imfilter(f, w, 'replicate');
imshow(g1, [ ]);

Nonlinear Spatial Filters

Function ordfilt2, computes order-statistic filters (also called rank filters). These are nonlinear spatial filters whose response is based on ordering (ranking) the pixels contained in an image neighborhood and then replacing the value of the center pixel in the neighborhood with the value determined by the ranking result.

In [ ]:
g = ordfilt2(f, order, domain); # domain acts like logical mask

# Example
g = ordfilt2(f, 1, ones(m, n)) # min filter, mask is of size m x n
g = ordfilt2(f, m*n, ones(m, n)) # max filter
g = ordfilt2(f, (m*n + 1)/2, ones(m, n)) # median filter - alternate "medfilt2"

Reference:
1) https://in.mathworks.com/help/images/linear-filtering.html
2) Digital Image Processing using Matlab by Gonzalez et al.

3. Geometric Transformations and image Registration

Common Geometric Transformations

In [ ]:
imcrop:	Crop image
imresize:	Resize image
imrotate:	Rotate image
imtranslate:	Translate image
....etc.
In [ ]:
I = imread('cameraman.tif');
[J, rect] = imcrop(I); # Using GUI

I2 = imcrop(I,[75 68 130 112]);
In [ ]:
J = imrotate(I,90);
In [ ]:
J = imtranslate(I,[25.3, -10.1],'FillValues',255);

Generic Geometric Transformations

Example: Affine Transformations

In [ ]:
Other Transformations
a) 'nonreflective similarity'
b) 'similarity'
c) 'projective'
d) 'polynomial'
e) 'piecewise linear'
f) 'lwm'
In [ ]:
## CASE 1: fitgeotrans

I = checkerboard(40);
J = imrotate(I,30);
imshowpair(I,J,'montage')

fixedPoints = [41 41; 150 100; 281 161];
movingPoints = [56 175; 165 210; 324 160];

tform = fitgeotrans(movingPoints,fixedPoints,'affine');

# Apply geometric transformation to image
Jregistered = imwarp(J,tform,'OutputView',imref2d(size(I)));

figure
imshowpair(I,Jregistered);
In [ ]:
## CASE 2: estimateGeometricTransform
    
original  = imread('cameraman.tif');
imshow(original);
title('Base image');

distorted = imresize(original,0.7); 
distorted = imrotate(distorted,31);
figure; imshow(distorted);
title('Transformed image');

# Detect and extract features from both images
ptsOriginal  = detectSURFFeatures(original);
ptsDistorted = detectSURFFeatures(distorted);
[featuresOriginal,validPtsOriginal] = ...
    extractFeatures(original,ptsOriginal);
[featuresDistorted,validPtsDistorted] = ...
    extractFeatures(distorted,ptsDistorted);
    
# Match features
index_pairs = matchFeatures(featuresOriginal,featuresDistorted);
matchedPtsOriginal  = validPtsOriginal(index_pairs(:,1));
matchedPtsDistorted = validPtsDistorted(index_pairs(:,2));

figure; 
showMatchedFeatures(original,distorted,...
    matchedPtsOriginal,matchedPtsDistorted);
title('Matched SURF points,including outliers');

# Exclude the outliers, and compute the transformation matrix
[tform,inlierPtsDistorted,inlierPtsOriginal] = ...
    estimateGeometricTransform(matchedPtsDistorted,matchedPtsOriginal,...
    'similarity');
figure; 

showMatchedFeatures(original,distorted,...
    inlierPtsOriginal,inlierPtsDistorted);
title('Matched inlier points');

# Recover the original image from the distorted image
outputView = imref2d(size(original));
Ir = imwarp(distorted,tform,'OutputView',outputView);
figure; imshow(Ir); 
title('Recovered image');