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

One Dimensional Low pass , High Pass and band pass filtering

Consider a one dimensional signal in time domain.
For instance, generate cosine waves of different amplitudes and different frequencies and combine them to form a complicated signal.
Consider four cosines waves with frequencies 23, 18, 10 and 5 and amplitudes 1.2, 0.8, 1.1 and 2 respectively and combine them to generate a signal.

Eg.

Cosine wave with frequency 23 and amplitude 1.2.
 
Figure.1

Cosine wave with frequency 18 and amplitude 0.8
 
Figure.2
Cosine wave with frequency 10 and amplitude 1.1
 
Figure.3

Cosine wave with frequency 5 and amplitude 2.
Figure.4
MATLAB code:

%To generate a signal
%Input function A cos(2pift)
freq =[23 18 10 5];
Amp = [1.2 0.8 1.1 2];

fs = 100;
ts = 1/fs;
t = 0:ts:5;
xt1 = 0;
for Ind = 1:numel(freq)
Freq = freq(Ind);
A = Amp(Ind);
xt1 = xt1+A*cos(2*pi*Freq*t);
end

figure,plot(t,xt1);
 
Figure.5
Let’s make a little understanding of this signal in frequency domain.
i.e in Fourier domain.
Fourier transform is one of the various mathematical transformations known which is used to transform signals from time domain to frequency domain.
The main advantage of this transformation is it makes life easier for many problems when we deal a signal in frequency domain rather than time domain.

After fourier transform, the signal will look like the below figure.


MATLAB code:

f1 = linspace(-fs/2,fs/2,numel(xt1));
xf = fftshift(fft(xt1));
str = 'Frequency domain';
figure,plot(f1,abs(xf)),title(str);

 
Figure.6

EXPLANATION:

In the figure.6, there are 4 different colors to denote four different signals.
Due to symmetric property, we see two peaks of the same signal.

fftshift is useful for visualizing the Fourier transform with the zero-frequency component in the middle of the spectrum.

The yellow color peaks denote the frequency with 5
The blue color peaks denote the frequency with 10
The green color peaks denote the frequency with 18
And the red color denotes the frequency with 23.

From this, we can understand that the low frequency components are close to the centre of the plot and the high frequency components are away from the centre.


Now, lets try to separate each of the original 4 signals from the combined ones.

We will keep only one signal at a time and remove/filter the rest.

MATLAB code:

       Kernel_BP = abs(f1) < 8;
   BPF = Kernel_BP .* xf;
  
   figure,plot(f1,abs(BPF));


Figure.7

Figure.8





A kernel is used to obtain the essential frequency component alone.
This can be done by making the values one for the frequency range less than 8 and rest zero.

We have obtained the lowest frequency component and the kernel we designed is for low pass filtering. The value which we use to find the limit is the cut off frequency. Here 8 is the cut of frequency.

Now , let’s go back to time domain.

MATLAB code:

BP_t = ifft(ifftshift(BPF));
plot(t,real(BP_t));

Compare the figure.4 and figure.9, both have same frequency right!!

Figure.9

Now let’s try to design the kernel for filtering high frequency component.

High Pass filter:MATLAB code :

    
   Kernel_BP = abs(f1) > 20;
   BPF = Kernel_BP .* xf;
  
   figure,plot(f1,abs(BPF));
 
Figure.10

   
Figure.11

  BP_t = ifft(ifftshift(BPF));
  plot(t,real(BP_t));



Figure.12


We have obtained the same signal as in figure.1

We will try to filter the signal of frequency 18.
This is band pass filter.

Band Pass Filter:
MATLAB code:
   Kernel_BP = abs(f1) > 15 & abs(f1) < 20;
   BPF = Kernel_BP .* xf;
  
   figure,plot(f1,abs(BPF));

         
Figure.13
   BP_t = ifft(ifftshift(BPF));
   plot(t,real(BP_t));
 
Figure.14

Figure.15

This same type of filtering can be applied on noisy signals to remove undesired signals.

MATLAB code:

Noise = 2 * randn(size(t));
xt1 = xt1 + Noise;

