Generate multiple images with rectangle of specific dimension
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
- Provide the four vertices of the rectangle.
- Read about patch.
- Use hold on to plot multiple different patches on the same figure.
- 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
kumara dommeti
on 17 Jun 2020
Edited: kumara dommeti
on 17 Jun 2020
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.
kumara dommeti
on 18 Jun 2020
Edited: kumara dommeti
on 18 Jun 2020
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.
KSSV
on 18 Jun 2020
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.
kumara dommeti
on 18 Jun 2020
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.
KSSV
on 18 Jun 2020
% 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
kumara dommeti
on 18 Jun 2020
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.
KSSV
on 18 Jun 2020
How you want to save the positions on the image?
kumara dommeti
on 18 Jun 2020
Image name and corresponding rectangle positions in a table
More Answers (0)
Categories
Find more on Image Thresholding in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)