reading mutiple images one by one without overwriting
Mostrar comentarios más antiguos
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
Rick Rosson
el 17 de Sept. de 2014
- 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
el 17 de Sept. de 2014
Image Analyst
el 17 de Sept. de 2014
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
el 17 de Sept. de 2014
Joseph Cheng
el 17 de Sept. de 2014
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
el 17 de Sept. de 2014
Editada: Image Analyst
el 17 de Sept. de 2014
Michael Haderlein
el 17 de Sept. de 2014
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
el 17 de Sept. de 2014
Michael Haderlein
el 17 de Sept. de 2014
So what does
dir('*.gif')
return?
vetri
el 17 de Sept. de 2014
Michael Haderlein
el 17 de Sept. de 2014
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.
Respuestas (4)
Image Analyst
el 17 de Sept. de 2014
0 votos
Why would reading a file overwrite it? imread() does not do writing.
Anyway, see sample code in the FAQ: http://matlab.wikia.com/wiki/FAQ?title=FAQ&cb=6868#How_can_I_process_a_sequence_of_files.3F
14 comentarios
Image Analyst
el 17 de Sept. de 2014
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
el 17 de Sept. de 2014
Michael Haderlein
el 17 de Sept. de 2014
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
el 17 de Sept. de 2014
vetri
el 17 de Sept. de 2014
Michael Haderlein
el 17 de Sept. de 2014
Have you ever had a look on the duplicate question? Image Analyst was even so kind to put the link in his comment.
Image Analyst
el 17 de Sept. de 2014
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.
Michael Haderlein
el 18 de Sept. de 2014
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
Image Analyst
el 18 de Sept. de 2014
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.
Image Analyst
el 18 de Sept. de 2014
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
el 18 de Sept. de 2014
Michael Haderlein
el 18 de Sept. de 2014
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.
Image Analyst
el 18 de Sept. de 2014
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.
Michael Haderlein
el 18 de Sept. de 2014
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.
Michael Haderlein
el 18 de Sept. de 2014
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
Michael Haderlein
el 18 de Sept. de 2014
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
el 18 de Sept. de 2014
vetri
el 18 de Sept. de 2014
Michael Haderlein
el 18 de Sept. de 2014
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
el 18 de Sept. de 2014
Michael Haderlein
el 18 de Sept. de 2014
can you please post the exact code?
Michael Haderlein
el 18 de Sept. de 2014
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
el 18 de Sept. de 2014
Michael Haderlein
el 18 de Sept. de 2014
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
el 18 de Sept. de 2014
Image Analyst
el 18 de Sept. de 2014
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
el 18 de Sept. de 2014
Editada: Image Analyst
el 18 de Sept. de 2014
Image Analyst
el 18 de Sept. de 2014
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.
Image Analyst
el 18 de Sept. de 2014
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?
Image Analyst
el 18 de Sept. de 2014
I gave you code above. I'm not sure why you're re-asking.
vetri
el 19 de Sept. de 2014
0 votos
4 comentarios
Mohammad Abouali
el 19 de Sept. de 2014
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
el 19 de Sept. de 2014
Michael Haderlein
el 19 de Sept. de 2014
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.
Michael Haderlein
el 19 de Sept. de 2014
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);
vetri
el 19 de Sept. de 2014
0 votos
Categorías
Más información sobre Convert Image Type en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!