# 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

### Optical Illusion – Circle

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

MATLAB CODE:

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

%Color Blue

%color pink

%color Green

%Color Red

imshow(color_cir);

Concentric Circles:
Colored:
MATLAB CODE:
clc
clear all

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;

nh  = uint8(nh);

%Color Red

%Color Green

%Color Blue

%Color yellow

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

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;

nh  = double(nh);

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

ccircle = ccircle + Gmag;
end

figure,imshow(ccircle);

Like "IMAGE PROCESSING" page

### I can't sleep Illusion

In this illusion, I used slanted lines in two different directions  to highlight the foreground from the background.

First, the background is formed with slanting lines in one direction.

Steps to form background with slanting lines:
1.       Get the size of the foreground image. For instance, MXN =5X8
2.       Initialize a matrix of size 1XN=1X8.
3.       Generate values for the matrix.
Eg. [10  10 250 250 10 10 250 250]ie [BBWWBBWW] B-Black, W-White
4.       Initialize another matrix of size MXN. In the first row copy the random values.
5.       For the second row perform circshift. By circshift the pixel at the first column will be moved to the last column and the remaining pixels are shifted to left by one.Eg [10 250 250 10 10 250 250 10].
Now the colors will be [BWWBBWWB].
6.       Continue to perform circshift  for all the rows.  Now the slanting lines are formed.
10    10   250   250    10    10   250   250
10   250   250    10    10   250   250    10
250   250    10    10   250   250    10    10
250    10    10   250   250    10    10   250
10    10   250   250    10    10   250   250

MATLAB CODE:

m=size(TImg,1);
n=size(TImg,2);

if(mod(n,2)~=0)
n=n-mod(n,2);
end
if(mod(n,4)~=0)
n=n-mod(n,4);
end
BImg=uint8(zeros([1 n 3]));
flag=0;
%Values for first column (bbwwbbwwbbww)
for i=1:2:n
if(flag==1)
BImg(1,i:i+1,:)=10;
flag=0;
else
BImg(1,i:i+1,:)=240;
flag=1;
end
end
%Initialization
A=uint8(zeros([m size(BImg,2) 3]));
Arot=uint8(zeros([m size(BImg,2) 3]));
SImg=uint8(zeros([m size(BImg,2) 3]));
FImg=uint8(zeros([m size(BImg,2) 3]));
A(1,:,:)=BImg(1,:,:);

%Slanting lines
for i=2:m
BImg=circshift(BImg,[1,-1]);
A(i,:,:)=BImg(1,:,:);
end

Steps to perform on foreground Image:
1.       Rotate the background image from left to right.
2.       Convert the foreground image into binary image and negate it. Then perform image multiplication with the rotated background image.

MATLAB CODE:
%Flip the image from left to right
Arot(:,:,1)=fliplr(A(:,:,1));
Arot(:,:,2)=fliplr(A(:,:,2));
Arot(:,:,3)=fliplr(A(:,:,3));
%Foreground
BImg=uint8(~im2bw(TImg));
BImg=imresize(BImg,[size(Arot,1) size(Arot,2)]);
SImg(:,:,1)=BImg.*Arot(:,:,1);
SImg(:,:,2)=BImg.*Arot(:,:,2);
SImg(:,:,3)=BImg.*Arot(:,:,3);
SImg(SImg==0)=1;

1.       Multiply the background image with the binary foreground image to obtain the mask.
MATLAB CODE:
BImg1=uint8(im2bw(TImg));
BImg1=imresize(BImg1,[size(Arot,1) size(Arot,2)]);
FImg(:,:,1)=BImg1.*A(:,:,1);
FImg(:,:,2)=BImg1.*A(:,:,2);
FImg(:,:,3)=BImg1.*A(:,:,3);
FImg(FImg==0)=1;

Combine Foreground Image and the background Image mask.

MATLAB CODE:
Zig=FImg.*SImg;
figure,imshow(Zig);

GUESS THE IMAGE:

Check this link for more optical Illusion Images: Images from Blog

Like "IMAGE PROCESSING" page

### Black and White-Optical illusion

Today, Some of my friends in Facebook posted black and white optical illusion photo saying 'ITS FREAKING AWESOME'.
After seeing the image, I found it's actually black and white illusion. Yes, convert the image to binary and do logical not of the image. When you look or concentrate at one point on the image ,maybe in center you can see a logical not of the image (i.e) White pixels will be black and black pixels will be white.

The most interesting thing is, you can try with your own photo.

MATLAB CODE:

B=uint8(zeros(size(A)));
A=~im2bw(A);
 Logical Not on Binary Image

for i=1:size(A,1)
for j=1:size(A,2)
if(A(i,j)==1)
B(i,j,:)=255;
end
end
end

Stare at the dots on image for 20 to 30 secs. Close your eyes or look up towards the ceiling.
You can see a binary image. i.e im2bw(A);

To draw the dots on the image:

C=uint8(ones(4 ,4, 3));
C(:,:,1)=C(:,:,1)*200;
C(:,:,2)=C(:,:,2)*0;
C(:,:,3)=C(:,:,3)*250;
%Find the mid point of the image and draw 3 dots.

midx=round(size(B,1)/2);
midy=round(size(B,2)/2)+30;
for i=1:size(C,1)
for j=1:size(C,2)
B(midx+i+5,midy+j,:)=C(i,j,:);
B(midx+i-5,midy+j,:)=C(i,j,:);
B(midx+i+15,midy+j,:)=C(i,j,:);
end
end

Check this Illusion also:I cant sleep Illusion
Check this link for more optical Illusion Images: Images from Blog

Like "IMAGE PROCESSING" page