Follow by Email

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

Recent Posts

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

Some Random effects

  • Negative Image
Subtract 255 from the RGB image, to obtain negative image.
MATLAB CODE:
%Here the  I am subtracting 255 from R,G and B.
A=imread('person.jpg');
imshow(255-A);

Original Image
Negative Image







  • Sine Wave

    First find the mid point of the RGB image.
    Convert the points from Cartesian to polar co_ordinates
    Find the sine value for the angle 'theta'
    Again convert the points to Cartesian co_ordinates

    SAMPLE CODE:
    x2=zeros([size(A,1) size(A,2)]);
    y2=zeros([size(A,1) size(A,2)]);
    for i=1:size(A,1)
        x=i-midx;
        for j=1:size(A,2)
    %Cartesian to Polar co-ordinates
               [theta1,rho1]=cart2pol(i,j-midy);
        
          
                  
            phi=sin(theta1);
          
    %Polar to Cartesian co-ordinates
            [l,m]=pol2cart(phi,rho1);
            x2(i,j)=ceil(l)+midx;
            y2(i,j)=ceil(m)+midy;
          
        end
    end

    Sine wave


Original Image

like button Like "IMAGE PROCESSING" page

Tiling effect in MATLAB


Tiling effect

original Image
          Let’s make some random rectangles from the image. 

MATLAB CODE:

A=imread('tulip2.jpg');
C=uint8(zeros(size(A)));
i=1; j=1;
col=0;
row=0;
m=1;n=1;
%change the rsz and csz value to obtain pieces in different sizes
rsz=15;
csz=36;

%change the tsz_row and tsz_col values to make some gaps between the pieces.

tsz_row=3;
tsz_col=5;



while ( i < size(A,1))
%check whether i is less than size of (A,1)


while(m+i+row < size(A,1)&& n+j+col < size(A,2))

    
        %size of the tiles varies depending upon the random number
        C(m+i:m+i+row,n+j:n+j+col,:)=A(i:i+row,j:j+col,:);
      
        m=ceil(rand(1)*tsz_row);
        n=ceil(rand(1)*tsz_col);
        col=ceil(rand(1)*csz);
        row=ceil(rand(1)*rsz);
        j=j+col;
       
   
    end
     i=i+row;
     j=1;
end
figure,imshow(C);
With small gaps


With Large gaps
Subscribe to 'Image processing' to get the complete code.


like button Like "IMAGE PROCESSING" page

Cone effect in MATLAB

The image shrinks towards the center, forming a cone effect. Here, I first found the mid point of the image, since the image expands from the center. Then the angle and the radius of the points are found by converting into polar co-ordinates from Cartesian.  The square root (radius * K) is obtained, where K  can be changed to make the image size differ.
                                        Again convert from polar to Cartesian and display the final image.
For more fun, try this effect with your own photography image .




MATLAB CODE:


A=imread('square.jpg');
B=uint8(zeros(size(A)));
figure,imshow(A);


%FIND THE MID VALUES
midx=ceil((size(A,1)+1)/2);
midy=ceil((size(A,2)+1)/2);


%CHANGE THE VALUE OF 'K' 
K=180;
x2=zeros([size(A,1) size(A,2)]);
y2=zeros([size(A,1) size(A,2)]);


%COMPUTATION TO GET CONE EFFECT
for i=1:size(A,1)
    x=i-midx;
    for j=1:size(A,2)
           [theta,rho]=cart2pol(x,j-midy);
           sqtrho=sqrt(rho*K);
           [l,m]=pol2cart(theta,sqtrho);
           x2(i,j)=ceil(l)+midx;
           y2(i,j)=ceil(m)+midy;
    end
end
       
        % IF THE ARRAY CONTAINS VALUES LESS THAN 1 REPLACE IT WITH 1 AND
        % IF GREATER THAN 1 REPLACE WITH SIZE OF THE ARRAY
        x2(x2<1)=1;
        x2(x2>size(A,1))=size(A,1);
     
        y2(y2<1)=1;
        y2(y2>size(A,2))=size(A,2);
     
        for i=1:size(A,1)
            for j=1:size(A,2)
                B(i,j,:)=A(x2(i,j),y2(i,j),:);
            end
        end
     
     figure,   imshow(B);





K=100
K=180

SEE ALSO : Oil Painting  Swirl effect   Glassy effect   Tiling effect   Paint application
  Reference:
