How can I draw a kurdistan flag in matlab?

22 Ansichten (letzte 30 Tage)
Yaseen Tarq
Yaseen Tarq am 25 Mär. 2021
Kommentiert: Steven Lord am 21 Jun. 2022
flag=uint8(zeros(300, 600, 3));
flag(:, :, :)=300;
%rad Color
flag(1:100, :, 1)=230;
flag(1:100, :, 2)=0;
flag(1:100, :, 3)=0;
%Green Color
flag(200:300, :, 1)=10;
flag(200:200, :, 2)=0;
flag(200:300, :, 3)=0;
for i=1:500
for j=1:500
if sqrt(power(i-10, 2)+ power(j-10, 2))>=0
if sqrt(power(i-150, 2)+power(j-300,2))<=40
flag(i, j, 3)=0;
end
end
end
end
figure, imshow(flag);
I stopped at this place, can you help me draw a flag with these scenes that can move?
  1 Kommentar
John D'Errico
John D'Errico am 25 Mär. 2021
Move? How? Now you want someone to help you to write code to make the flag flutter in the wind? Seriously? Lets see, modeling a flexible membrane, allowed to move in 3 dimensions, under the action of turbulent air currents. So expertise in fluid mechanics, plus the mechanics of a flexible membrane.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 25 Mär. 2021
Bearbeitet: Adam Danz am 27 Mär. 2021
I agree with John that this is quite a big request and since there are lots of GIF images already available it makes the task a bit mundane.
Update: See Chad Greene's comment below for wave animation.
But let's look at what you got and make some improvements
Your original code
With smart indentation (ctrl+a to select all, ctrl+i to smart indent)
1. flag=uint8(zeros(300, 600, 3));
2. flag(:, :, :)=300;
3. %rad Color
4. flag(1:100, :, 1)=230;
5. flag(1:100, :, 2)=0;
6. flag(1:100, :, 3)=0;
7. %Green Color
8. flag(200:300, :, 1)=10;
9. flag(200:200, :, 2)=0;
10. flag(200:300, :, 3)=0;
11. for i=1:500
12. for j=1:500
13. if sqrt(power(i-10, 2)+ power(j-10, 2))>=0
14. if sqrt(power(i-150, 2)+power(j-300,2))<=40
15. flag(i, j, 3)=0;
16. end
17. end
18. end
19. end
20. figure, imshow(flag);
The first two lines can be combined for better efficiency but there's a problem with line 2. The max value for uint8 is 255 but you're setting it to 300 which defaults to 255. Instead of lines 1 and 2,
1. flag = uint8(ones(300,600,3))*intmax('uint8');
2. % flag(:, :, :)=300; % REMOVE
Height & Width should be 2:3
Next, I referred to this helpful technical description of the Kurdistan flag by the Kurdish Digital Library that defines the flag's height:width ratio as 2:3 but your height:width ratio is 300:600 or 1:2. To set the correct ratio the first line should be changed to
1. flag = uint8(ones(300,450,3))*intmax('uint8');
But be careful! The height (300) must be divisible by 3 and the width should be width=height/2*3.
Official colors
That website also identifies the official colors using the Pantone color system which are converted to RGB values below.
Red: PMS 032 [239 51 64]
Green: PMS 354 [0 177 64]
Yellow: PMS 116 [255 205 0 ]
Your colors do not match the colors from this website (maybe the website is wrong?). To match those colors using the 300x450 image,
3. % red Color
4. flag(1:100, :, 1)=239;
5. flag(1:100, :, 2)=51;
6. flag(1:100, :, 3)=64;
7. % Green Color
8. flag(201:300, :, 1)=0; % Change index to 201, not 200
9. flag(201:300, :, 2)=170; % Change index to 201:300, not 200:200
10. flag(201:300, :, 3)=64; % Change index to 201, not 200
Sun size and shape
Again, referencing the website, the sun has 21 equidistant rays where the inner diamter is half of the outer diameter and the orientation of the sun is such that the vertical meridian passes through one of the rays at the top. Finally, the ratio of the outer diameter to the height of the flag is 1:2.
Your flag contains 300x450 pixels which is not fine enough resolution to represent a 21 pointed star (or sun). Rather than building the star into the color matrix, I'll create it as a patch object that's plotted on top of the flag.
xy is an mx2 matrix of [x,y] verticies of the star.
11. % for i=1:500
12. % for j=1:500
13. % if sqrt(power(i-10, 2)+ power(j-10, 2))>=0
14. % if sqrt(power(i-150, 2)+power(j-300,2))<=40
15. % flag(i, j, 3)=0;
16. % end
17. % end
18. % end
19. % end
outerDiam = size(flag,1)/2;
nRays = 21;
th = linspace(0,2*pi,nRays*2+1)-pi/2; % -pi/2 ensures tip is on top
flagCenter = size(flag)/2;
xy = outerDiam/2 * [cos(th(:)), sin(th(:))];
xy(2:2:end,:) = xy(2:2:end,:)/2;
xy = xy + flagCenter([2,1]);
Plot flag
20. figure, imshow(flag);
21. hold on
22. patch(xy(:,1), xy(:,2), [255 205 0]/255, 'EdgeColor', 'none')
23. axis equal
Complete, updated code
flag = uint8(ones(300,450,3))*intmax('uint8');
% red
flag(1:100, :, 1)=239;
flag(1:100, :, 2)=51;
flag(1:100, :, 3)=64;
% Green Color
flag(201:300, :, 1)=0;
flag(201:300, :, 2)=170;
flag(201:300, :, 3)=64;
% Create sun ray coordinates (x,y)
outerDiam = size(flag,1)/2;
nRays = 21;
flagCenter = size(flag)/2;
th = linspace(0,2*pi,nRays*2+1)-pi/2; % -pi/2 ensures tip is on top
xy = outerDiam/2 * [cos(th(:)), sin(th(:))];
xy(2:2:end,:) = xy(2:2:end,:)/2;
xy = xy + flagCenter([2,1]);
% plot flag
figure, imshow(flag);
hold on
patch(xy(:,1), xy(:,2), [255 205 0]/255, 'EdgeColor', 'none')
axis equal
To visually confirm that the star is centered,
xline(size(flag,2)/2,'alpha',1)
yline(size(flag,1)/2,'alpha',1)
plot([min(xlim); max(xlim)], [min(ylim); max(ylim)], 'k-')
plot([min(xlim); max(xlim)], [max(ylim); min(ylim)], 'k-')
  9 Kommentare
