Open a series of images and save them in another folder

1 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2018-2-17
编辑:Image Analyst 2018-2-17
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 个评论
Tian Tian
Tian Tian 2018-2-17
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by