Does imshow not work in a loop?

9 visualizzazioni (ultimi 30 giorni)
Bhavana Bojja
Bhavana Bojja il 18 Mar 2018
Commentato: Walter Roberson il 13 Ago 2023
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 Commenti
Image Analyst
Image Analyst il 13 Ago 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
Walter Roberson il 13 Ago 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.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 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 Commenti
Walter Roberson
Walter Roberson il 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
Image Analyst il 18 Mar 2018
I copied and pasted his original code, and it worked fine for me. It showed every image.

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by