reading mutiple images one by one without overwriting

to read multiple images one by one from folder without overwriting.i was new to matlab so plz provide me coding to read image1,image2...image89

11 comentarios

  • Are the images color or grayscale?
  • Are all of the images the same size in terms of pixels (rows x columns)?
  • What is the file format and extension?
vetri
vetri el 17 de Sept. de 2014
It's a grayscale image with the same size of 512*512.it's in gif format
Why are you ignoring my answer below and the answers in your original, duplicate question at http://www.mathworks.com/matlabcentral/answers/155140-how-to-read-mutiple-images-from-folder-without-overwriting-of-matrix-and-images ??? Your comment seems to indicate you're not reading or paying attention to any of the answers you get.
vetri
vetri el 17 de Sept. de 2014
FAQ sample code giving error, that image doesn't exit even the correct path is given for image
So the best thing to do is comeback to the post with the error and let us see where the sample code was implemented incorrectly for your application.
vetri
vetri el 17 de Sept. de 2014
Editada: Image Analyst el 17 de Sept. de 2014
for loop=1:5
gifFileName = strcat('image', num2str(loop), '.gif');
if exist(gifFileName, 'file')
imageData = imread(gifFileName);
else
fprintf('File %s does not exist.\n', gifFileName);
end
end
you are sure that the files are in the correct folder? I.e. the files are in the current folder? What does
dir('*.gif')
return?
vetri
vetri el 17 de Sept. de 2014
yes it is in correct folder only..image is in gif format
So what does
dir('*.gif')
return?
vetri
vetri el 17 de Sept. de 2014
am getting error as image does not exist
I believe the files are just not in the current folder. You can get the path of the current folder by
pwd
Also, these files must appear in the current folder window. I suppose they don't. Either copy the files into this folder or construct the full file name as Image Analyst has suggested.

Iniciar sesión para comentar.

Respuestas (4)

Image Analyst
Image Analyst el 17 de Sept. de 2014
Why would reading a file overwrite it? imread() does not do writing.

14 comentarios

