how to save/write images using for loop?

조회 수: 5 (최근 30일)
azizullah khan
azizullah khan 2014년 2월 10일
댓글: Soner Kar 2022년 5월 16일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 10일
편집: Azzi Abdelmalek 2014년 2월 10일
for i=1:10
imwrite(x,sprintf('%d.jpg',i))
end;
  댓글 수: 4
ABDULMAJEED ALYAZIDI
ABDULMAJEED ALYAZIDI 2021년 12월 1일
Thanks!
Soner Kar
Soner Kar 2022년 5월 16일
Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Mischa Kim
Mischa Kim 2014년 2월 10일
편집: Mischa Kim 2014년 2월 10일
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
Kamran shahani
Kamran shahani 2016년 11월 20일
편집: Image Analyst 2016년 11월 20일
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');
Image Analyst
Image Analyst 2016년 11월 20일
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);

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2014년 2월 10일
  댓글 수: 6
Muhammad Hammad Malik
Muhammad Hammad Malik 2018년 9월 10일
편집: Walter Roberson 2018년 9월 10일
i tried your code but still getting error
Image Analyst
Image Analyst 2018년 9월 10일
Uh, okay.... So think about what you might be able to do, if you were in our shoes, to help us to help you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by