
Using imshow with subplot in for loop
5 views (last 30 days)
Show older comments
Dear MATLAB community,
I'm not good with figures, I could not grasp the figure window handling, axes properties etc. I have 2x2 subplot which I use first one with imshow to show current frame from video, second using imagesc to show binary image of the current frame, for 3 and 4th slots I plot the counts of area detected. My problem is at each iteration imshow kinda look like shaking, I couldn't figured out why, but its probably because I don't know how to handle figures in loops effeciently or more correct manner. My code look something like this,
for ii = startingframe:numberofframes
Frame = read(video,ii);
[Frame_Binary,rgb] = createMask(Frame,red_min,red_max,green_min,green_max,blue_min,blue_max);
subplot(2,2,1)
imshow(Frame)
title('Original Frame')
subplot(2,2,2)
imagesc(Frame_Binary)
% Detecting Particles
%
%
title('Detected Particles')
subplot(2,2,[3 4])
plot(k/video_fps,particles(k),'-r')
axis([0 inf 0 inf ])
xlim([0 NumberOfFrames/video_fps])
title('Particle Counts')
xlabel('Seconds')
ylabel('Particle Count')
end
Could you give me suggestions about how can I solve this issue and maybe other suggestions on how can I improve the implementation?
Thanks in advance!
0 Comments
Answers (1)
Raag
on 27 Apr 2025
Hi Richard,
The "shaking" or flickering observed when displaying video frames and plots in a MATLAB loop can happen because each call to ‘subplot’ , ‘imshow’ , or ‘imagesc’ inside the loop can recreate or resize axes, causing the display to jump or flicker.
To address this, create your figure and axes just once before the loop and then update only the image and plot data inside the loop. This approach keeps the figure layout stable and makes updates much smoother.
You can use the sample code below to organize your code for flicker-free animation.
% Example parameters
NumberOfFrames = 100;
video_fps = 10;
FrameSize = [240, 320, 3]; % Example frame size
Frame_Binary_Size = [240, 320];
% Simulate particle counts and frames for demonstration
particles = randi([0 50], NumberOfFrames, 1);
videoFrames = uint8(randi([0 255], [FrameSize, NumberOfFrames]));
binaryFrames = randi([0 1], [Frame_Binary_Size, NumberOfFrames]);
% Create figure and axes once
figure;
ax1 = subplot(2,2,1);
hFrame = imshow(videoFrames(:,:,:,1), 'Parent', ax1);
title('Original Frame')
ax2 = subplot(2,2,2);
hBinary = imagesc(binaryFrames(:,:,1), 'Parent', ax2);
axis(ax2, 'image');
title('Detected Particles')
ax3 = subplot(2,2,[3 4]);
hPlot = plot(ax3, nan, nan, '-r');
axis([0 NumberOfFrames/video_fps 0 max(particles)+5])
title('Particle Counts')
xlabel('Seconds')
ylabel('Particle Count')
for ii = 1:NumberOfFrames
% Update images and plot data
set(hFrame, 'CData', videoFrames(:,:,:,ii));
set(hBinary, 'CData', binaryFrames(:,:,ii));
set(hPlot, 'XData', (1:ii)/video_fps, 'YData', particles(1:ii));
drawnow limitrate
end
The above code results in the following output:

For a better understanding of the above solution, refer to the following documentations:
0 Comments
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!