Making UK flag with MATLAB
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I want to make the UK flag with MATLAB, the idea is to tweak the RGB value of each pixel on the matrix so to represent the color and design of the flag.
My idea was to work on a quarter of the flag and then flip it multiple times and compose it in the final big matrix and print it. Here come the problems and my doubts:
1 - actually the UK flag isn't completely symmetrical: the red diagonal cross isn't symmetrical (the red and white central cross is symmetrycal, the white diagonal cross is symmetrycal, the blue parts are symmetrical, not the red diagonal cross), so I had in mind to add it in the end, after I compose the entire matrix, even if I still don't know if I'm able to
2 - to make the diagonal white cross I had in mind, to work on the diagonal elements of the matrix and the upper/lower diagonals till I get the right width of white, this to be done requires the matrix to be square, so I was wondering if it is possible to work entirely on a square matrix, build the flag and then print it with an aspect ratio of 2:1 (lenght : height) instead of working directly on a non-square matrix
Here is the flag and it's proportions:


Here is the quarter of the flag to work on and then flip to obtain the complete one:

The RGB values for the colors of the flag are:
white part : R=G=B=255/255
red part: R=200/255 G=16/255 B=46/255
blue part: R=1/255 G=33/255 B=105/255
The size of the matrix is 250pixel x 500pixel (it's not restrictive and can be changed) and represents a quarter, so the total flag should be 500pixel x 1000pixel.
So I started working on a rectangular matrix since I don't know if it's possible to work on a square matrix and then print it rectangular (problem number 2), would prefer to work on a square matrix.
ratio = 2/1; % The flag has a ratio of length / height = 2 / 1
% I'd like to work on a quarter of the total flag to exploit symmetries:
he = 250; % height, 500 in total
len = ratio * he; % length, 1000 in total
%flag is the 3D matrix: height x lenght x 3, where each 2D matrix height x lenght is relative to one RGB channel
% so we obtain a flag height x length and the third dimension determines the color (RGB)
flag = ones(len, he, 3); % Initializing the matrix
flag(:,:,1) = 1/255; % I make the matrix completely blue, the idea is to add the white and red crosses afterwards
flag(:,:,2) = 33/255;
flag(:,:,3) = 105/255;
The result of this part is:

%Central white cross
flag(:, 1:round(len / 6), :) = 1; % Make white vertical stripe on the left, len / 6 is equal to the ratio in the image of 5 / 30, where 5 is the
% width of the white central cross, and 30 is the length of the quarter of the flag
flag(he:-1:he-round(he / 3), :, :) = 1; % Make white horizontal stripe on the bottom, he / 3 is equal to the ratio of 5 / 15
The result of this part is :

Now I'd like to make the diagonal white cross, but I need the answer to my 2nd question.
So I show the code to make the red central cross:
% Red central cross
flag(:, 1:len/10, 1) = 200/255; % Making red vertical stripe on the left
flag(:, 1:len/10, 2) = 16/255;
flag(:, 1:len/10, 3) = 46/255;
flag(he:-1:he-he/5, :, 1) = 200/255; % Making red horizontal stripe on the bottom, always respecting the ratios
flag(he:-1:he-he/5, :, 2) = 16/255;
flag(he:-1:he-he/5, :, 3) = 46/255;
The output of this part is:

And here I can compose the ultimate matrix, by flipping and composing matrices:
flag = [flip(flag, 2) flag ; flip(flip(flag,2),1) flip(flag, 1)];
imshow(flag);
And this is the result:

So the white and red diagonal crosses are missing:
- if I can work with the flag matrix as a square one then I can modify its diagonal and k-th upper and lower diagonals to make them white, this solves the diagonal white cross problem.
- need help on how to make the red diagonal cross, since they aren't symmetryc.
Thanks a lot for the help, I know this is long to read and I'm sorry for that, but MATLAB is awesome and I'd like to learn the most. Also if you have a better solution to the problem feel free to tell me. In particular I'd like to solve this stuff within saturday night if possible. Many thanks!
6 Kommentare
Jan
am 30 Nov. 2018
Bearbeitet: Jan
am 30 Nov. 2018
img = zeros(100, 200, 3);
index = linspace(1, 100, 200); % A diagonal
sub = round(sub2ind([100, 200], index, 1:200));
img(sub) = 0.1;
img(sub + 100*200) = 0.2;
img(sub + 100*200 * 2) = 0.3;
This is a diagonal in a 100x200 rectangle. The ugly manual addition of Slice*Size(image,1)*Size(image,2) is cruel. This would be much easier if you create the 2D matrix with the values 1,2,3 at first and create the 3D RGB array at the end only. I cannot imagine, why anyone could ask you to do it in an uglier and less efficient fashion.
I've used round() here instead of floor to get a symmetrix result.
You can do this in a loop also:
img = zeros(100, 200, 3);
y = round(linspace(1, 100, 200)); % A diagonal
x = 1:200;
for k = 1:length(x)
img(x(k), y(k), 1:3) = [0.1, 0.2, 0.3];
end
In your case this variant might help also:
img = zeros(100, 300, 3); % Now 300 pixels wide
y = round(linspace(1, 100, 200)); % A diagonal, still until 200
x = 1:200;
B = repmat(reshape([0.1, 0.2, 0.3], [1, 1, 3]), 20, 1, 1);
for k = 1:length(x)
img(x(k):x(k)+19, y(k), 1:3) = B;
end
image(img)
Did I mention altready, that working in the 3D RGB array is ugly? The repmat(reshape()) approach looks confusing.
Akzeptierte Antwort
Jan
am 3 Dez. 2018
Bearbeitet: Jan
am 3 Dez. 2018
Repetition of my comment:
img = zeros(100, 300, 3); % Now 300 pixels wide
y = round(linspace(1, 100, 200)); % A diagonal, still until 200
x = 1:200;
B = repmat(reshape([0.1, 0.2, 0.3], [1, 1, 3]), 20, 1, 1);
for k = 1:length(x)
img(x(k):x(k)+19, y(k), 1:3) = B;
end
image(img)
And the 2D method:
img = ones(100, 300); % 1 means red
y = round(linspace(1, 100, 200));
x = 1:200;
for k = 1:length(x)
img(x(k):x(k)+19, y(k)) = 2; % 2 means blue
end
% [red, blue, white]
map = [200, 16, 46; 1, 33, 105; 255, 255, 255] / 255);
imgRGB = ind2rgb(img, map);
image(imgRGB)
Now you create the 2D index matrix at first and can avoid the mess with repmat(reshape()) or the ugly conversion from a mask to the linear indices as in:
img(sub) = 0.1; img(sub + 100*200) = 0.2; img(sub + 100*200 * 2) = 0.3;
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!