Does imshow not work in a loop?

I'm basically trying to read a number of images from a certain folder using imread, and my code is as shown below:
imagefiles=dir('*.jpg');
nfiles=length(imagefiles);
for k=1:nfiles
currentfilename=imagefiles(k).name;
currentimage=imread(currentfilename);
figure;
imshow(currentimage)
end
And after this, when I run the code, I can't see the image output. So does imshow work in cases like these at all?

14 Comments

"Does imshow not work in a loop?"
Of course imshow works in a loop. But there are lots of reasons why your image might not display properly. Please show us the output of these commands:
whos currentImage
imshow(currentImage,[])
Attached below are the outputs of the commands.
@Bhavana Bojja: what value does nFiles have?
It looks to me like you can see the image in the figure, so what is your problem now?
I ran those you gave commands individually. I run the code as a whole, I don't see the output.
@Bhavana Bojja: try removing the figure call inside the loop and adding pause(1) : do you see different images being displayed?
Yup. Thanks, it works.:)
And the "drawnow" like in my answer below didn't work?
@Image Analyst thank you for posting this. I'm new to MATLAB, trying to render a basic image inside a for loop, and this helped tremendously. I ended up using
figure; imshow(img); drawnow; pause(1);
Computers are funny. Cheers!
At least in theory, pause() for any positive time by itself should also internally do what drawnow() does, so in theory you could use the pause(1) without drawnow().
There are, however, cases where you need to pause -- where in theory drawnow() by itself would work, but in practice you need pause()
@Walter that makes sense. I'm starting with the kitchen sink and whittling it down from there. 😁
Yeah, sometimes the processing is so fast that you can't really see what happened. In that case, if you want to see the output image before it vanishes and is replaced by the next one, you can put in a pause.
When you are working with graphics in a loop, the graphics system really does do things differently if you do not have one of pause or waitfor or figure or uiwait or waitfor (and perhaps there are others by now.) Until one of those is invoked, MATLAB does not do layout or finalize axes limits or tick positions, and so on -- and some scaling and movement callbacks might not get called. If you query a Position that has changed since the last time one of those functions was invoked, you might get back incorrect information -- information that might be neither what it was before nor what it was most recently set to.
There are certainly times where you might want to tell layout what has been done so-far without making the changes visible, but unfortunately MATLAB does not provide a way to do that.

Sign in to comment.

Answers (1)

The loop is being processed so fast that the screen painting messages are not being processed. To fix, put a drawnow after your imshow()
imshow(currentImage, [])
drawnow;

2 Comments

However figure() is one of the calls defined to trigger a graphics update so I would expect the current code to work.
I copied and pasted his original code, and it worked fine for me. It showed every image.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 18 Mar 2018

Commented:

on 13 Aug 2023

Community Treasure Hunt

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

Start Hunting!