# IMAGE PROCESSING

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

### PART 3: MEDIAN FILTER - RGB IMAGE

MATLAB CODE:

clear all

A = imnoise(I,'Salt & pepper',0.1);
figure,imshow(A);title('IMAGE WITH SALT AND PEPPER NOISE');

%DEFINE THE WINDOW SIZE MXN
M=3;
N=3;

%PAD THE MATRIX WITH ZEROS ON ALL SIDES

B = zeros([size(A,1) size(A,2)]);
med_indx = round((M*N)/2); %MEDIAN INDEX
for i = 1:size(modifyA,1)-(M-1)
for j = 1:size(modifyA,2)-(N-1)

temp = modifyA(i:i+(M-1),j:j+(N-1),:);
%RED,GREEN AND BLUE CHANNELS ARE TRAVERSED SEPARATELY
for k = 1:3

tmp = temp(:,:,k);
B(i,j,k) = median(tmp(:));

end

end
end

%CONVERT THE IMAGE TO UINT8 FORMAT.
B = uint8(B);
figure,imshow(B);
title('IMAGE AFTER MEDIAN FILTERING');

EXPLANATION:
·        The image is read into the matrix I.
·        Salt and pepper noise is added to the image.  Learn how to add 'salt and pepper noise to an image'.
·        Define the window size MxN example: 3x3, 7x7, 5x3
·        Pad the image with zeros on all sides. This is done to perform the filtering on the border pixels. .
·        Median is the middle point of the series. The index that is obtained by dividing the total number of elements in a window by 2 gives the position.

·        A sliding window of size M x N is used on each channel (Red, Green and Blue) separately and the elements in the window are sorted and the middle element from the sorted array is chosen.
Like "IMAGE PROCESSING" page

### PART 2: 2D MEDIAN FILTER WITH DIFFERENT WINDOW SIZE

MATLAB CODE:

clear all

display(size(I));

%CONVERT RGB IMAGE INTO GRAYSCALE
A = rgb2gray(I);

%ADD SALT AND PEPPER NOISE TO THE GRAYSCALE IMAGE
A = imnoise(A,'Salt & pepper',0.1);

figure,imshow(A);title('IMAGE WITH SALT AND PEPPER NOISE');

%DEFINE THE WINDOW SIZE MXN
M=5;
N=5;

%PAD THE MATRIX WITH ZEROS ON ALL SIDES

B = zeros([size(A,1) size(A,2)]);
med_indx = round((M*N)/2); %MEDIAN INDEX

for i=1:size(modifyA,1)-(M-1)
for j=1:size(modifyA,2)-(N-1)

temp=modifyA(i:i+(M-1),j:j+(N-1),:);
tmp_sort = sort(temp(:));%tmp(:) converts 2D matrix to 1D matrix
B(i,j) = tmp_sort(med_indx);

end
end

%CONVERT THE IMAGE TO UINT8 FORMAT.
B=uint8(B);
figure,imshow(B);
title('IMAGE AFTER MEDIAN FILTERING');

EXPLANATION:
·        The image is read into the matrix A.
·        The image is color matrix that contains Red, Green and Blue channels so ‘rgb2gray()’ command is used to convert RGB image to grayscale.
·        Salt and pepper noise is added to the image.  Learn how to add 'salt and pepper noise to an image'.
·        Define the window size MxN example: 3x3, 7x7, 5x3
·        Pad the image with zeros on all sides. This is done to perform the filtering on the border pixels. .
·        Median is the middle point of the series. The index that is obtained by dividing the total number of elements in a window by 2 gives the position.
·        A sliding window of size M x N is used and the elements in the window are sorted and the middle element from the sorted array is chosen.

EXPLANATION:

·        The image filtered with 3x3 window still contains salt and pepper noise but the edges are still sharp.
·        The image filtered with 5x5 window  contains less noise compared to 3x3 window filter and edges are smoothed a bit
·        The image filtered with 7x7 window is free of noise but the edges are smoothed to certain extent.
Like "IMAGE PROCESSING" page

### Read, process and save video in MATLAB

This tutorial will come in handy if you are interested in learning about video processing using MATLAB. Techniques such as Reading and writing a video file, displaying the frames and writing the frames as images in a folder are discussed below.

‘VideoReader’ command in MATLAB creates reader object for the video file. This object contains the metadata or the parameters related to the video such as Frame rate, Height and Width of the frames,duration of the video etc.
To read all the frames in the video, we can use two methods. The first method is to find the number of frames in the video and read it. The second method is to read the frames until no more video frames are available. Here in the tutorial, the frame rate is assumed to be constant throughout the duration of the video. At constant frame rate, the number of frames in the video is obtained by direct multiplication of frame rate and video duration. In our example, the video is 13.29 seconds long and the frame rate is 24.0. Multiplying 13.29 and 24 gives 318.96 that is 319 frames available in the video.

1. To Read the frames in the video , display , save as image file and store as mat file

EXAMPLE : 1
%MATLAB CODE:

%Num_Frames = Vptr.NumberOfFrames;
NFrames = round(Vptr.FrameRate*Vptr.Duration);