Using this signal as input, we can apply low, high and band pass to remove noise and obtain the original signal(to some extent).

Wait for my next post on two dimensional low and high pass filtering. i.e on images in space domain.

like button Like "IMAGE PROCESSING" page

Optical Illusion – Circle

Let us define a circle and assign different color to each one of the four quadrants.

MATLAB CODE:

%Radius
rad=400;

[X,Y]=meshgrid(-rad:rad);
nh = X.^2+Y.^2 <= rad.^2;


color_cir= uint8([size(nh,1) size(nh,2) 3]);
nh =uint8(nh);

%Color Blue
color_cir(1:rad,1:rad,1) = nh(1:rad,1:rad)*10;
color_cir(1:rad,1:rad,2) = nh(1:rad,1:rad)*100;
color_cir(1:rad,1:rad,3) = nh(1:rad,1:rad)*180;

%color pink
color_cir(rad:rad*2,1:rad,1) = nh(rad:rad*2,1:rad)*180;
color_cir(rad:rad*2,1:rad,2) = nh(rad:rad*2,1:rad)*10;
color_cir(rad:rad*2,1:rad,3) = nh(rad:rad*2,1:rad)*100;

%color Green
color_cir(rad:rad*2,rad:rad*2,1) = nh(rad:rad*2,rad:rad*2)*100;
color_cir(rad:rad*2,rad:rad*2,2) = nh(rad:rad*2,rad:rad*2)*180;
color_cir(rad:rad*2,rad:rad*2,3) = nh(rad:rad*2,rad:rad*2)*10;

%Color Red
color_cir(1:rad,rad:rad*2,1) = nh(1:rad,rad:rad*2)*180;
color_cir(1:rad,rad:rad*2,2) = nh(1:rad,rad:rad*2)*10;
color_cir(1:rad,rad:rad*2,3) = nh(1:rad,rad:rad*2)*10;


imshow(color_cir);







Concentric Circles:
Colored:
MATLAB CODE:
clc
clear all
rad1 = 600;
[X,Y] = meshgrid(-rad1:rad1);

nh  =  X.^2+Y.^2  <=  rad1.^2;

color_cir =  uint8([size(nh,1) size(nh,2) 3]);
nh  = uint8(nh);

color_cir(1:size(nh,1),1:size(nh,2),1) = nh*10;
color_cir(1:size(nh,1),1:size(nh,2),2) = nh*1;
color_cir(1:size(nh,1),1:size(nh,2),3) = nh*2;




for rad = 550:-50:50
[X,Y] = meshgrid(-rad:rad);
diff = rad1-rad;
nh = X.^2+Y.^2  <= rad.^2;

nh  = uint8(nh);
Pos1 = diff:(rad+diff-1);
Pos2 = diff+rad:(rad*2)+diff-1;

%Color Red
color_cir(Pos1,Pos1,1) = nh(1:rad,1:rad)*100 + color_cir(Pos1,Pos1,1);
color_cir(Pos1,Pos1,2) = nh(1:rad,1:rad)*10 + color_cir(Pos1,Pos1,2);
color_cir(Pos1,Pos1,3) = nh(1:rad,1:rad)*10 + color_cir(Pos1,Pos1,3);

%Color Green
color_cir(Pos1,Pos2,1) = nh(1:rad,rad:rad*2-1)*10 + color_cir(Pos1,Pos2,1);
color_cir(Pos1,Pos2,2) = nh(1:rad,rad:rad*2-1)*100 + color_cir(Pos1,Pos2,2);
color_cir(Pos1,Pos2,3) = nh(1:rad,rad:rad*2-1)*10 + color_cir(Pos1,Pos2,3);


%Color Blue
color_cir(Pos2,Pos1,1) = nh(rad:rad*2-1,1:rad)*10 + color_cir(Pos2,Pos1,1);
color_cir(Pos2,Pos1,2) = nh(rad:rad*2-1,1:rad)*10 + color_cir(Pos2,Pos1,2);
color_cir(Pos2,Pos1,3) = nh(rad:rad*2-1,1:rad)*100 + color_cir(Pos2,Pos1,3);

