creating a loop to input photos into the code

조회 수: 1 (최근 30일)
Kevin Paterson
Kevin Paterson 2021년 1월 28일
답변: Mathieu NOE 2021년 1월 29일
I am very new to Matlab. I am trying to create a loop to input photos in, so I do not have to put in imread in 100 times. The file all the photos are in is called "Pathtoimages". I am trying to upload photos to have the machine recongizie and classify if a photo is a pengiun or a dog. I am not sure how to set this up. Any Help would be greatly appreiated . this is what I found on the internet but I do not understand sprintf or the "pentan1h_6uLmin_0%3d.tif" ( how do you create a file like that or is there a way to do it with a .m file?) . Any help would be greatly appreciated. Thank you.
for n=1:10
images{n}= imread(sprintf('pentan1h_6uLmin_0%3d.tif',n));
imshow(Images{n},[],'initialmagnification','fit');
end

답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 1월 29일
hello
this code example reads all tif files in the current directory
the for loop will open each individual file and do a few operations (like cropping here)
it's up to you to adapt it to your specific needs....
% select appropriate options below and adapt code to your specific needs ...
% files = dir('*.tif'); % list all tif files in current directory
files = dir('handheld*.tif'); % as example : only list tiff files with "handheld" in the filename
% % resize options (sacle factor or pixels)
% scale_factor = 0.5;
% select portion of the image
% 451*279*3. I will like to extract a portion with dimensions of 120*80*3
x_dim = 120;
y_dim = 80;
x_offset = 100; % please make sure the "corner" of the cropped image is correct
y_offset = 50;
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% % resize
% H{ck}= imresize(img, scale_factor);
% select (crop)
H{ck}= img(x_offset+(1:x_dim),y_offset+(1:y_dim),: );
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
end

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by