Open a series of images and save them in another folder

1 visualización (últimos 30 días)
Tian Tian
Tian Tian el 17 de Feb. de 2018
Comentada: Tian Tian el 17 de Feb. de 2018
Hi,I want to open images from one folder and save the 8-bit images in another existed folder using original name. Here is my code:
close all; rand('seed',0);
folder = 'F:' files = [folder filesep '\*.tif']
files = dir(files); n = numel(files)
for k=1:n
fullFileName = fullfile(folder, files(k).name);
idx=randi(k);
im = files(idx).name;
afm = imread(fullfile(folder,im));
I8bit =im2uint8(afm);
[pathstr,name,ext] = fileparts(fullFileName) ; filename = strcat(pathstr,name,'.tif');
my_file = "F:\8bit\filename";
imwrite(I8bit,filename)
end
But the images were not able to be saved in that folder, can anyone help me? Another question is my code can only open jpg files if I change hat to '*\.jpg', with tif (as shown above) not able to open. Can anyone give me a hand? Thank you so much in advance.

Respuesta aceptada

Image Analyst
Image Analyst el 17 de Feb. de 2018
Editada: Image Analyst el 17 de Feb. de 2018
You need to have two folder variables, one for input, and one for output. And you need two base file names, one for input and one for output. Then use full file to create two filenames:
fullInputFileName = fullfile(inputFolder, baseInputFileName);
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
baseInputFileName would be files(k).name.
See full demo below:
% Copies all the files from one folder to another folder.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format compact;
% Define input and output folders.
% CHANGE THESE FOLDER NAMES!!!!!!
inputFolder = pwd;
outputFolder = uigetdir(pwd);
if strcmp(outputFolder, inputFolder)
errorMessage = sprintf('Error: the output folder must be different than the input folder');
uiwait(warndlg(errorMessage));
return;
end
% Check to see that both folders exist.
if ~isdir(inputFolder)
errorMessage = sprintf('Error: The following input folder does not exist:\n%s', inputFolder);
uiwait(warndlg(errorMessage));
return;
end
if ~isdir(outputFolder)
errorMessage = sprintf('Error: The following output folder does not exist:\n%s', outputFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of files to copy.
filePattern = fullfile(inputFolder, '*.*'); % All files.
% filePattern = fullfile(inputFolder, '*.m'); % m-files.
fileNamesToTransfer = dir(filePattern);
numFiles = length(fileNamesToTransfer);
% Do the copying.
for k = 1 : numFiles
% Get the base file name.
baseFileName = fileNamesToTransfer(k).name;
% Create the full input and output filenames.
fullInputFileName = fullfile(inputFolder, baseFileName);
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf(1, 'Now copying file #%d of %d: %s to %s\n', ...
k, numFiles, fullInputFileName, fullOutputFileName);
copyfile(fullInputFileName, fullOutputFileName);
end
msgbox('Done copy files!', 'modal');
  2 comentarios
Tian Tian
Tian Tian el 17 de Feb. de 2018
Thank you so much!
Tian Tian
Tian Tian el 17 de Feb. de 2018
May I ask you a question? I strictly followed the rule, but somehow the images were not saved properly, the second image was saved in its original name, but the image was the same as first image.
folder = 'F:\CoTh' files = [folder filesep '\*.tif'] files = dir(files); n = numel(files)
for k=1:n
fullFileName = fullfile(folder, files(k).name);
idx=randi(k);
files(k).name
im = files(idx).name;
afm = imread(fullfile(folder,im));
I8bit =im2uint8(afm);
[pathstr,name,ext] = fileparts(fullFileName); filename = strcat(pathstr,name,'.tif');
outputFolder='F:';
fullOutputFileName = fullfile(outputFolder,filename);
May you please tell me my problem? Thanks in advance.
imwrite(I8bit,filename);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by