Adam Danz
Adam Danz am 28 Mär. 2021
Bearbeitet: Adam Danz am 28 Mär. 2021
With the wave, you can't really see the pixelation of the star added directly into the image matrix at this size.
Another way to do that is using insertShape (computer vision toolbox).
Replace this
% Color the pixels inside the star:
[X,Y] = meshgrid(1:size(flag,2),1:size(flag,1));
star = flipud(inpolygon(X,Y,xy(:,1),xy(:,2)));
c = [255 205 0];
for k = 1:3
tmp = flag(:,:,k);
tmp(star) = c(k);
flag(:,:,k) = tmp;
end
with this
flag = insertShape(flag, 'FilledPolygon', xy, 'color', uint8([255 205 0]),'Opacity',1);
One small but important error is that the in the official flag, one of the sun rays is pointing directly upward, centered on the vertical meridian but in your version the vertical meridian splits two rays.
Here's a comparison of the 3 methods to add the star to the image (inpolygon, insertShape, and patch). The difference can only been seen when you zoom into the image.
Steven Lord
Steven Lord am 21 Jun. 2022
tic
flag = uint8(ones(300,450,3))*intmax('uint8');
toc
Elapsed time is 0.005061 seconds.
This line creates a double precision array, converts that array to uint8, and multiplies each element by the maximum uint8 value. Instead, I'd probably use repmat to directly expand the maximum value to an array of the correct size.
tic
flag2 = repmat(intmax('uint8'), [300 450 3]);
toc
Elapsed time is 0.001817 seconds.
isequal(flag, flag2)
ans = logical
1
The times you see are from rerunning the code several times. If for whatever reason you couldn't use repmat you could still avoid the "creates a double precision array" step by calling ones with the type of array to create.
tic
M1 = uint8(ones(300, 450, 3));
toc
Elapsed time is 0.002032 seconds.
tic
M2 = ones(300, 450, 3, 'uint8');
toc
Elapsed time is 0.001300 seconds.
isequal(M1, M2)
ans = logical
1

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Bjorn Gustavsson
Bjorn Gustavsson am 25 Mär. 2021
You can add something like this:
phi360 = linspace(0,2*pi,43);
r = 60 + 25*(-1).^(1:43);
phi360 = linspace(0,2*pi,43);
hold on
ph = fill(r0(1)+r.*sin(phi360),r0(2)+r.*cos(phi360),[1 1 0]);
set(ph,'EdgeColor','none')
If you want to set the colours of the star to yellow you should take a look at the help and documentation of inpolygon for a clean way to find pixels that are inside the star.
HTH
  1 Kommentar
Shahir Ahmad Safi
Shahir Ahmad Safi am 21 Jun. 2022
Hello there, could you please draw me flag of Afghanistan?
Thanks!!!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by