Search code examples
performancematlabimage-processingcomputer-visionmatlab-cvst

How to perform faster updation of data in my image using matlab?


I'm coding an algorithm for lane detection,

This is the skeleton code I've used,

while ~isDone(video)
   currentFrame = getFrame(video);
   .
   . % Do segmentation and lane detection
   .
   figure(1),imshow(currentFrame),hold on
   figure(1),plot( theLinesThatWereDetected );
   pause(.0001); % without pause the plot command wouldn't work like a streamer.
end

This is a video https://www.youtube.com/watch?v=K881hFCyiQ8 of the simulation, Problem : The output video gets slower and slower after displaying each frame, but as soon as I close the figure window it automatically restarts(as the code's running) and it gets faster(check video). Why is this happening, is there some memory build up that is happening to slow down the plotting? What can I do to fast things up other than manually closing figure window?

I know there is a video.ShapeInserter object available which is faster than the plotting method I've used . The reason I didn't use it is because the changing the thickness of lines in thee video.ShapeInserter object came only in the 2014 release and I'm using 2013 version. I wanted very distinct thick lines for my lane detection.

Kindly give me suggestions.

Edit: This is the video after applying the edits suggested by Shai. https://www.youtube.com/watch?v=LJ_may0hkaE&feature=youtu.be


Solution

  • Problem:

    Basically, all frames are added to your figure one on top of the other due to the hold on state your axes handle is in. This causes memory buildup and slows you down.

    Solution:

    You should turn off hold after drawing the lines, so the imshow of the next frame will discard the previous frame.

    imshow(currentFrame);
    hold on;
    plot( theLinesThatWereDetected  );
    hold off; %// super critical!
    drawnow; %// instead of pause
    

    Comments:

    1. As pointed by Ander it is better practice to use drawnow instead of pause(0.001).
    2. Just changing XData and YData of your plot (as suggested by Benoit_11) will not be enough, it does not solve the memory waste caused by "holding" all the frames in the figure.