如何从文件夹中读取图像,分割每张图像并将其顺序存储在新文件夹中
How to read images from folder, divide each image and store it in new folder serially
我正在从文件夹中拍摄图像('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
您需要使用 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
我正在从文件夹中拍摄图像('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
您需要使用 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