insertShape query {URGENT}

I am trying to insert 4 shapes in a single image using MATLAB function insertShape. The sequence of these 4 types of shape is decided by the MATLAB function randperm . I want suppose a shape is inserted after another shape, if the previous shape lies inside the present shape, then it should be removed. Please help!
The shapes are of white color with black boundary on a white background.
Please find the code as follows and suggest the changes as per my requirement
% definition of function used below to generate polygon
function cord=my_shape(sides,centroid_distance,centre)
clear x
clear y
clear xy
clear xyRotated
clear rotationMatrix
numSides=sides;
centroidToVertexDistance=centroid_distance;
minDistance = centroidToVertexDistance;
maxDistance = centroidToVertexDistance;
thisDistance = (maxDistance - minDistance) * rand(1) + minDistance;
for v = 1 : numSides
angle = v * 360 / numSides;
x(v) = thisDistance * cosd(angle);
y(v) = thisDistance * sind(angle);
end
x(end+1) = x(1);
y(end+1) = y(1);
angleToRotate = 360 * rand(1);
rotationMatrix = [cosd(angleToRotate), sind(angleToRotate); -sind(angleToRotate), cosd(angleToRotate)];
xy = [x ; y];
xy=xy';
xyRotated = xy * rotationMatrix;
x = xyRotated(:, 1);
y = xyRotated(:, 2);
xCenter = centre;
yCenter = centre;
x = x + xCenter;
y = y + yCenter;
cord=[x' ; y'];
cord=cord(:);
cord=round(cord');
num_of_images=2;
size_x=3000;
start_x=size_x;
size_y=3000;
start_y=size_y;
start=round(size_x/16);
for loop=1:num_of_images
cx=randi([round(start_x/4) round(3*size_x/4)]);
cy=randi([round(start_y/4) round(3*size_y/4)]);
cr=randi([round(start_x/6) round(size_x/4)]);
rx=randi([round(start_x/4) round(3*size_x/4)]);
ry=randi([round(start_y/4) round(3*size_y/4)]);
rl=randi([round(start_x/6) round(size_x/3)]);
rw=randi([round(start_x/6) round(size_y/3)]);
RGB =255+zeros(size_x,size_y,3);
RGB1=zeros(size_x,size_y,3);
segim=zeros(size(RGB));
turn=randperm(4);
for i=1:4
if turn(i)==1
RGB = insertShape(RGB,'Circle',[cx cy cr],'LineWidth',15,'Color','black');
segim = insertShape(segim,'FilledCircle',[cx cy cr],'Color','red','Opacity',1);
elseif turn(i)==2
RGB = insertShape(RGB,'Rectangle',[rx ry rw rl],'LineWidth',15,'Color','black');
segim = insertShape(segim,'FilledRectangle',[rx ry rw rl],'Color','green','Opacity',1);
elseif turn(i)==3
cent=randi([round(start_x/8) round(3*size_x/4)]);
dist=randi([round(start_x/6) round(size_x/4)]);
put=my_shape(3,dist,cent);
RGB = insertShape(RGB,'Polygon',put,'LineWidth',15,'Color','black');
segim = insertShape(segim,'FilledPolygon',put,'Color','blue','Opacity',1);
elseif turn(i)==4
cent=randi([round(start_x/8) round(3*size_x/4)]);
dist=randi([round(start_x/6) round(size_x/4)]);
const=my_shape(5,dist,cent);
RGB = insertShape(RGB,'Polygon',const,'LineWidth',15,'Color','black');
segim = insertShape(segim,'FilledPolygon',const,'Color','yellow','Opacity',1);
end
end
imshow([RGB,segim])
end

답변 (0개)

카테고리

태그

질문:

2019년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by