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

Lee filter using MATLAB


Lee filter for speckle reduction has been realized in MATLAB. For explanation check:
https://www.imageeprocessing.com/2014/08/lee-filter.html

MATLAB code:

%Read an Image
I = imread('coins.png');
%Add multiplicative noise to the image
J = imnoise(I,'speckle',0.01);
%Apply Lee filter
K = Lee_filter(I,J,[5 5]);
figure,subplot(121),imshow(J);title('Noisy Image');
subplot(122),imshow(uint8(K));title('Despeckled Image');




MATLAB Function:


function Y = Lee_filter(R,E,sz)
%R is the Reference Image
%E is the Error or Noisy Image
%K is the Kernel or Window
%Y is the Output Image

% Y = mean(K)+W*(C-mean(K);
% W = variance(K)/(variance(K)+variance(R))

%Define the type
R = double(R);
E = double(E);


%Preallocate the Output Matrix
Y = zeros(size(R));
mn = round((sz-1)/2);
Tot = sz(1,1)*sz(1,2);
EImg = padarray(E,mn);

%Variance of the reference Image
Rvar = var(R(:));

Indx = floor(median(1:Tot));
for i = 1:size(R,1)
    for j = 1:size(R,2)
        K = EImg(i:i+sz(1,1)-1,j:j+sz(1,2)-1);
        varK = var(K(:));
        meanK = mean(K(:));
        W = varK./(varK+Rvar);
       
        Y(i,j) = meanK + W * (K(Indx)-meanK);
      
    end
end

end

 EXPLANATION:

Save the function Lee-filter.m in separate file
like button Like "IMAGE PROCESSING" page

Frost Filter


Based on the local statistics in a sliding window, the frost filter works on preserving the edges while suppressing the noise. The Damping factor which is an exponential damping is the key factor in controlling the smoothness of the filter. When damping factor is small, the image tends to be smooth.

ALGORITHM:









MATLAB CODE:
%FROST FILTER

%LENNA IMAGE - SPECKLE REDUCTION

% INPUT PARAMETERS - NOISY IMAGE, DAMPING FACTOR, WINDOW SIZE
load('Noise_image1.mat')
ima_nse = double(ima_nse);

%Damping factor
Damp_fact = 1;

%window size
sz = [5,5];

%Preallocate the Output Matrix
ima_fi = zeros(size(ima_nse));
if(mod(sz,2)~=1)
    sz=sz+~mod(sz,2);
end

mn = round((sz-1)/2);

%Padding with zeros around the border
EImg = padarray(ima_nse,mn);


[x,y]= meshgrid(-mn(1,1):mn(1,1),-mn(1,2):mn(1,2));
S = sqrt(x.^2+y.^2);

for i = 1:size(ima_nse,1)
    for j = 1:size(ima_nse,2)
        %Local Window
        K = EImg(i:i+sz(1,1)-1,j:j+sz(1,2)-1);
       
        %Mean value of the pixels in the local window
        meanV = mean(K(:));
       
        %variance of the pixels in the local window
        varV = var(K(:),1);
       
        %Weight for each pixel in the local window
        B =  Damp_fact*(varV/(meanV*meanV));
        Weigh = exp(-S.*B);
       
        % Filtering
        ima_fi(i,j) = sum(K(:).*Weigh(:))./sum(Weigh(:));
       
    end
end

figure,subplot(121),imagesc(ima_nse);colormap(gray);title('Original Image');
subplot(122),imagesc(ima_fi);colormap(gray);title('After Despeckling - Frost Filter');





EXPLANATION:
Here in the example given above, the damping factor = 1 and the size is 5x5.

Let’s consider another example, where damping factor =1 and the local window size is 11x11
The image becomes smooth as well as the edges.


This is the parameter, S that has the distance from centre of the pixel to its neighbours in the local window. See that the distance at the centre for the centre pixel is zero, while the distance from the centre to the adjacent pixel is 1 and the increase in value based on the distance between the pixels is evident in the figure.


While in this example, where damping factor = 3 and the local window size is 11x11, the edges are preserved. By controlling the damping factor, a trade-off between the smoothness and preservation of the edges can be done.



like button Like "IMAGE PROCESSING" page

Part 3: Median Filter - RGB Image

MATLAB CODE:

clear all

%READ THE RGB IMAGE
I = imread('peppers.png');

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
modifyA=padarray(A,[floor(M/2),floor(N/2)]);

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 howto 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. Learn howto pad with zeros using MATLAB built_in function padarray.
·        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 button Like "IMAGE PROCESSING" page

Part 2: 2D Median Filter With Different Window Size

MATLAB CODE:

clear all

%READ AN IMAGE
I = imread('zebra.jpg');
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
modifyA=padarray(A,[floor(M/2),floor(N/2)]);
figure,imshow(uint8(modifyA)); title('PADDED WITH ZEROS');

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. Learn howto pad with zeros using MATLAB built_in function padarray.
·        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 button 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.


To read a video file:

Vptr = VideoReader('VIDE01.mp4')



‘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:
Vptr = VideoReader('VIDE01.mp4');

%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
%Read each frame
Img = readFrame(Vptr);

%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
Vptr = VideoReader('VIDE01.mp4');
NFrames = round(Vptr.FrameRate*Vptr.Duration);

Jump_ptr = 27;
N = 1;

%To display the Images
for i=1:NFrames

Img = readFrame(Vptr);
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
Vptr = VideoReader('VIDE01.mp4');
Wptr = VideoWriter('VIDE02.mp4','MPEG-4');
Wptr.FrameRate=10;
open(Wptr);
for i=1:120
Img = readFrame(Vptr);
writeVideo(Wptr,Img);

end
close(Wptr);

EXPLANATION:

Create the video reader object using ‘VideoReader’ for ‘VIDEO1.mp4’
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
%Create video Reader object
Vptr = VideoReader('VIDE01.mp4');
%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
%Read from video file
Img = readFrame(Vptr);
%Add noise to the image
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
%Adjust the Image intensity
%Crop the Image
%Read 250 Frames

Vptr = VideoReader('VIDE01.mp4');
%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
    %Read from video file
    Img = readFrame(Vptr);
   
    %Adjust the image intensity
    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 button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com