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

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

      MATLAB Output Functions

      The output to the screen using ‘displaymatlab function.

                  To display the result with new line and more than 1 argument or variable, I used the cell format to display the variables.
      In the first example, I used cell format to display the variables.  The special character ‘,’ is used as a concatenation operator and the ‘;’ is used as a new line character.

      %Simple Example to demonstrate display function
      length=10;
      breadth=30.4;
      shape=' rectangle';

      area=length*breadth;

      result={'Length =',length; 'Breadth =',breadth;'Shape:',shape; 'Area of the rectangle =',area};
      display(result);



      Here I converted the single line into a string and then used ‘;’ for a new line character.

      %Lets make use strings here

      result={strcat('length of the',shape,'= ',num2str(length)); strcat('Breadth of the',shape,'=',num2str(breadth)); strcat('Area of the',shape,'=',num2str(area))};
      display(result);






      %sprintf

      result=sprintf('Length of the %s = %.2f \nBreadth of the %s = %.2f \nArea of the %s = %.2f',shape,length,shape,breadth,shape,area);
      display(result);




      Sprintf is used to format the text. It is similar to the ‘C’ function ‘printf’. For strings, ‘%s’ is used. For decimals,’%d’ is used.
      And for new line same as in ‘C’ we use ‘\n’ here.

      like button Like "IMAGE PROCESSING" page

      MATLAB CODE:Local Histogram equalization


      For every pixel, based on the neighbor hood value the histogram equalization is done. Here I used 3 by 3 window matrix for explanation. By changing the window matrix size, the histogram equalization can be enhanced. By changing the values of M and N the window size can be changed in the code given below.

      Steps to be performed:





      MATLAB CODE:


      A=imread('tire.tif');
      figure,imshow(A);
      Img=A;
           
        
      %WINDOW SIZE
      M=10;
      N=20;


      mid_val=round((M*N)/2);

      %FIND THE NUMBER OF ROWS AND COLUMNS TO BE PADDED WITH ZERO
      in=0;
      for i=1:M
          for j=1:N
              in=in+1;
              if(in==mid_val)
                  PadM=i-1;
                  PadN=j-1;
                  break;
              end
          end
      end
      %PADDING THE IMAGE WITH ZERO ON ALL SIDES
      B=padarray(A,[PadM,PadN]);

      for i= 1:size(B,1)-((PadM*2)+1)
          
          for j=1:size(B,2)-((PadN*2)+1)
              cdf=zeros(256,1);
              inc=1;
              for x=1:M
                  for y=1:N
        %FIND THE MIDDLE ELEMENT IN THE WINDOW          
                      if(inc==mid_val)
                          ele=B(i+x-1,j+y-1)+1;
                      end
                          pos=B(i+x-1,j+y-1)+1;
                          cdf(pos)=cdf(pos)+1;
                         inc=inc+1;
                  end
              end
                            
              %COMPUTE THE CDF FOR THE VALUES IN THE WINDOW
              for l=2:256
                  cdf(l)=cdf(l)+cdf(l-1);
              end
                  Img(i,j)=round(cdf(ele)/(M*N)*255);
           end
      end
      figure,imshow(Img);
      figure,
      subplot(2,1,1);title('Before Local Histogram Equalization'); imhist(A);
      subplot(2,1,2);title('After Local Histogram Equalization'); imhist(Img);



















      After Local Histogram Equalization

      Histogram equalization of an Image:
       http://angeljohnsy.blogspot.com/2011/04/matlab-code-histogram-equalization.html

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