Does imshow not work in a loop?
Show older comments
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
Stephen23
on 18 Mar 2018
"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,[])
Bhavana Bojja
on 18 Mar 2018
Stephen23
on 18 Mar 2018
@Bhavana Bojja: what value does nFiles have?
Bhavana Bojja
on 18 Mar 2018
Rik
on 18 Mar 2018
It looks to me like you can see the image in the figure, so what is your problem now?
Bhavana Bojja
on 18 Mar 2018
Stephen23
on 18 Mar 2018
@Bhavana Bojja: try removing the figure call inside the loop and adding pause(1) : do you see different images being displayed?
Bhavana Bojja
on 18 Mar 2018
Image Analyst
on 18 Mar 2018
And the "drawnow" like in my answer below didn't work?
Justin
on 13 Aug 2023
@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!
Walter Roberson
on 13 Aug 2023
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()
Image Analyst
on 13 Aug 2023
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.
Walter Roberson
on 13 Aug 2023
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.
Answers (1)
Image Analyst
on 18 Mar 2018
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
Walter Roberson
on 18 Mar 2018
However figure() is one of the calls defined to trigger a graphics update so I would expect the current code to work.
Image Analyst
on 18 Mar 2018
I copied and pasted his original code, and it worked fine for me. It showed every image.
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!