Create Indian Flag using matlab



A coloured image is represented in a 3-Dimensional matrix during digital image processing. Images can be represented using a variety of colour models, The most popular model is RGB model. The RGB model typically represents an image. Red, Green, and Blue make up the first, second, and third channels of the matrix, respectively.

 Steps to create the tri-color Indian Flag:

Step 1:

First we need to create a blank 3D matrix filled by white color with a length to breadth ratio of 3:2 as per the Indian flag code 2002.

Here we choose the dimension of the as 3*n:2*n, where we can choose the n as any integer value as per our requirement.

  • For color image 3 channels are required, so 3 represents RGB (Red Green Blue) channel.  n=300; 
    flag=uint8(zeros(2*n, 3*n, 3));
  • Divide the matrix to three parts and assign the saffron, white and green color respectively.
  • To make Ashok chakra, first we need to create the circle followed by the spokes. To create the circle we need to understand the equation of distance between two given coordinates.
    (distance)2=(x2-x1)2+(y2-y1)2
    (x1, y1) and (x2, y2) are the two given coordinates.
  • Using the equation, The circle of Ashok chakra can be made. Center coordinates of the matrix and circle is (2*n/2, 3*n/2). To draw circle inner radius is (2*n/6)-5 and outer radius is (2*n/6)-2.
  • Angle between two adjacent spokes of Ashok chakra is (360/w)=15o, where w(number of spokes=24). atand is a MATLAB function used to find the angle. 
The matlab code as follows: 
 

%% MATLAB code to create Indian flag

% initialising a zero matrix as per the ratio of length to breadth of
% the flag i.e. 3:2

n=300;  % Provide the dimension you want to create the flag


flag=uint8(zeros(2*n, 3*n, 3));
flag(:, :, :)=255;
%Saffron Color
flag(1:round(2*n/3), :, 1)=255;
flag(1:round(2*n/3), :, 2)=153;
flag(1:round(2*n/3), :, 3)=51;

%Green Color
flag(2*round(2*n/3):2*n, :, 1)=19;
flag(2*round(2*n/3):2*n, :, 2)=136;
flag(2*round(2*n/3):2*n, :, 3)=8;

%Ashok Chakra
for i=1:2*n
    for j=1:3*n
        if sqrt(power(i-2*n/2, 2)+ power(j-round(3*n/2), 2))>=round(2*n/6)-5
            if sqrt(power(i-2*n/2, 2)+ power(j-round(3*n/2), 2))<=round(2*n/6)-2
                flag(i, j, 1:2)=0;
            end
        end
    end
end
for i=round(2*n/3)+1:2*round(2*n/3)
    for j=round(3*n/2-2*n/6):round(3*n/2+2*n/6)
        dist= (sqrt(power(i-n, 2)+power(j-round(3*n/2), 2)));
        k=round(atand((round(3*n/2)-j)/(n-i)));
        if dist<=round(2*n/6)-2 && mod(k, 15)==0  % angle between two spokes 360/24
            flag(i, j, 1:2)=0;
        end
    end
end
% display

figure, imshow(flag);



OUTPUT:


Comments