for i=1:10
imwrite(x,'i.jpg') % x be a image to write
end;
how to vary i that different images can be saved?

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 10 Feb. 2014
Bearbeitet: Azzi Abdelmalek am 10 Feb. 2014

12 Stimmen

for i=1:10
imwrite(x,sprintf('%d.jpg',i))
end;

4 Kommentare

sumit kumar
sumit kumar am 26 Mai 2017
Thank you sir....
Thara C.S
Thara C.S am 9 Dez. 2018
sir,
if it in the case of geotiffwrite?
can use the same code?
Thanks!
Soner Kar
Soner Kar am 16 Mai 2022
Thanks a lot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Mischa Kim
Mischa Kim am 10 Feb. 2014
Bearbeitet: Mischa Kim am 10 Feb. 2014

5 Stimmen

Azizullah, the code snippet below shows you how to save an (or several) images using a loop.
N = 4;
A = imread('my_img.png');
for ii = 1:N
imwrite(A,strcat('my_new',num2str(ii),'.png'));
end
The strcat command creates (oncatenates) a new string for each image containing a number (as a possible image identifier).

3 Kommentare

azizullah khan
azizullah khan am 10 Feb. 2014
thanks bro......................
Kamran shahani
Kamran shahani am 20 Nov. 2016
Bearbeitet: Image Analyst am 20 Nov. 2016
bro and how I read and write multiple images I have code when i select two pictures, it give me error :
Error in imread (line 316)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
%improve underwater image
tic;
[filename, pathname, filterindex] = uigetfile( ...
{
'*.*', 'All Files (*.*)'}, ...
'Pick a file', ...
'MultiSelect', 'on');
if isequal(filename,0)
disp('User selected Cancel')
else
disp(['User selected', fullfile(pathname, filename)])
end
Il=imread(filename);
% imshow(Il), title('original');
RGB=Il;
cform2lab=makecform('srgb2lab');
LAB= applycform(RGB, cform2lab);
%image onvert in to L*A*B color space
L=LAB(:,:,1);
LAB(:,:,1)=adapthisteq(L,'cliplimit', 0.02, 'Distribution', 'rayleigh');
cform2srgb=makecform('lab2srgb');
% convert back to RGB
A=applycform(LAB, cform2srgb);
t=toc,
imwrite(A,strcat('a','.jpg'))
subplot(2,2,1), imshow(RGB), title('BEFOR CLAHE');
subplot(2,2,2), imshow(A), title('AFETR CLAHE');
That's because you constructed the full filename when you used disp() but forgot to use it when you called imread. Also, multiselect needs to be off, not on since imread() can't open multiple files at once. To fix:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
fprintf('You clicked the Cancel button.\n');
return;
end
fullFileName = fullfile(folder, baseFileName)
fprintf('You chose file %s.\n', fullFileName);
% Read in the file.
Il = imread(fullFileName);

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 10 Feb. 2014

0 Stimmen

6 Kommentare

Muhammad Hammad Malik
Muhammad Hammad Malik am 20 Aug. 2018
Bearbeitet: Walter Roberson am 10 Sep. 2018
i am using this code
N = 4; A = imread('my_img.png'); for ii = 1:N imwrite(A,strcat('my_new',num2str(ii),'.png'));
but when i run this code it is saving multiple images but when again i run this, it overwrite the images on previous ones instead creating the new images. so how to do that? thanks
Try this:
N = 4;
% A = imread('my_img.png');
A = imread('cameraman.tif');
titleBarCaption = 'Overwrite?';
for ii = 1 : N
baseFileName = sprintf('my_new%d.png', ii);
fullFileName = fullfile(pwd, baseFileName);
if exist(fullFileName, 'file')
promptMessage = sprintf('%s exists already.\nDo you want to overwrite %s?', baseFileName);
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(buttonText, 'Yes')
imwrite(A, fullFileName);
fprintf('Wrote %s\n', fullFileName);
else
fprintf('Skipped %s\n', fullFileName);
end
else
imwrite(A, fullFileName);
fprintf('Wrote %s\n', fullFileName);
end
% recycle on;
% delete(fullFileName);
end
Muhammad Hammad Malik
Muhammad Hammad Malik am 27 Aug. 2018
Bearbeitet: Muhammad Hammad Malik am 27 Aug. 2018
thanks. i tried but getting error.i want to know that what is 'fullFileName' here? and i want to capture images at different time but each time new captured image replaced with old image, i want to keep all images
fullFileName is what you did like this:
strcat('my_new',num2str(ii),'.png')
but with the folder prepended.
Muhammad Hammad Malik
Muhammad Hammad Malik am 10 Sep. 2018
Bearbeitet: Walter Roberson am 10 Sep. 2018
i tried your code but still getting error
Image Analyst
Image Analyst am 10 Sep. 2018
Uh, okay.... So think about what you might be able to do, if you were in our shoes, to help us to help you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by