How to read images in a folder

조회 수: 529 (최근 30일)
Nilushi Dissanayake
Nilushi Dissanayake 2011년 1월 25일
답변: DGM 2023년 6월 24일
I am a student and I need to find the code to read the images in a file in order to do cropping. In here I named my images as
user001-01.bmp
user001-02.bmp etc.
I need help for this because I'm a beginner to MATLAB.
  댓글 수: 7
naila
naila 2023년 6월 24일
hi,
i have some jpeg images in a folder (saved in C drive),using imread command in matlab 2018 but its not working.
please help me out.
Image Analyst
Image Analyst 2023년 6월 24일
If you have any more questions, then attach your image and code to read it in with the paperclip icon after you read this:
Do it in your own new question rather than here in @Nilushi Dissanayake's question. And "not working" is so vauge as to be unanswerable. Explain what that means, like give us ALL the red text.

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

채택된 답변

Michelle Hirsch
Michelle Hirsch 2011년 1월 25일
Nilushi -
We won't be able to hand you a complete answer, but can give some pointers. Here's a code snippet that shows common patterns for reading data from files. There are a bunch of concepts you are going to want to learn about to understand this code:
  • Working with structures. When you get a list of image files, their names will be stored inside of a structure.
  • Working with cell arrays. We'll use a cell array to store the data coming from the different images, since it allows for each image to be a different size. If you happen to know that all of your images are exactly the same size you can use a regular numeric array instead.
% Get list of all BMP files in this directory
% DIR returns as a structure array. You will need to use () and . to get
% the file names.
imagefiles = dir('*.bmp');
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
images{ii} = currentimage;
end
Best of luck! - scott
  댓글 수: 10
Hasaan Ijaz
Hasaan Ijaz 2020년 9월 12일
Worked for me as well. Thank you!
Javaid Iqbal
Javaid Iqbal 2022년 2월 28일
Thanks a lot

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

추가 답변 (4개)

Sebastian
Sebastian 2011년 1월 25일
There might be different ways to do this. An easy one is
for a = 1:10
filename = ['user001-' num2str(a,'%02d') '.bmp'];
img = imread(filename);
% do something with img
end
  댓글 수: 4
artprakasa
artprakasa 2017년 3월 13일
yeaayy.. thanks, good idea.
Liu Sam
Liu Sam 2018년 3월 4일
Thank you! This is the exact answer I want!

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


Walter Roberson
Walter Roberson 2011년 1월 25일
Please check the FAQ on this topic
  댓글 수: 1
Nilushi Dissanayake
Nilushi Dissanayake 2011년 1월 25일
thank you very much,,

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


George
George 2016년 9월 24일
You can try ImageDatastore
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 9월 11일
To be slightly more accurate, the imageDatastore function needs R2016a or later, but the imageDatastore class was introduced in R2015b and objects could be created by calls to datastore.
Walter Roberson
Walter Roberson 2017년 9월 11일
Before R2015a, the closest equivalent was the dataset array from the Statistics toolbox.

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


DGM
DGM 2023년 6월 24일
There are already many examples that I don't need to repeat, but I have my own ways of doing things. I'm only throwing this here because this is a defacto reference question.
Pay particular note to the latter part of the answer.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by