%Color yellow
color_cir(Pos2,Pos2,1) = nh(rad:rad*2-1,rad:rad*2-1)*150 + color_cir(Pos2,Pos2,1);
color_cir(Pos2,Pos2,2) = nh(rad:rad*2-1,rad:rad*2-1)*100 + color_cir(Pos2,Pos2,2);
color_cir(Pos2,Pos2,3) = nh(rad:rad*2-1,rad:rad*2-1)*10 + color_cir(Pos2,Pos2,3);

end




figure,imshow(color_cir);






EXPLANATION:
1.      Draw a circle of radius 600.
2.      Fill the circle with black color.
3.      Draw another circle of radius 550 and fill the first quadrant will green, second quadrant with red, third quadrant with blue and the fourth quadrant with yellow color.
4.      Place the smaller circle (radius 550) on the bigger circle (radius 600).
5.      Draw another circle of radius 500 and repeat the same process till the radius of the smaller circle is 50.

Black and White:
MATLAB CODE:
clc
clear all
rad1 = 1250;
[X,Y] = meshgrid(-rad1:rad1);

nh = X.^2+Y.^2  <=  rad1.^2;

color_cir =  double([size(nh,1) size(nh,2) 3]);
nh  = double(nh);

color_cir(1:size(nh,1),1:size(nh,2),1) = nh*1;
color_cir(1:size(nh,1),1:size(nh,2),2) = nh*1;
color_cir(1:size(nh,1),1:size(nh,2),3) = nh*1;



c1 = 0;
c2 = 255;

for rad = 1200:-50:50

[X,Y] = meshgrid(-rad:rad);
diff = rad1-rad;
nh = X.^2+Y.^2 <= rad.^2;

nh  = double(nh);
Pos1 = diff:(rad+diff-1);
Pos2 = diff+rad:(rad*2)+diff-1;

%Second Quadrant
color_cir(Pos1,Pos1,1) = nh(1:rad,1:rad)*c1 + color_cir(Pos1,Pos1,1);
color_cir(Pos1,Pos1,2) = nh(1:rad,1:rad)*c1 + color_cir(Pos1,Pos1,2);
color_cir(Pos1,Pos1,3) = nh(1:rad,1:rad)*c1 + color_cir(Pos1,Pos1,3);

%First Quadrant
color_cir(Pos1,Pos2,1) = nh(1:rad,rad:rad*2-1)*c2 + color_cir(Pos1,Pos2,1);
color_cir(Pos1,Pos2,2) = nh(1:rad,rad:rad*2-1)*c2 + color_cir(Pos1,Pos2,2);
color_cir(Pos1,Pos2,3) = nh(1:rad,rad:rad*2-1)*c2 + color_cir(Pos1,Pos2,3);


%Third Quadrant
color_cir(Pos2,Pos1,1) = nh(rad:rad*2-1,1:rad)*c2 + color_cir(Pos2,Pos1,1);
color_cir(Pos2,Pos1,2) = nh(rad:rad*2-1,1:rad)*c2 + color_cir(Pos2,Pos1,2);
color_cir(Pos2,Pos1,3) = nh(rad:rad*2-1,1:rad)*c2 + color_cir(Pos2,Pos1,3);

%Fourth Quadrant
color_cir(Pos2,Pos2,1) = nh(rad:rad*2-1,rad:rad*2-1)*c1 + color_cir(Pos2,Pos2,1);
color_cir(Pos2,Pos2,2) = nh(rad:rad*2-1,rad:rad*2-1)*c1 + color_cir(Pos2,Pos2,2);
color_cir(Pos2,Pos2,3) = nh(rad:rad*2-1,rad:rad*2-1)*c1 + color_cir(Pos2,Pos2,3);

if(c2 > 1)
    c1 = 255;
    c2 = -255;
else
    c1 = -255;
    c2 = 255;
end

end



color_cir = uint8(color_cir);
figure,imshow(color_cir);




