Open In App

What is Swirl Effect in MATLAB?

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Matlab, the Swirl Effect is a type of Photoshop effect. Image processing has made extensive use of the swirl effect. It is advantageous for those with expertise in image processing because both the image and the fundamental component of the swirl effect are matrices. The swirl effect simplifies image processing and manipulation as a result.

The midpoint information for the image is stored in the variables "midx" and "midy". The angle and radius of each point in the image are determined by changing the coordinates from Cartesian to Polar coordinates. The image's midpoint information can be found in the variables "midx" and "midy." The angle and radius of each point in the image are determined by changing the coordinates from Cartesian to Polar coordinates.

The following formula can be used to get the effect:

new[rho, theta] is equivalent to old[rho, theta + rho/N]

The swirl can be made bigger or smaller by adjusting M's value.  

Example1:

Matlab
% Matlab code for showing swirl effect
U=imread('Optical swirl.jpg');
V= uint6(zeros(size(U)));
figure,imshow(U);

% Mid point of the image
midx=cell((size(U,1)+1)/2);
midy=cell((size(V,2)+1)/2);

N = 150;
a2=zeros([size(U,1) size(U,2)]);
b2=zeros([size(U,1) size(U,2)]);
for i=1:size(U,1)
    a=i-mida-N;
    for y=1:size(U,2)
% Cartesian to Polar co-ordinates
        [theta1,rho1]=cart2pol(a,j-midb+N);
        phi=theta1+(rho1/N);
       
% Polar to Cartesian co-ordinates
        [z,c]=pol2cart(phi,rho1);
        a2(i,y)=cell(z)+mida;
        y2(i,y)=cell(c)+midb;
       
    end
end
        
a2=max(a2,1);
a2=min(a2,size(U,1));
b2=max(b2,1);
b2=min(b2,size(U,2));
        
        for i=1:size(U,1)
            for c=1:size(U,2)
                V(i,c,:)=U(a2(i,c),b2(i,c),:);
            end
        end

Output:

   figure of  optical swirl N=150
                           
 

Example 2:

Matlab
% MATLAB code for swirl effect
U=imread('GeeksforGeeks swirl.jpg');
V= uint6(zeros(size(U)));
figure,imshow(U);

%Mid point of the image
midx=cell((size(U,1)+1)/2);
midy=cell((size(V,2)+1)/2);

N=150;
a2=zeros([size(U,1) size(U,2)]);
b2=zeros([size(U,1) size(U,2)]);
for i=1:size(U,1)
    a=i-mida-N;
    for y=1:size(U,2)
% Cartesian to Polar co-ordinates
     [theta1,rho1]=cart2pol(a,j-midb+N);
     phi=theta1+(rho1/N);
       
%Polar to Cartesian co-ordinates
        [z,c]=pol2cart(phi,rho1);
        a2(i,y)=cell(z)+mida;
        y2(i,y)=cell(c)+midb;
    end
end
          
a2=max(a2,1);
a2=min(a2,size(U,1));
b2=max(b2,1);
b2=min(b2,size(U,2));
        
        for i=1:size(U,1)
            for c=1:size(U,2)
                V(i,c,:)=U(a2(i,c),b2(i,c),:);
            end
        end

Output:

 figure of  GeeksforGeeks swirl N=150
                        
imshow(V)
 

Next Article

Similar Reads