%Find the height and weight of the frame
Nrows = Vptr.height;
Ncols = Vptr.width;

%Preallocate the matrix
Img_s = zeros([Nrows,Ncols,NFrames]);

for i = 1:NFrames

%To display all the frames
figure,imshow(Img);

%To save the images
Img_name=['Image',num2str(i),'.jpg'];
imwrite(Img,Img_name);

%To store in MAT file
Img_s(:,:,i)=Img;
end

%Save the matrix as .mat file
Save Video_Images.mat Img_s;

EXPLANATION:
The above MATLAB code can
a. Display all the frames in the video
b. Save all the frames as images in the current working directory
c. Store all the frames as images in a multidimensional matrix and save it as ‘.mat’ file

After creating the video reader object, the number of frames is computed using the frame rate and duration of the video. The height and width of the frame can be obtained from the metadata.

‘readFrame’ extracts each frame sequentially in the image format. The image can be further displayed using ‘imshow’ or written to an image file using ‘imwrite’ command or stored in a multidimensional matrix as stack of images.

The name format of the images saved in the current working directory will be ‘Image1.jpg’,’Image2.jpg’…’Image319.jpg’

2. To read all the frames in the video and display few frames
EXAMPLE 2:

%MATLAB CODE
NFrames = round(Vptr.FrameRate*Vptr.Duration);

Jump_ptr = 27;
N = 1;

%To display the Images
for i=1:NFrames

if(mod(i-1,Jump_ptr)==0)
figure(2),subplot(3,4,N),imshow(Img);
N=N+1;
end

end

EXPLANATION:
The above MATLAB code reads all the frames in the video but displays only few frames. This example typically highlights the use of MATLAB command ‘subplot’.
Instead of displaying all the frames, frames with specific interval are displayed. In this instance,frame 1 will be displayed first, frame 28 the next then followed by 55 and so on and so forth. ‘mod’ command is used to find the remainder after division, so whenever ‘i’ assumes the value equal to multiples of the variable ‘Jump_ptr’ then the image will be displayed. To displayall the images in the same figure, ‘subplot’ can be used.

‘subplot(3,4,N)’ refers that the ‘figure(2)’ can be divided into 3 rows and 4 columns and each image can be placed in each position.  In the given example, number of frames =319 and the interval distance (Jump_ptr) is 27, then 319/27 gives 12. So the subplot is divided as 3 rows and 4 columns to allocate spacefor 12 images.

3. To read from a video file and write the frames to another video file

%To write frames to the video
Wptr = VideoWriter('VIDE02.mp4','MPEG-4');
Wptr.FrameRate=10;
open(Wptr);
for i=1:120
writeVideo(Wptr,Img);

end
close(Wptr);

EXPLANATION:

Create the video writer object using ‘VideoWriter’ for ‘VIDEO2.mp4’
Set the frame rate for the video to be written to a file.
Here, the frame rate 10 indicates,10 frames will be displayed per second in a video.
‘open’ command will open the video file to start the writing process. Instead of 319 frames from the original video(‘VIDEO1.MP4’), only 120 frames are written to the video file.So it is unnecessary to go through all the frames in the video. First read the frame from the input video file and write it to the output video file. After 120 frames are read from the input file and written to the output file, the output file is closed.

4. To read a video file and process the frames and write it to another video file

%To write frames to the video
%Find number of frames
NFrames = round(Vptr.FrameRate*Vptr.Duration);

%Create Video Writer Object
Wptr = VideoWriter('VIDEO_NOISY.mp4','MPEG-4');
%Open the output video file
open(Wptr);
for i=1:NFrames
Img = imnoise(Img,'salt & pepper');
%write to video file
writeVideo(Wptr,Img);

end
%Close the output video file
close(Wptr);

EXPLANATION:

All the frames in the input video is processed and then written to an output file. Here, noise is added to each frame in the intermediate step and then written to the output video file. However, instead of addition of noise, the image can be enhanced or processed in the intermediate step.

EXAMPLE:

%EXAMPLE - VIDEO PROCESSING
%Set the frame rate
%Crop the Image

%Find number of frames
NFrames = round(Vptr.FrameRate*Vptr.Duration);

%Create Video Writer Object
Wptr = VideoWriter('VIDEO_Enhance.mp4','MPEG-4');

Wptr.FrameRate = 10;
%Open the output video file
open(Wptr);
for i=1:230

Img = imadjust(Img,[0 0 0; 0.7 0.7 0.5],[]);

%Crop undesired portion
Img = Img(1:end,251:end,:);

%write to video file
writeVideo(Wptr,Img);

end
%Close the output video file
close(Wptr);

EXPLANATION:

In this example, the frame rate is set to 10 and Instead of reading all the frames(319), 230 frames are read starting from the first frame. Each frame is enhanced and a portion of it is cropped as well.

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.1  Upsampling using MATLAB built-in function
1.2  Upsampling in 1D
1.3  Upsampling in 2D or image matrix
2.
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

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 "IMAGE PROCESSING" page