如何在for循环中读取后立即存储图像? [英] how to store the image immediately after reading it in a for loop?

查看:127
本文介绍了如何在for循环中读取后立即存储图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下程序来读取多张图像(约300张图像)。现在我希望在将每个图像读取到某个位置之后立即存储这些图像,其名称为 g1 g2 g3 ...是否可以循环执行此操作?
这是我的尝试:

I tried the following program for reading multiple images(about 300 images). Now I want to store these images immediately after reading each to some location by some name as g1,g2,g3... Is it possible to do this in a loop? Here is my attempt:

for i=1:5
    m=imread(['C:\Users\shree\Desktop\1\im' num2str(i) '.jpg']);
    figure,imshow(m);
end


推荐答案

我强烈建议您存储它们在一个单元格数组中:

I highly recommend that you store them in a cell array:

for k=1:5
    image_path = ['C:\Users\shree\Desktop\1\im' num2str(i) '.jpg']; %// I have moved this to be on its own line as it will make debugging easier. You don't have to, but I think it's a good idea.
    images_all{k}  = imread(image_path);
end

使用 eval 创建变量名称,如 g1 g2 等,您使用无法管理的变量来污染工作区。另外,如果它们都在单元格数组中,则可以很容易地将相同的函数应用于循环中或使用 cellfun

By using eval to create variable names like g1, g2 etc you pollute your workspace with an unmanageable amount of variables. Plus if they are all in a cell array then it's really easy to apply the same function to each of them either in a loop or with cellfun.

例如,如果你想现在将它们全部转换为灰度:

For example if you want to convert them all to greyscale now:

images_grey = cellfun(@rgb2gray, images_all, 'UniformOutput', false);

这篇关于如何在for循环中读取后立即存储图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