필터 지우기
필터 지우기

Plotting Rectangles on Same Figure in a While Loop

조회 수: 2 (최근 30일)
Madeleine Wilson
Madeleine Wilson 2018년 6월 12일
답변: MUHAMMED IRFAN 2018년 6월 16일
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
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.
Madeleine Wilson
Madeleine Wilson 2018년 6월 14일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

MUHAMMED IRFAN
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.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by