wilyam walass
wilyam walass

Reputation: 37

parse some folders in specific directory within particular names in matlab

I've written this code

srcFolders = dir('D:\test*');
for folder = srcFolders
path = strcat('D:\',folder);
sear = strcat(path, '\*.bmp');
srcFiles = dir(searc);
for i = 1 : length(srcFiles)
    filename = strcat(path,'\',srcFiles(i).name);
    Image1= imread(filename);
    Image2 = imread('D:\\2','jpeg'); % Image 2
    x=diff(Image1 - Image2); %display the result to console
    if (x >= 0.05)
        name1 = strcat('D:\\temp2\',srcFiles(i).name);
        imwrite(Image1, name1, 'jpg'); 
        disp('Value 111111111111.')
        disp(x)
        else
            name2 = strcat('D:\\temp3\',srcFiles(i).name);
            imwrite(Image1,  name2, 'jpg'); 
            disp('Value 222222222222.')
            disp(x)
        end
    end
end 

My problems are:

1- When I implement my code its only views the value of x and writes only the first image from folder test1 to folder temp or to folder discard .

2- How can I write All images in folder test1 to folder temp or discard .

3- Then if I have folders from test 1 to test n how can I repeat this process for all folders with particular name test(i) in a specific directory D to check every image in test folders and complete remaining procedure? Thanks...

Upvotes: 1

Views: 139

Answers (1)

Ikaros
Ikaros

Reputation: 1048

1,2 -There are some things that doesn't work in your code

  imwrite ( Image1(i), 'temp', '.jpg');

First of all, if you look in the the MatLab Documentation, you can see that the format of imwrite is

imwrite(A,filename)
%or 
imwrite(A,filename,format)

On your code, you entered the folder name instead of the file name. A right way to write it could be

output_filename = strcat('your_path\temp\',srcFiles(i).name);
imwrite(Image1, output_filename, 'jpg'); %note that 'jpg' is facultative
                                            %and should not take a dot as you did

Just a detail, you wrote Image1(i), which take only the i element of the image, i guess you want Image1 instead.

3 - If you want to browse in multiple folders that have the defined pattern test i, you can do it this way (this is my way to do it, there are some more ways, maybe more optimal) :

Replace

srcFiles = dir('D:\test1\*.bmp');
for i = 1 : length(srcFiles)
    filename = strcat('D:\test1\',srcFiles(i).name);
    ...
end

By

srcFolders = dir('D:\test*');
for folder = srcFolders
    path = strcat('D:\', folder.name);
    sear = strcat(path, '\*.bmp');
    srcFiles = dir(sear);
    for i = 1 : length(srcFiles)
        filename = strcat(path,'\',srcFiles(i).name);
        ...
    end
end

Here is the full code :

srcFolders = dir('D:\test*');
for folder = 1:length(srcFolders)
    path = strcat('D:\',srcFolders(folder).name);
    sear = strcat(path, '\*.bmp');
    srcFiles = dir(sear);
    for i = 1 : length(srcFiles)
        filename = strcat(path,'\',srcFiles(i).name);
        Image1= imread(filename);
        Image2 = imread('D:\2','jpeg'); % Image 2
        x=diff(Image1 - Image2); %display the result to console
        if (x >= 0.05)
            name1 = strcat('D:\temp2\',srcFiles(i).name);
            imwrite(Image1, name1); 
            disp('Value 111111111111.')
            disp(x)
        else
            name2 = strcat('D:\temp3\',srcFiles(i).name);
            imwrite(Image1, name2); 
            disp('Value 222222222222.')
            disp(x)
        end
    end
end 

Upvotes: 2

Related Questions