Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Nearest Neighbor Interpolation


This method is the simplest technique that re samples the pixel values present in the input vector or a matrix. In MATLAB, ‘imresize’ function is used to interpolate the images.


Consider the following example,



The pictorial representation depicts that a 3x3 matrix is interpolated to 6x6 matrix. The values in the interpolated matrix are taken from the input matrix (i.e) no new value is added.

MATLAB CODE:

%3x3 Matrix
A = zeros(3,3);
A(:)=[10,2,9,4,18,14,22,7,25];

display(A); %Before Interpolation

C = imresize(C,[6,6],'nearest');
display(C); %After Interpolation

EXPLANATION:
The result as shown in the pictorial representation can be achieved using the MATLAB function ‘imresize’
Now let’s see how to perform nearest neighbor interpolation without using the MATLAB ‘imresize’ function



MATLAB CODE:
%READ AN INPUT IMAGE
A=imread('cameraman.tif');


% DEFINE THE RESAMPLE SIZE
Col = 512;
Row = 512;


%FIND THE RATIO OF THE NEW SIZE BY OLD SIZE
rtR = Row/size(A,1);
rtC = Col/size(A,2);


%OBTAIN THE INTERPOLATED POSITIONS
IR = ceil([1:(size(A,1)*rtR)]./(rtR));
IC = ceil([1:(size(A,2)*rtC)]./(rtC));


%ROW_WISE INTERPOLATION
B = A(:,IR);


%COLUMN-WISE INTERPOLATION
B = B(IC,:);



figure,subplot(121),imshow(A);title('BEFORE INTERPOLATION'); axis([0,512,0,512]);axis on;

subplot(122),imshow(B);title('AFTER INTERPOLATION');  axis([0,512,0,512]);axis on;




MATLAB CODE :

clear all
clc
close all
%READ A RGB IMAGE
A=imread('peppers.png');


% DEFINE THE RESAMPLE SIZE
Col = 256;
Row = 192;


%FIND THE RATIO OF THE NEW SIZE BY OLD SIZE
rtR = Row/size(A,1);
rtC = Col/size(A,2);

%OBTAIN THE INTERPOLATED POSITIONS
IR = ceil([1:(size(A,1)*rtR)]./(rtR));
IC = ceil([1:(size(A,2)*rtC)]./(rtC));


%RED CHANNEL
Temp= A(:,:,1);
%ROW-WISE INTERPOLATION
Red = Temp(IR,:);
%COLUMNWISE INTERPOLATION
Red = Red(:,IC);



%GREEN CHANNEL
Temp= A(:,:,2);
%ROW-WISE INTERPOLATION
Green = Temp(IR,:);
%COLUMNWISE INTERPOLATION
Green = Green(:,IC);


%BLUE CHANNEL
Temp= A(:,:,3);
%ROW-WISE INTERPOLATION
Blue = Temp(IR,:);
%COLUMNWISE INTERPOLATION
Blue = Blue(:,IC);



Output=zeros([Row,Col,3]);
Output(:,:,1)=Red;
Output(:,:,2)=Green;
Output(:,:,3)=Blue;

Output = uint8(Output);

figure,subplot(121),imshow(A);title('BEFORE INTERPOLATION'); axis([0,512,0,384]);axis on;

subplot(122),imshow(Output);title('AFTER INTERPOLATION');  axis([0,512,0,384]);axis on;




EXPLANATION:

For RGB image, the Red, Green and Blue channels are interpolated separately.




like button Like "IMAGE PROCESSING" page

Upsampling in MATLAB

Upsampling is the process of inserting zeros in between the signal value in order to increase the size of the matrix.  We will discuss about upsampling in both spatial and time domain.
1.1  Upsampling using MATLAB built-in function
1.2  Upsampling in 1D
1.3  Upsampling in 2D or image matrix
2.1  Upsampling a 1D signal
2.2  Upsampling a image matrix

UPSAMPLING IN SPATIAL DOMAIN:

Given: 1-D array of size 1xN
Upsample by factor of 2:  

Output: 1-D array of size 1x(2*N)
Where N represents length of the array, n represents the index starting from 0,1,2…,N
%UPSAMPLING USING MATLAB BUILT-IN FUNCTION 'UPSAMPLE'

A = 1:25;
B = upsample(A,2);

EXPLANATION:

The above MATLAB function will insert zeros in between the samples.

To upsample an array by ratio 2, update the output array as follows:
1.      If index(n) of the output array is divisible by 2(ratio), then update the output array with the value of the input array with index n/2
2.      Otherwise, insert zero

STEPS TO PERFORM:
1.      Consider an array A of size 1xM
2.      Obtain the upsample Ratio N
3.      Pre-allocate an array B of size 1x(M*N)
4.      If the index is divisible by N then update the array B with value of A else zero


MATLAB CODE:

%UPSAMPLING IN SPATIAL DOMAIN

A = 1:10;

%UPSAMPLING WITH RATIO 2
B = zeros([1 size(A,2)*2]);
B(1:2:end)=A;



%UPSAMPLING WITH RATIO N
N=3;
B=zeros([1 size(A,2)*N]);
B(1:N:end)=A;

EXPLANATION:
Let A = [1 2 3 4 5]
Let us upsample try to upsample A with ratio 2
Pre-allocate output matrix B = [0 0 0 0 0 0 0 0 0 0]
Substitute the value of the matrix A in indices divisible by the ratio i.e. 2 of matrix B
B(1:2:end) = A
Now B(1,3,5,7,9) =[1 2 3 4 5]
Thus B = [1 0 2 0 3 0 4 0 5 0]