FOR FUN:  create your own Photoshop effects
    like button Like "IMAGE PROCESSING" page

    Glassy effect in MATLAB

    To obtain a glassy effect, define a window of size m by n. Obtain a random pixel value from the window matrix and add it with the current position value.


    MATLAB CODE:
    A=imread('aish.jpg');
    %WINDOW SIZE
    m=6;                                                                  
    n=7;
    Image=uint8(zeros([size(A,1)-m,size(A,2)-n,3]));

    for i=1:size(A,1)-m
        for j=1:size(A,2)-n

            mymask=A(i:i+m-1,j:j+n-1,:);
    %Select a pixel value from the neighborhood.
    x2=ceil(rand(1)*m);
    y2=ceil(rand(1)*n);
       
            Image(i,j,:)=mymask(x2,y2,:);
        end
    end

    figure,imshow(Image);
        We can change the matrix size and see the difference in the result.

    SEE ALSO : Oil Painting  Swirl effect   Cone effect     Tiling effect   Paint application

      Reference:
    FOR FUN:  Create your own Photoshop effects
      like button Like "IMAGE PROCESSING" page

      Oil Painting in MATLAB

      To obtain a painting-like effect , define a small window matrix of size m by n. Copy the original image pixel values into the matrix and find the histogram of each value. Find the maximum occurring pixel value and replace the current position with the maximum occurrence value.

      Original Image

      Oil Paint Effect

      MATLAB CODE:
      A=imread('aish.jpg');

      %Define the matrix size of  your convience.
      m=5;
      n=6;
      Image=uint8(zeros([size(A,1)-m,size(A,2)-n,3]));
      %Calculate the histogram for each RGB value.
      for v=1:3

      for i=1:size(A,1)-m
          for j=1:size(A,2)-n
              mymask=A(i:i+m-1,j:j+n-1,v);
              h=zeros(1,256);
              for x=1:(m*n)
                  h(mymask(x)+1)=h(mymask(x)+1)+1;
              end
        %Maximum occurring value and the position is obtained
              [maxvalue,pos]=max(h);
              Image(i,j,v)=pos-1;
          end
      end
      end
      figure,imshow(Image);

      SEE ALSO : Swirl effect  Cone effect    Glassy effect   Tiling effect   Paint application(coloring)
        Reference:
      FOR FUN:  Create your own Photoshop effects
      like button Like "IMAGE PROCESSING" page

      Swirl effect in MATLAB

      After reading a couple of image processing books, I tried to implement some Photoshop effects in MATLAB.  First, I tried to implement the twirl effect in MATLAB.


      The mid point of the image is stored in the variables ‘midx’ and ‘midy’.
      The angle and the radius for each point in the image are found by converting the co-ordinates from Cartesian to polar co-ordinates. 

       The effect can be obtained by using the following formula: 
      new[rho , theta] = old[rho , theta + rho /K] .

      By changing the value of K, the swirl can be increased or decreased.


      For more fun, try this effect with your own photography image.

      Additional Information: Learn to make your own Photoshop effects so that you can code your own effects.

      For beginners in photoshop: Read photoshop for dummies .





      MATLAB CODE:


      A=imread('flower2.jpg');

      B=uint8(zeros(size(A)));
      figure,imshow(A);

      %Mid point of the image
      midx=ceil((size(A,1)+1)/2);
      midy=ceil((size(A,2)+1)/2);
      K=150
      K=100;
      x2=zeros([size(A,1) size(A,2)]);
      y2=zeros([size(A,1) size(A,2)]);
      for i=1:size(A,1)
          x=i-midx-K;
          for j=1:size(A,2)
      %Cartesian to Polar co-ordinates
                 [theta1,rho1]=cart2pol(x,j-midy+K);
           
             
                     
              phi=theta1+(rho1/K);
             
      %Polar to Cartesian co-ordinates
              [l,m]=pol2cart(phi,rho1);
              x2(i,j)=ceil(l)+midx;
              y2(i,j)=ceil(m)+midy;
             
          end
      end
                
        %The result may produce value lesser than 1 or greater than the image size.


      x2=max(x2,1);
      x2=min(x2,size(A,1));

      y2=max(y2,1);
      y2=min(y2,size(A,2));
              
             
              for i=1:size(A,1)
                  for j=1:size(A,2)
                      B(i,j,:)=A(x2(i,j),y2(i,j),:);
                  end
              end
             
           figure,   imshow(B);
      K=100







           Reference:


      FOR FUN:  Create your own Photoshop effects
      like button Like "IMAGE PROCESSING" page
      Next Post Home
      Related Posts Plugin for WordPress, Blogger...
      Google ping Hypersmash.com