How to make and save image for each iteration in a for loop?
18 ビュー (過去 30 日間)
古いコメントを表示
Dear all,
I want to know how to make and save an image for each time iteration in a for loop. The following is a simple test code I made, but if I can figure this out I believe mine should be easy to figure out too.
function hel = gr(st)
t = [1:10] ;
for n=1:10
graze(n,1) = (n+1)*st ;
graze(n,2) = (n+1)*(st+1) ;
end
figure(n)
hel = plot(t,graze(:,1),'r+',t,graze(:,2),'b+') ;
legend('ubc','my mood')
xlabel('time')
ylabel('hungry')
% filename = sprintf('mat_img_%d.jpg', n);
% saveas(gcf, filename, 'jpg')
end
every iteration, I'd like to create an image corresponding to the iteration step and save it on my computer. How should I modify this code? Thank you.
6 件のコメント
Cedric
2013 年 4 月 23 日
Yes, my example was about the image (if you right click on it, you can see that it is an image). So your question is how to produce this kind of image from a variable's content (?)
採用された回答
Cedric
2013 年 4 月 23 日
編集済み: Cedric
2013 年 4 月 23 日
I understand now; you'll want something like:
filename = sprintf('mat_img_%d.jpg', n) ;
imwrite(B, filename, 'png') ;
Note that you can resize the image if it is too small, e.g.
filename = sprintf('mat_img_%d.jpg', n) ;
B = imresize(B, 100, 'nearest') ; % 100 is the scale factor.
imwrite(B, filename, 'png') ;
Then you can use the code point at by Image Analyst for building the movie.
3 件のコメント
Cedric
2013 年 4 月 23 日
編集済み: Cedric
2013 年 4 月 23 日
Actually, you could avoid the double loop and replace it with:
img = ones(size(almos)) ;
img(almos==french) = 0 ;
Then you can proceed as follows:
filename = sprintf('mat_img_%d.jpg', n) ;
img = imresize(img, 400, 'nearest') ;
imwrite(img, filename, 'png') ;
The mistake that you made is that you ask IMSHOW to magnify what it displays, but this function doesn't return the magnified image (it only displays it magnified). The number that is returned is a handle to the image object actually. This is why I proposed IMRESIZE, which outputs a magnified image.
その他の回答 (1 件)
Image Analyst
2013 年 4 月 23 日
3 件のコメント
Image Analyst
2013 年 4 月 23 日
If you want images, then use im2frame(). If you want the whole figure including axes, titles, tick marks, etc. then save each figure out with export_fig() to an image file, such as a PNG file, then build the movie by reading in all the frames with imread(), convert to a movie frame with im2frame. See the end of my demo here http://www.mathworks.com/matlabcentral/answers/48797#answer_59652 for an example using the standard demo movie rhinos that ships with MATLAB.
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!