In your comment above, you're not constructing the full filename so it expects the files to be in the current folder. Use the second example from the FAQ instead.
vetri
vetri el 17 de Sept. de 2014
in the second examples the matrix values are overwriting...it doesnot display matrix for each image..
No, it's not overwriting. It's overdrawing. Now comes the programming part for you: combine the second example with my comment in the duplicate question.
vetri
vetri el 17 de Sept. de 2014
I do not want over drawing image ..I want to display all images and their corresponding matrix ...what to do??what u said in the above I can't understand
vetri
vetri el 17 de Sept. de 2014
help me with small example ..
Have you ever had a look on the duplicate question? Image Analyst was even so kind to put the link in his comment.
It now appears that by overwrite you mean something having to do with the display of the image and possibly some graphics or image in the overlay, rather than overwriting the disk file. Is that correct? If you put "cla" before any call to imshow() or image() then all old stuff in the axes will be cleared out and new images will not appear on top of (piled onto and covering up) the last (currently displayed) image. Is that what you mean. Otherwise please clarify, perhaps with some screenshots.
A little example (I cannot test it for the moment, but I think it should work):
figs=dir('imagefolder\image*.gif');
for cnt=1:length(figs)
image=imread(['imagefolder\' figs(cnt).name]);
subplot(1,length(figs),cnt)
imshow(image)
end
vetri's "Answer" moved here:
figs=dir('C:\Users\swathi\Desktop\images*.gif');
for cnt=1:length(figs)
image=imread(['images' figs(cnt).name]);
subplot(1,length(figs),cnt)
imshow(image)
end
it doesn't return any value.
You messed up the names. Try this:
folder = 'C:\Users\swathi\Desktop\images';
filePattern = fullfile(folder, '*.gif');
figs = dir(filePattern);
for cnt=1:length(figs)
thisFileName = fullfile(folder, figs(cnt).name);
thisImage =imread(thisFileName);
subplot(1,length(figs),cnt)
imshow(thisImage)
end
By the way, don't use image for the name of a variable - it's already a built-in function name.
vetri
vetri el 18 de Sept. de 2014
thank u.its working
ok, not to use "image" as variable name - agree. But what is now the difference to the answer he posted, which was then deleted, became a comment on my answer, from where I copied it before it was deleted again and now is here? I mean, apart from using fullfile instead of []? I'm just wondering, because I don't have no idea what could have been the error.
My guess is that he didn't have any gif files called images*.gif in the desktop folder. My guess is that there was probably a folder called "images" on the desktop, so he was missing a slash between the images and the asterisk. If so, figs would be empty since it did not find the files . I put that / in and then used fullfile() to be more robust and explicit.
Ah, I see. I always thought 'image' would be part of the file name. Thanks for pointing me on that. I seriously became doubtful about my Matlab skills.

Iniciar sesión para comentar.

Because you have to tell imread where to find the file.
figs=dir('C:\Users\swathi\Desktop\image*.gif');
for cnt=1:length(figs)
image=imread(['C:\Users\swathi\Desktop\' figs(cnt).name]);
subplot(1,length(figs),cnt)
imshow(image)
end

15 comentarios

Please don't respond on previous answers with new answers but rather as comments. This thread is already difficult enough to read.
What is the answer on
length(figs)
?
And are you sure that there is more than one file on your desktop which has a proper name (image(something).gif)?
vetri
vetri el 18 de Sept. de 2014
yes image name is in proper format only but I want to display all images and corresponding matrix in an array
vetri
vetri el 18 de Sept. de 2014
length(figs)=30
Please make a breakpoint inside the loop and go through the loop step by step (F10 will run the next line only). Check if
- it iterates 30 times
- it displays the current image and then possibly deletes it
In any case, 30 images is maybe too much to display it in one line. Better use
subplot(5,6,cnt)
instead.
What do you mean with "display corresponding matrix in an array"?
vetri
vetri el 18 de Sept. de 2014
in that coding atlast only one image is displayed with matrix value.so I want all images to be displayed
can you please post the exact code?
I feel free to repost your code in a readable manner:
figs=dir('C:\Users\swathi\Desktop\image*.gif');
for cnt=1:length(figs)
image=imread(['C:\Users\swathi\Desktop\' figs(cnt).name]);
subplot(1,length(figs),cnt)
subplot(5,6,cnt)
imshow(image)
end
The first subplot-line is wrong, please delete this. The rest of the code looks totally fine. You have written that the counter is not incrementing. I cannot see why it shouldn't. If, what you say, length(figs) is 30 (I hope you didn't count the files in the windows explorer but used the Matlab function), then it must go from 1 to 30. I have no idea what could be wrong with that.
vetri
vetri el 18 de Sept. de 2014
after performing some operation the value of each image should be stored in array..how to code for that
Is "the value" one scalar or a 1-D array or a 2-D array or of the same size as the image, will it be the same size for every image or might there be differences? The best solution depends on this.
vetri
vetri el 18 de Sept. de 2014
the value is in 1-D .I want to change to2-D
Try
allResults = zeros(numberOfImages, 3); % Measure 3 things for each image.
for k = 1 : numberOfImages
% Code to read images: imread(), etc.
% Now measure the 3 things you want to measure with some function
theseResults = AnalyzeSingleImage(theImage);
% Append to the 2D array of results from all the images.
allResults(k,:) = theseResults; % Stick the 3 numbers into the 2D array
end
vetri
vetri el 18 de Sept. de 2014
Editada: Image Analyst el 18 de Sept. de 2014
Undefined function 'AnalyzeSingleImage' for input arguments of type 'uint8'.is the error shown for above program
for k = 1 : 5
% Code to read images: imread(), etc.
folder = 'C:\Users\swathi\Desktop\images';
filePattern = fullfile(folder, '*.gif');
figs = dir(filePattern);
for cnt=1:length(figs)
thisFileName = fullfile(folder, figs(cnt).name);
thisImage =imread(thisFileName);
subplot(1,length(figs),cnt)
imshow(thisImage)
end
% Now measure the 3 things you want to measure with some function
theseResults = AnalyzeSingleImage(thisImage);
% Append to the 2D array of results from all the images.
allResults(k,:) = theseResults; % Stick the 3 numbers into the 2D array
end
AnalyzeSingleImage(theImage) is some function you have to write of course. I have no idea how you got your 1D results. All you said was "the value is in 1-D", so how did you get that 1D vector? I don't know but you must. Maybe you had some function that returned your 1D vector, or maybe you just did 14 different operations right there in the for loop - I have no idea.
And, just to be super clear, even though I, in my example, called your 1D vector "theseResults" you might have called it something completely different. Use whatever name you gave to the vector instead of the name I gave. The point is, you need to adapt the code to your situation.
vetri's "Answer" moved here since it's not an "Answer" to his question but actually just a follow up (and duplicate) question:
i want to read 1st image perform some operation and store in an array and then next and next .what to do?
I gave you code above. I'm not sure why you're re-asking.

Iniciar sesión para comentar.

vetri
vetri el 19 de Sept. de 2014
I want to read all images and perform some operation and all image result must be store in an array..but in this it is storing last image value only.

4 comentarios

folderPath='~/myimageFolder'; %if on Linux or Mac
% folderPath='C:\myImageFolder'; %if on Windows
imageExt='jpg';
fileList=dir([folderPath filesep '*.' imageExt]);
nImage=1;
for i=1:numel(fileList)
if (~fileList(i).isdir)
image{nImage}=imread([folderPath filesep fileList(i).name]);
nImage=nImage+1;
end
end
This code, (which was provided before) reads all image and keep all of them in the memory, not just the last one. You can access any of those image later by passing its number. For example to get 3rd image type image{3}. To get 5th image pass image{5} and so on.
NOTE: as "image analyst" has mentioned "image" is also a matlab function so changing the variable name to something else is recommended.
vetri
vetri el 19 de Sept. de 2014
but I have to show the result for all images at a time
Ok, please: Write exactly what you want. This thread now has 4 answers and in total 44 comments (this one here not included). At least 3 people have tried to help including Image Analyst who is one of the most experienced users here in this forum. Most likely, it's not complicated what you want to do. But we're struggling to understand what you want.
"after performing some operation the value of each image should be stored in array..how to code for that"
"but I have to show the result for all images at a time"
Do you want to store the result or do you want to show it? If you want to show it, where? In the command window? In the title of each image? Inside each image? In a separate figure? We cannot know that.
vetri's answer on the comment above:
"after performing some operation i want to store the result of each image in an array.but in my coding for one image result is store in an array...thank u very much for your helps"
That is exactly what Image Analyst's code does: http://www.mathworks.com/matlabcentral/answers/155155#comment_237898
In his code, there is the comment
% Code to read images: imread(), etc.
You need to replace this line by the lines which read the image. See all the codes before. There is no need to create a second loop.
Then, you need to do this operation on the image which was abbreviated by
theseResults = AnalyzeSingleImage(theImage);

Iniciar sesión para comentar.

vetri
vetri el 19 de Sept. de 2014
after performing some operation i want to store the result of each image in an array.but in my coding for one image result is store in an array...thank u very much for your helps

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Sept. de 2014

Comentada:

el 19 de Sept. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by