解析matlab

时间:2015-07-01 07:17:20

标签: matlab

我已经写了这段代码

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 

我的问题是:

1-当我实现我的代码时,它只查看x的值,并且只将文件夹test1中的第一个图像写入文件夹temp或文件夹丢弃。

2-如何将文件夹test1中的所有图像写入文件夹temp或discard。

3-然后,如果我有test 1test n的文件夹,我怎样才能对特定目录D中具有特定名称test(i)的所有文件夹重复此过程,以检查测试中的每个图像文件夹和完整的剩余程序?谢谢......

1 个答案:

答案 0 :(得分:1)

1,2 - 有些东西在你的代码中不起作用

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

首先,如果你查看the MatLab Documentation,你可以看到imwrite的格式是

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

在您的代码中,您输入了文件夹名称而不是文件名。写它的正确方法可能是

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

只是一个细节,你写了Image1(i),它只取了图像的i元素,我猜你想要Image1

3 - 如果要浏览具有已定义模式test i的多个文件夹,您可以这样做(这是我的方法,有更多方法,可能更优化):

替换

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

通过

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

以下是完整代码:

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