splitting multiple images in a loop
3 次查看(过去 30 天)
显示 更早的评论
Hello All,
Please i have a code i have succesfully used to split/sub-divide an image into multiple ones.
I want o modify it to be able to split mutiple images in a folder but im having erros.
Can you pleasse help me with making this possible.
Below is an excerpt of the code.
Thanks
clc
clear all
close all
myFolder = 'E:\split\IRT\1.ForestRiverNB';
filePattern = fullfile(myFolder,'*.png');
jpegFiles = dir(filePattern);
for k =1
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1,'Now reading %s\n',fullFileName);
% imageArray = imread(fullFileName);
% imshow(imageArray); % Display image.
% j = 1:size (data_set.Files, 1)
% folder_name = data_set.Files(k) ;
% mkdir('E:\Sattar\Spring2018\Crack Detection Comparison\Sensors\TestingUnlabeled\',char(folder_name))
input = imread (fullFileName) ;
input = imresize (input, [500 2000]) ;
figure
imshow(input);
% input = rgb2gray (input) ;
n=1;
for j = 1:2
for i = 1:8
row1 = (j-1)*225+1 ;
row2 = row1 + 224 ;
col1 = (i-1)*225+1 ;
col2 = col1+224;
sub_im = input (row1:row2 , col1:col2, :) ;
base_name = sprintf( 'C%i.jpg',n);
full_name = fullfile (myFolder, base_name);
fprintf('Saving %s\n', full_name);
imwrite(sub_im, full_name) ;
n = n+1 ;
end
end
end
回答(1 个)
DGM
2022-2-18
编辑:DGM
2022-2-18
This works without error; however, there were and still are issues that I can't fix without knowing what your intentions are.
The loop iterator k was set to 1, so only one file would be processed. I fixed that.
When multiple files are processed, the outputs overwrite each other since the filenames are the same. I simply added another numeric field to the dynamic filename spec. It probably still needs a more useful name. Also, use leading zeros on numeric fields if you ever want your files to sort correctly.
The output image size is 225x225, and the tiling is 2x8, but the image is resized to be 500x2000. This means that only a part of the image is used (450x1800). This also means that all images will be grossly distorted unless their original aspect ratio were the same as 500x2000.
Hard-coding all these geometries is a great way to make code inflexible, and it makes me have to ask if the output behavior is even intended. You'll have to specify what your goals are.
myFolder = 'sources';
filePattern = fullfile(myFolder,'a*.png');
selectedFiles = dir(filePattern);
for k = 1:length(selectedFiles)
baseFileName = selectedFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1,'Now reading %s\n',fullFileName);
input = imread(fullFileName) ;
input = imresize(input, [500 2000]);
n = 1;
for j = 1:2
for i = 1:8
row1 = (j-1)*225 + 1;
row2 = row1 + 224;
col1 = (i-1)*225 + 1;
col2 = col1 + 224;
sub_im = input(row1:row2 , col1:col2, :);
base_name = sprintf( 'C_%03d_%03d.jpg',k,n);
full_name = fullfile('.', base_name);
fprintf('Saving %s\n', full_name);
imwrite(sub_im, full_name) ;
n = n+1 ;
end
end
end
This might make more sense, but it still has problems. In this example, the original filename is used to construct a unique set of filenames for the tiles. The tiling is specified with a tiling vector and a tile size. The image is brute-resized to accomodate. This still results in gross distortion, but I'm assuming you wanted something specific from some files with a particular geometry.
myFolder = 'sources';
filePattern = fullfile(myFolder,'a*.png');
tiling = [2 8];
tilesize = [250 250];
selectedFiles = dir(filePattern);
for k = 1:length(selectedFiles)
baseFileName = selectedFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1,'Now reading %s\n',fullFileName);
shortfilename = regexprep(baseFileName,'\.[^.]+$','');
input = imread(fullFileName) ;
input = imresize(input,tilesize.*tiling);
n = 1;
for j = 1:2
for i = 1:8
row1 = (j-1)*tilesize(1) + 1;
row2 = row1 + tilesize(1) - 1;
col1 = (i-1)*tilesize(2) + 1;
col2 = col1 + tilesize(2) - 1;
sub_im = input(row1:row2 , col1:col2, :);
base_name = sprintf( '%s_%03d.jpg',shortfilename,n);
full_name = fullfile('.', base_name);
fprintf('Saving %s\n', full_name);
imwrite(sub_im, full_name) ;
n = n+1 ;
end
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!