如何从文件夹中读取图像,分割每个图像并将其串行存储在新文件夹中

时间:2015-02-11 07:57:58

标签: matlab image-processing

我正在从文件夹中拍摄图像(' E:\ cicila \ ME PROJECT \ ME 4 SEM \ brodatz \ IMAGES TAKEN \ TRY)图像存储为1. gif,2.gif等等

我正在划分它。我想用新名称存储这些图像。例如,拍摄的图像是1.gif 将它分成4个部分之后,我想将这些新图像命名为1. gif,2.gif,3.gif,4.gif

请帮帮我。我尝试了我的代码,但它显示错误:

feb11try出错(第25行) Img1(i,j)= NewImage(startr + i,startc + j);

代码是:

clc;
close all;
clear all;

srcFiles = dir('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES         TAKEN\TRY\*.gif');  % the folder in which ur images exists

%Taking the image from the given URL, it could have been the name of the file with extension if the root folder

n =2; %defining the number of rows
m =2; %defining the number of columns


for c = 1 : length(srcFiles)

   NewImage = 'newimage.gif'; %granting permission to create a file and write in it

rf = floor(512/n); %generating the number of row pixels in the new file
cf = floor(512/m); %generating the number of row pixels in the new file

for v = 1:n
for s = 1:m %nXm files need to be made

startr = (v-1)*rf;
startc = (s-1)*cf;

for i = 1 : rf
for j = 1 : cf

Img1(i,j) = NewImage(startr+i, startc+j);
end
end


imwrite( NewImage,'c.gif');

end
end


end

1 个答案:

答案 0 :(得分:0)

您需要使用imread()来读取源图像,而不是将文件名视为图像。根据您所拥有的,我假设您的图像是单通道。我还假设您的图像是unsigned int。另外,请记住,使用索引可以更快的方式执行此操作。

我对您的样本进行了一些更改,以便在这些假设下完成工作。这是未经测试的。

clc;
close all;
clear all;

srcFiles = dir('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES         TAKEN\TRY\*.gif');

n = 2; %defining the number of rows
m = 2; %defining the number of columns

count = 0;    
for c = 1 : length(srcFiles)
    srcFilename = ['E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES         TAKEN\TRY\' srcFiles(c).name];
    srcImg = imread(srcFilename); % Read source image
    [~,srcName] = fileparts(srcFilename);

    rf = floor(512/n); %generating the number of row pixels in the new file
    cf = floor(512/m); %generating the number of row pixels in the new file

    % initialize output image
    destImg = uint8(zeros(rf,cf));

    for v = 1:n
    for s = 1:m %nXm files need to be made


        startr = (v-1)*rf;
        startc = (s-1)*cf;

        for i = 1 : rf
        for j = 1 : cf

            destImg(i,j) = srcImg(startr+i, startc+j);

        end
        end

        count = count + 1;                
        imwrite( destImg, sprintf('%d.gif', count) );
    end
    end

end