Why does not line appear over plot

2 次查看(过去 30 天)
Andrés Méndez
Andrés Méndez 2017-10-16
评论: OCDER 2017-10-17
I am trying to draw a skeleton over an RGB image. pos2D is a cell which holds the x,y coordinates of the body.
for w= 1:10
color = imresize(Foto(:,:,w),COL_SCALE);
c.im = imshow(color,[]);
%set(c.im,'CData',color);
skeleton = pos2D{1,w};
for m = 1:19
X1 = [skeleton(SkeletonConnectionMap(m,1),1) skeleton(SkeletonConnectionMap(m,2),1)];
Y1 = [skeleton(SkeletonConnectionMap(m,1),2) skeleton(SkeletonConnectionMap(m,2),2)];
line(X1,Y1, 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', '+', 'Color', 'r');
end
end
Outside the loop it works perfecty, i.e. if i change the w index by hand and do step by step, the line appears. However, when I do these nested loops it does not work. It only appears in the last frame before closing the loop. I tried hold on and off several times. What am I missing?
Thanks!

回答(2 个)

OCDER
OCDER 2017-10-16
Using imshow will replace the current axes and everything drawn on that axes, including the lines. To fix, use imshow once and then change CData of that image handle. This way, your lines are not deleted.
for w = 1:10
color = imresize(Foto(:,:,w),COL_SCALE);
skeleton = pos2D{1,w};
if w == 1
c.im = imshow(color, []);
else
c.im.CData = color;
end
for m = 1:19
X1 = [skeleton(SkeletonConnectionMap(m,1),1) skeleton(SkeletonConnectionMap(m,2),1)];
Y1 = [skeleton(SkeletonConnectionMap(m,1),2) skeleton(SkeletonConnectionMap(m,2),2)];
line(X1,Y1, 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', '+', 'Color', 'r');
end
end
  3 个评论
Walter Roberson
Walter Roberson 2017-10-17
for w = 1:10
color = imresize(Foto(:,:,w),COL_SCALE);
skeleton = pos2D{1,w};
if w == 1
c.im = imshow(color, []);
c.lh = line(nan, nan, 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', '+' 'Color', 'r');
else
c.im.CData = color;
end
for m = 1:19
X1 = [skeleton(SkeletonConnectionMap(m,1),1) skeleton(SkeletonConnectionMap(m,2),1)];
Y1 = [skeleton(SkeletonConnectionMap(m,1),2) skeleton(SkeletonConnectionMap(m,2),2)];
set(c.lh, 'XData', X1, 'YData', Y1);
drawnow()
end
end
OCDER
OCDER 2017-10-17
Hi Andrés, Walter answered your next question. What you want to do is the first loop w = 1, do imshow and draw your 19 lines. In all other loop w > 1, adjust the data in imshow and line.
for w = 1:10
color = imresize(Foto(:,:,w),COL_SCALE);
skeleton = pos2D{1,w};
if w == 1 %First time, make image and 19 lines
c.im = imshow(color, []);
for m = 1:19
X1 = [skeleton(SkeletonConnectionMap(m,1),1) skeleton(SkeletonConnectionMap(m,2),1)];
Y1 = [skeleton(SkeletonConnectionMap(m,1),2) skeleton(SkeletonConnectionMap(m,2),2)];
c.lh(m) = line(X1, Y1, 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', '+', 'Color', 'r');
end
else %All other times, adjust image and 19 lines position
c.im.CData = color;
for m = 1:19
X1 = [skeleton(SkeletonConnectionMap(m,1),1) skeleton(SkeletonConnectionMap(m,2),1)];
Y1 = [skeleton(SkeletonConnectionMap(m,1),2) skeleton(SkeletonConnectionMap(m,2),2)];
set(c.lh(m), 'XData', X1, 'YData', Y1);
drawnow()
end
end
end

请先登录,再进行评论。


Andrés Méndez
Andrés Méndez 2017-10-16
Donald,
There seems to be something about timing. If I do the CData, i just get a final black background with all the skeleton on top, no video with RGB sequence...
Things change if i put a pause(0.01) in the loop Then I see the image sequence with the skeleton drawn on top (why is this?)...however, the skeleton appears and disappears with the imshow...if I do what you suggest (c.im.CData) i can see the image sequence but the skeleton do not disappear and add one on top of each other. how do i erase the skeleton to update it with the next?
Thanks!

Community Treasure Hunt

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

Start Hunting!

Translated by