how to store the image immediately after reading it in a for loop?

后端 未结 3 795
故里飘歌
故里飘歌 2021-01-25 02:07

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

相关标签:
3条回答
  • 2021-01-25 02:56

    You can simply save all them into one big matrix:

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

    After this, all images will be stored in images_all (here assume that all images are colored images, i.e. 3 channels).

    0 讨论(0)
  • 2021-01-25 02:59

    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
    

    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);
    
    0 讨论(0)
  • 2021-01-25 03:06

    Try this -

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

    Another approach could be to use STRUCT and store those images as fields of a struct.

    Storing as a 4D matrix is another efficient way as suggested by herohuyongtao.

    0 讨论(0)
提交回复
热议问题