Does imshow not work in a loop?

조회 수: 9 (최근 30일)
Bhavana Bojja
Bhavana Bojja 2018년 3월 18일
댓글: Walter Roberson 2023년 8월 13일
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
Image Analyst
Image Analyst 2023년 8월 13일
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 2023년 8월 13일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by