EXPLANATION:
1.      Draw a circle of radius 1250.
2.      Fill it with black color.
3.      Draw another circle with radius 1200.
4.      Fill second and fourth quadrant of the smaller circle with white color.
5.      Fill First and Third quadrant of the smaller circle with black color.
6.      Place the smaller circle on the bigger circle.
7.      Define another circle of radius 1150.
8.      Fill second and fourth quadrant of the smaller circle with black color.
9.      Fill First and Third quadrant of the smaller circle with white color.
10.  Now place this circle on the bigger one. Repeat this process with smaller circles and by interchanging black and white color on the quadrants for each circle.

Concentric circle - 2:

MATLAB CODE:

%Define the matrix
[x,y] = meshgrid(-600:600);

%Preallocate
ccircle = zeros(size(x));
for i = 500:-10:1
   
    Tmp = x.^2 + y.^2 <= i*i;
   
    %Find the gradient of the image
    [Gmag,Gdir] = imgradient(Tmp);
   
    ccircle = ccircle + Gmag;
end

figure,imshow(ccircle);





like button Like "IMAGE PROCESSING" page

Digital Image Water Marking – Part 1


Digital image watermarking is the method in which data is embedded in a multimedia file such as an image or a video, so as to verify the credibility of the content or the identity of the owner.

MATLAB CODE:
%Digital Image Watermarking

%Unmasked Image f
f = imread('angel.jpg');
figure,imshow(f);title('Unmasked Image');
 
Unmasked Image
%Watermark
w = imread('watermark_sym.jpg');
figure,imshow(w);title('Watermark');







%Visibility of the watermark
alpha=0.5;

Fw = (1-alpha)*f + alpha.*w;
figure,imshow(Fw);title('Watermarked Image-Visibilty:0.5');



Explanation:
1.      Read the image to be watermarked. In my case it ‘s ‘angel.jpg’
2.      Read the watermark. Make sure the image size and the watermark size are same.
3.      Adjust the alpha value based on your requirement. Alpha can be between 0 and 1. If alpha is less than 0.5 the watermark will be less visible and if the alpha is more than 0.5, the watermark will be clearly visible.



MATLAB CODE:

%Read the Blog logo
Logo = imread('Dwatermark2.jpg');
figure,imshow(Logo);title('Blog Logo');


OIm = f;
%Visibility control
alpha = 0.5;

[m, n,~]=size(Logo);

%choose a portion of the Image
Sz = [495 927];

%Apply watermark
OIm(Sz(1):Sz(1)+m-1,Sz(2):Sz(2)+n-1,:)=(1-alpha)*OIm(Sz(1):Sz(1)+m-1,Sz(2):Sz(2)+n-1,:) + alpha.*Logo;
figure,imshow(OIm);title('Watermark in the centre');



EXPLANATION:
1.      Read the blog logo.
2.      Note: The size of the input image and the logo are not same.
3.      Choose a portion in the input image and apply watermark.



LSB Digital Watermarking
In this method, the watermark is hidden in the input image.
Consider a pixel from the input image; say 249 and a pixel from the watermark say, 155.
Now hide the pixel value of the watermark in the two least significant bits of the input image.
1.      The binary representation of 249 is 11111001
2.      The binary representation of 155 is 10011011
3.      Make the two least significant bits of input image value 249 to zeros i.e. 11111000
4.      Find the two Most significant bits of the watermark value 155 i.e. 10
5.      Now place the two most significant bits of the watermark at the two least significant bits of the input image.
6.      11111000 + 00000010 = 11111010 i.e.  250


MATLAB CODE:
%LSB watermarked Image
Fw1 = 4*(f/4) + w/64;
figure,imshow(Fw1);title('LSB watermarked Image');




How to retrieve the watermark:
Method 1:
Subtract the input image (without watermark) from the watermarked Image.
%Retrieve the Watermark
DMark = (Fw1-f)*64;
figure,imshow(DMark);title('Watermark extracted from the image');



Method 2:
Retrieve the two least significant bits of the image
%Retrieve the Watermark
DMark2 = rem(Fw1,4)*64;
figure,imshow(DMark2);title('Watermark extracted from the image');

We can extend the above techniques for steganography or image coding. In the next upcoming post I will share about how to validate the digital watermark.





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