Triangle Circle Dataset Creation

I need to generate a triangle in matlab in the scope of 500x500 pixels. I need to generate a certain amount of images so its a dataset. The triangles either need to be at a certain angle and be either pink or purple. I have no idea what commands to use.

2 comentarios

DGM
DGM el 15 de Nov. de 2022
What are the constraints?
  • Do they need to be a certain configuration? Irregular? Regular?
  • How many triangles per image?
  • Do they need to be a certain size?
  • Do they need to be in a certain position?
  • Can they intersect image boundaries?
  • What's the background?
  • Are the triangles supposed to be a particular pink/purple, or can it be any random color in that range?
  • Do the triangles need to be binarized, or should they be antialiased?
marsmar
marsmar el 25 de Nov. de 2022
They can be irregular or regular! I just need one triangle per image and they just need to be within the 500x500 pixels. They can be in any position and it could vary. The background by default is white. They can be in any random color range.

Iniciar sesión para comentar.

 Respuesta aceptada

DGM
DGM el 16 de Nov. de 2022
Until clarification, here's a crude example.
% some parameters, maybe?
szo = [500 500]; % [y x]
fgcolor = [0.7 0.3 1];
bgcolor = [0 0 0.2];
% does any of this need to be fixed? random?
% this will use a fixed radius
% but angles and center are contrained random
r = 100;
center = (szo-2*r-1).*rand(1,2)+1+r; % [y x]
angles = 0:120:240;
angles = angles + 10*rand() + 10*randn(size(angles));
angles = [angles angles(1)];
x = r*cosd(angles) + center(2);
y = r*sind(angles) + center(1);
% create a mask
mask = poly2mask(x,y,szo(1),szo(2));
% create the ouput image (no antialiasing)
outpict = repmat(permute(bgcolor,[1 3 2]),szo);
outpict = mask.*permute(fgcolor,[1 3 2]) + (1-mask).*outpict;
outpict = im2uint8(outpict);
imshow(outpict)

7 comentarios

DGM
DGM el 17 de Nov. de 2022
Editada: DGM el 19 de Nov. de 2022
Here's another blind tangent. If I'm just guessing in the dark, I'm not going to go out of my way to avoid MIMT tools. I doubt this is what you want, but right now, you're the only person who can know that.
% some parameters, maybe?
szo = [500 700]; % [y x]
fgcolorlim = [0.8 0.3 1; 1 0.3 0.5];
bgcolor = [0 0 0.2];
rlimits = [75 150]; % allowed range of vertex radius
maxrotation = 60; % maximum nominal rotation
anglevariance = 10; % variance of individual vertex angles
margins = -50; % how far to keep triangles from border
fgCT = makect(fgcolorlim(1,:),fgcolorlim(2,:),64); % create color table
outpict = repmat(permute(bgcolor,[1 3 2]),szo); % create colored BG
for k = 1:20
% get constrained random vertex coordinates
r = randrange(rlimits,1);
margins = min(margins,szo/2 - r); % margins must leave no less than zero area
center = (szo-2*(r+margins)-1).*rand(1,2)+1+r+margins; % [y x]
angles = 0:120:240; % nominal vertex angles
angles = angles + maxrotation*rand() + anglevariance*randn(size(angles)); % rotate, perturb
angles = [angles angles(1)]; %#ok<AGROW> % close polygon
x = r*cosd(angles) + center(2);
y = r*sind(angles) + center(1);
% create a mask
mask = poly2mask(x,y,szo(1),szo(2));
% create the ouput image
fgcolor = fgCT(randi([1 size(fgCT,1)],1,1),:); % a random color from the CT
alpha = randrange([0.2 0.8],1); % pick random opacity
outpict = replacepixels(fgcolor,outpict,alpha*mask);
end
outpict = im2uint8(outpict);
makect(), randrange(), replacepixels() are from MIMT on the FEX.
marsmar
marsmar el 25 de Nov. de 2022
How could i generate a data set with only one triangle image?
DGM
DGM el 25 de Nov. de 2022
You would tailor the code to generate single images in a configuration you like, then you'd run that in a loop, and you can write the images using imwrite(). You can use sprintf() (and fullfile() if necessary) to create sequential filenames.
% set up some things
numberofimages = 10;
basepath = 'myfolderwhereiputthings';
basename = 'triangleimage';
for k = 1:numberofimages
% ...
% code to generate one triangle image
% ...
% generate filename and write file
fname = sprintf('%s_%05d.png',basename,k);
imwrite(outpict,fullfile(basepath,fname))
end
Something like that. The first example should be close enough. You can adjust the colors as needed. If you want the fgcolor to be completely random, you can just set that using rand(1,3)
marsmar
marsmar el 25 de Nov. de 2022
Thank you! I have an additional question, if I wanted to make different shapes I would use this snippet of the code. I assume it's just trigonometry, but where can I find more information to make different shapes? I wanted to make a circle and a square too.
% but angles and center are contrained random
r = 100;
center = (szo-2*r-1).*rand(1,2)+1+r; % [y x]
angles = 0:120:240;
angles = angles + 10*rand() + 10*randn(size(angles));
angles = [angles angles(1)];
x = r*cosd(angles) + center(2);
y = r*sind(angles) + center(1);
So long as the shapes are polygons, all that's needed is a change to the nominal vertex angle list.
% some parameters, maybe?
szo = [500 500]; % [y x]
fgcolor = [0.7 0.3 1];
bgcolor = [0 0 0.2];
nvert = 5; % number of vertices
rlimits = [75 150]; % allowed range of vertex radius
maxrotation = 60; % maximum nominal rotation
anglevariance = 0; % variance of individual vertex angles
margins = 0; % how far to keep triangles from border
% generate vertices
r = randrange(rlimits,1);
margins = min(margins,szo/2 - r); % margins must leave no less than zero area
center = (szo-2*(r+margins)-1).*rand(1,2)+1+r+margins; % [y x]
angles = 0:360/nvert:(360-360/nvert); % nominal vertex angles
angles = angles + maxrotation*rand() + anglevariance*randn(size(angles)); % rotate, perturb
angles = [angles angles(1)]; % close polygon
x = r*cosd(angles) + center(2);
y = r*sind(angles) + center(1);
% create a mask
mask = poly2mask(x,y,szo(1),szo(2));
% create the ouput image (no antialiasing)
outpict = repmat(permute(bgcolor,[1 3 2]),szo);
outpict = mask.*permute(fgcolor,[1 3 2]) + (1-mask).*outpict;
outpict = im2uint8(outpict);
imshow(outpict)
marsmar
marsmar el 25 de Nov. de 2022
I have an error with the "randrange" function. Is it a function specific to a certain toolbox? I'm trying to recreate a square and had just edited the "nvert" number to 4.
DGM
DGM el 26 de Nov. de 2022
Editada: DGM el 26 de Nov. de 2022
I attached randrange(). It's just a convenience tool that I used and didn't bother removing. It's basically just one line. You could also simply just replace the one where it's used with
r = rlimits(1) + rand()*range(rlimits);

Iniciar sesión para comentar.

Productos

Versión

R2020b

Preguntada:

el 15 de Nov. de 2022

Editada:

DGM
el 26 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by