Generate multiple images with rectangle of specific dimension

Like shown in figure, I want to create an image (224x224) with one rectangle (black) with dimensions (1meter*0.25meter) and its shadow (blue) with dimensions (1meter*2meters). In between these two rectangles, i want to have some space with dimensions (1meter*0.15meter) (also shown in figure). Like this i want to create 100 images placing the rectangle, space and shadow at different positions in image. please help me with this.

 Accepted Answer

  1. Provide the four vertices of the rectangle.
  2. Read about patch.
  3. Use hold on to plot multiple different patches on the same figure.
  4. Save the images using imwrite.
Also if you know the origin (left bottom vertex) of the rectangle and it's length, breadth; you can use rect , boundingbox also.
You can draw rectangle like this:
A = rand(1,2) ; % origin
L = 5 ; % length
B = 6 ; % breadth
% Make all coordinates of rectangle
V = [A ; A(1)+B A(2) ; A(1)+B A(2)+L ; A(1) A(2)+L] ;
patch(V(:,1),V(:,2),'r')

8 Comments

Thank you for that. But I need rectangle, space and shadow of specific dimensions (constant in all images) by pixel wise labelling in terms of meters. This is to create synthethic images for semantic segmentation. May be by modifying using this:
Please help me with this.
I created an image like that by using the:
clc;
image = zeros(224,224, 'uint8');
image(10:90, 10:60) = 125;
image(95:110, 10:60) = 225;
figure
imshow(image,[]);
imwrite(image,'myimage.png')
:The output is
But, now i want to create images around 200 like shown above by shifting the rectangle, space and shadow (which is shown in above figure). How can i do that?
Help me with this issue.
How you want to shift them? You can shift the indices which you have specifed to shift...
Don't use the variable name as image; there is one inbuilt function on the same name.
I will change the variable name. I want to shift uniformly to different positions and save that image. Like that i want to create 200 images and save them.
% move along x-axes
count = 0 ;
for i = 0:10:200
count = count+1 ;
I = zeros(224,224, 'uint8');
idx1 = (10:90) ;
idy1 = (10:60)+i ;
I(idx1, idy1) = 125;
idx2 = (95:100) ;
idy2 = (10:60)+i ;
I(idx2, idy2) = 225;
imshow(I,[]);
filename = ['myimage',num2str(count),'.png'] ;
imwrite(I,filename)
end
% move along y-axes
for j = 0:50:200
count = count+1 ;
I = zeros(224,224, 'uint8');
idx1 = (10:90)+j ;
idy1 = (10:60) ;
I(idx1, idy1) = 125;
idx2 = (95:100)+j ;
idy2 = (10:60) ;
I(idx2, idy2) = 225;
imshow(I,[]);
filename = ['myimage',num2str(count),'.png'] ;
imwrite(I,filename)
end
This worked for me. Is it possible to save the positions of the rectangles in corresponding images? Because I need to have gtruth of these images to train the CNN network.
How you want to save the positions on the image?
Image name and corresponding rectangle positions in a table

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!