Montag, 2. Juni 2014

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;




forrad = 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;

forrad = 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);





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.