- replaced fragile string concatenation with the recommended and robust fullfile.
- replaced fragile '.' matching to locate file extension with the recommended and robust fileparts.
- replaced regexprep and num2str with sprintf.
How to rename an image after processing with different parameters
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Patrice Gaofei
el 15 de Oct. de 2020
Editada: Patrice Gaofei
el 15 de Oct. de 2020
Hello dear programmers,
I have a sequence of images and I would like to perform dilation on each of them with different dilation parameters. Then, I would like to save the processed images with new name including both the old name and the corresponding dilation parameter. My codes are as follows.
```
Input_folder =
Output_folder =
D = dir([Input_folder '*.jpg']);
Inputs = {D.name}';
Outputs = Inputs; % preallocate
%print(length(Inputs));
for k = 1:length(Inputs)
X = imread([Input_folder Inputs{k}]);
dotLocations = find(Inputs{k} == '.');
name_img = Inputs{k}(1:dotLocations(1)-1);
image1=im2bw(X);
vec = [3;6;10];
vec_l = length(vec);
for i = 1:vec_l
%se = strel('disk',2);
fprintf(num2str(vec(i)));
se = strel('diamond',vec(i)); %imdilate
im = imdilate(image1,se);
image2 = im - image1;
Outputs{k} = regexprep(Outputs{k}, name_img, strcat(name_img,'_', num2str(vec(i))));
imwrite(image2, [Output_folder Outputs{k}])
end
end
```
As it can be seen, I would like to apply dilation with parameters 3,6 and 10. Let us assume that an image has as name "image1", after processing it, I would like to have "image1_3", "image1_6" and "image1_10". However, I am getting as results "image1_3", "image1_6_3" and "image1_10_6_3". Please, how can I modify my codes to fix this problem?
0 comentarios
Respuesta aceptada
Stephen23
el 15 de Oct. de 2020
Editada: Stephen23
el 15 de Oct. de 2020
The cause of the problem is that your inner loop repeatedly processes the same string of the cell array Outputs. So you just keep applying regexprep to the same name, each time adding to whatever you did on the last loop iteration.
The simplest solution would be to NOT use the cell array Outputs at all, but to always start again from the raw (input) filename, e.g. something like this (note how the filename N and extension E do not change in the inner loop):
Input_folder =
Output_folder =
S = dir(fullfile(Input_folder,'*.jpg'));
for ii = 1:numel(S)
F = S(ii).name;
X = imread(fullfile(Input_folder,F));
I = im2bw(X);
[~,N,E] = fileparts(F);
V = [3;6;10];
for jj = 1:numel(V)
se = strel('diamond',V(jj)); %imdilate
J = imdilate(I,se);
Y = I - J;
G = sprintf('%s_%d%s',N,V(jj),E);
imwrite(Y,fullfile(Output_folder,G))
end
end
Note that I also made some other improvements to your code, namely:
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!