Search code examples
matlabfile-ionested-loopsfile-handling

How to build filenames in a nested loop


I am trying to access 4k images and crop some ROI based areas (4 ROI in my case) and store them in some directory. So far everything is working okay except the loops handling of the filename.

Below is my code attached. I am accessing N 4k images, crop and resize them to my desired resolution. In the end, when I tried to save the data the images got overwritten.

N=2;
for img = 1:N
    x = gTruth.LabelData.crack{img,1}

    for i=1:4
        Cells = x(i,1:4)

        baseFileName = theFiles(img).name;
        fullFileName = fullfile(myFolder, baseFileName);
        fprintf(1, 'Now reading %s\n', fullFileName);
        imageArray = imread(fullFileName);
        CroP = imcrop(imageArray,Cells);
        imshow(CroP);
        B = imresize(CroP,[256 256]);
        imshow(B);
        imwrite(B,strcat('C:\Users\USER\Desktop\Directory\imagefile_00',num2str(i),'.png'));
    end
end

My question is that after loop i runs, it saves 4 images and for img again it saves four values. Now when the code runs it saves only last 4 images and not 8. I should get a i*N number of total images, but I'm getting only 4 and rest are overwritten.

How can I adapt my program to save all files?


Solution

  • imwrite(B,strcat('C:\Users\USER\Desktop\Directory\imagefile_00',num2str(i),'.png'));
    

    is where the problem is. You use num2str(i) to change the number, and i=[1,2,3,4]. Thus, you cannot create a file which is outside numbers 1 to 4. Taking you want something based on img as well, you can use e.g.

    imwrite(B,strcat('C:\Users\USER\Desktop\Directory\imagefile_',num2str(img),num2str(i),'.png'));
    

    which will create a file named imagefile_11 for the first image and first region, imagefile_12 for the first image, second region, imagefile_324 for the 32nd image, fourth region etc. Change according to your needs of course.

    Example in action:

    >> i=3;img=1;
    >> strcat('C:\Users\USER\Desktop\Directory\imagefile_',num2str(img),num2str(i),'.png')
    
    ans =
    
    C:\Users\USER\Desktop\Directory\imagefile_13.png
    
    >> i=1;img=2;
    >> strcat('C:\Users\USER\Desktop\Directory\imagefile_',num2str(img),num2str(i),'.png')
    
    ans =
    
    C:\Users\USER\Desktop\Directory\imagefile_21.png
    

    Free advice:

    • i and j are the imaginary unit. It's, in my opinion, preferable not to use them as loop indices.

    • Your desktop is most likely not the best place to save stuff. Make a folder in for example your Documents folder with an apt name, e.g. C:\Users\USER\Documents\ROIfrom4k\

    • The declarations of x and Cells would benefit from a closing semicolon on the line, so as to prevent them from outputting to the console, slowing down the program and clogging the command window. The orange wiggles MATLAB puts there are not for festivity, they present a useful warning. (Not to be confused with red wiggles, those present an serious error, because of which MATLAB cannot run at all.)