Plotting Rectangles on Same Figure in a While Loop
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
MUHAMMED IRFAN
2018-6-14
Hello,
What kind of error are you facing??
You have provided the code but the nature of your problem is not clear.
回答(1 个)
MUHAMMED IRFAN
2018-6-16
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!