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

"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
Bhavana Bojja 2018년 3월 18일
Attached below are the outputs of the commands.
Stephen23
Stephen23 2018년 3월 18일
@Bhavana Bojja: what value does nFiles have?
Bhavana Bojja
Bhavana Bojja 2018년 3월 18일
6
Rik
Rik 2018년 3월 18일
It looks to me like you can see the image in the figure, so what is your problem now?
Bhavana Bojja
Bhavana Bojja 2018년 3월 18일
I ran those you gave commands individually. I run the code as a whole, I don't see the output.
Stephen23
Stephen23 2018년 3월 18일
@Bhavana Bojja: try removing the figure call inside the loop and adding pause(1) : do you see different images being displayed?
Bhavana Bojja
Bhavana Bojja 2018년 3월 18일
Yup. Thanks, it works.:)
Image Analyst
Image Analyst 2018년 3월 18일
And the "drawnow" like in my answer below didn't work?
Justin
Justin 2023년 8월 13일
@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
Walter Roberson 2023년 8월 13일
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()
Justin
Justin 2023년 8월 13일
@Walter that makes sense. I'm starting with the kitchen sink and whittling it down from there. 😁
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일

0 개 추천

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.

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

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2018년 3월 18일

댓글:

2023년 8월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by