NOTE:
Definition of upsampling is usually given assuming the index starts from zero. But in case of MATLAB, the index starts with one that is why the odd positions are considered instead of even.

STEPS TO PERFORM TO UPSAMPLE A 2D MATRIX:


MATLAB CODE:



%IMAGE UPSAMPLING

A = imread('cameraman.tif');

M = 2;
N = 3;

B = zeros([size(A,1)*M size(A,2)*N]);
B(1:M:end,1:N:end) = A;

figure,imagesc(B);colormap(gray);


sz = M*N;
H = fspecial('average',[sz sz]);
C = conv2(B,H,'same');

figure,imagesc(C);colormap(gray);


EXPLANATION:


The above image is the pixel representation of the zero inserted image. In each row, two zeros are inserted between the pixels and in the each column; single zero is inserted between the pixels. In spatial domain, inserting zeros will be quite visible so it’s advisable to perform any low pass filtering or approximation like spatial averaging or applying Gaussian.
In the above example, spatial averaging is done. In order to understand how spatial averaging is done using convolution check the following link:








like button Like "IMAGE PROCESSING" page

Upsampling in Frequency Domain




1.1  Upsampling using MATLAB built-in function
1.2  Upsampling in 1D
1.3  Upsampling in 2D or image matrix
2.1  Upsampling a 1D signal
2.2  Upsampling a image matrix

In Frequency domain, upsampling means nothing but the padding of zeros at the end of high frequency components on both sides of the signal.

STEPS TO PERFORM:

1.      Read an image
2.      Obtain the ratio to upsample
3.      Perform Fast Fourier Transform
4.      Shift the Low frequency components to the centre and High frequency components outside.
5.      Add zeros on both the sides of the image
6.      Shift the High frequency components to the centre and Low frequency components to the exterior (Inverse of fftshift)
7.      Perform Inverse Fast Fourier Transform
8.      Display the Upsampled Image

MATLAB CODE:

%UPSAMPLING IN FREQUENCY DOMAIN
%1D UPSAMPLING

FS = 100;
t  = 0:(1/FS):1;

A = 10*sin(2*pi*5*t);
figure,plot(t,A);
 



%FOURIER DOMAIN

FT = fft(A);
fq =linspace(-1/FS,1/FS,101);
figure,plot(fq,abs(FT));title('Before FFTSHIFT');


FT_s = fftshift(FT);
figure,plot(fq,abs(FT_s));title('After FFTSHIFT');


pad_zero = padarray(FT_s,[0 50]);

fq =linspace(-1/FS,1/FS,201);
figure,plot(fq,abs(pad_zero));title('After PADDING WITH ZEROS');


%INVERSE FOURIER TRANSFORM
IFT = ifft(ifftshift(pad_zero));

%UPSAMPLED SIGNAL
t1 = linspace(0,1,201);
figure,plot(t1,(IFT*2),'r',t,A,'g');




EXPLANATION:

Amplitude of the input original signal is 10 and the frequency is 5.
Similarly, the amplitude of the upsampled signal is 10 and the frequency is 5. The number of samples used to plot the signal is increased in the later case.



IMAGE UPSAMPLING IN FOURIER DOMAIN

MATLAB CODE:

%READ AN INPUT IMAGE
A = imread('cameraman.tif');

%RATIO
RatioM = 3;
RatioN = 2;

%UPSAMPLING OVER EACH ROW

mnrow = round(size(A,2)*(RatioM-1)/2);
% 1D FFT ON EACH ROW
row_fft = fft(A,[],2);

%PAD WITH ZEROS ON BOTH SIDES OF EACH ROW
pad_row = padarray(fftshift(row_fft,2),[0 mnrow]);

 
Logarthmic scale
%UPSAMPLING OVER EACH COLUMN
mncol = round(size(A,1)*(RatioN-1)/2);

% 1D FFT ON EACH COLUMN
col_fft = fft(pad_row,[],1);

%PAD WITH ZEROS ON BOTH SIDES OF EACH COLUMN
pad_col = padarray(fftshift(col_fft,1),[mncol 0]);
 
Logarthmic scale
%PERFORM 1D IFFT ON EACH COLUMN
ifft_col = ifft(ifftshift(pad_col,1),[],1);

%PERFORM 1D IFFT ON EACH ROW
ifft_col_row = ifft(ifftshift(ifft_col,2),[],2);

%DISPLAY THE IMAGE AFTER UPSAMPLING
res = abs(ifft_col_row);
res = uint8(res*(numel(res)/numel(A)));


figure,imagesc(res);




SIMPLE VERSION :

A = imread('cameraman.tif');

%RATIO
RatioM = 3;
RatioN = 2;



mnrow = round(size(A,2)*(RatioM-1)/2);
mncol = round(size(A,1)*(RatioN-1)/2);

%FFT ON 2D MATRIX
FT = fftshift(fft2(A));


%PADDING WITH ZEROS
pad_rc = padarray(FT,[mncol mnrow]);


%INVERSE FOURIER TRANSFORM
IFT = ifft2(ifftshift(pad_rc));

Img = uint8(abs(IFT)*(numel(IFT)/numel(A)));
figure,imagesc(Img);




like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com