Plotting Rectangles on Same Figure in a While Loop

3 Ansichten (letzte 30 Tage)
Madeleine Wilson
Madeleine Wilson am 12 Jun. 2018
Beantwortet: MUHAMMED IRFAN am 16 Jun. 2018
Hi,
I am trying to plot multiple rectangles in one figure during a while loop. I am selecting a region of interest in specific frames of a video and saving the x-min, y-min, width, and height of each region. Later in the while loop, I want to plot this rectangle so that I can visualize the movement of this region of interest throughout the video.
Edit: Instead of plotting the rectangles in the same plot on the same figure with every iteration of the loop, a new figure with a new plot with just one rectangle plotted on it will be produced with every iteration. Im getting 50 plots with 1 rectangle on each, instead of 1 plot with all 50 rectangles on it.
Here is my code:
while ~isDone(videoFileReader)
figure;
imshow(objectFrame);
objectRegion = round(getPosition(imrect));
regions = [regions; objectRegion];
xmin = objectRegion(1);
ymin = objectRegion(2);
width = objectRegion(3);
height = objectRegion(4);
new_centroid = [(xmin + 0.5*width), (ymin + 0.5*height)];
centroid_list = [centroid_list; new_centroid]; %#ok<*AGROW>
objectImage = insertShape(objectFrame, 'Rectangle', objectRegion, 'Color', 'red');
hold on
imshow(objectImage);
plot(new_centroid(1), new_centroid(2), 'r*');
videoPlayer(objectImage);
hold off
figure('Name', 'Tracking box', 'NumberTitle', 'off');
hold on
rectangle('Position', [xmin ymin width height]);
xlim([0 1080]);
ylim([0 720]);
hold off
end
  2 Kommentare
MUHAMMED IRFAN
MUHAMMED IRFAN am 14 Jun. 2018
Hello,
What kind of error are you facing??
You have provided the code but the nature of your problem is not clear.
Madeleine Wilson
Madeleine Wilson am 14 Jun. 2018
Instead of plotting the rectangles in the same plot on the same figure with every iteration of the loop, a new figure with a new plot with just one rectangle plotted on it will be produced with every iteration. Im getting 50 plots with 1 rectangle on each, instead of 1 plot with all 50 rectangles on it.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

MUHAMMED IRFAN
MUHAMMED IRFAN am 16 Jun. 2018
Hello,
The function figure is used to open a new figure window. Each time you call the function figure in your loop, a new figure is opened and your image is displayed in it.So,
  • Totally avoid using the figure command
  • Use it with a number argument. Say ,
figure(1)
If you want to display multiple rectangles on this same image, all you have to do is call your < image with rectangle > as input to the loop. So that each time the loop is run , it will work on the image with previous rectangles and new rectangle gets added on it. I mean , replace
objectImage = insertShape(objectFrame, 'Rectangle', objectRegion, 'Color', 'red');
with
objectFrame= insertShape(objectFrame, 'Rectangle', objectRegion, 'Color', 'red');
Hope my answer is clear.

Kategorien

Mehr zu Lighting, Transparency, and Shading finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by