Problem to load tiff files using for loop
Afficher commentaires plus anciens
Hi all,
I would like to reshape several (N = 12) tiff files and do that through a for loop to go quicker than doing it one by one. Find below the code I wrote, however an error appeared, which said that only Mat and ASCII can be open this way. DOes anybody knows how to do it for .tiff files, please?
Error using load
Unable to read file
'E:\Data_emodnet_fisheries\Vessel_density\2017_01_st_01.tif'. Input
must be a MAT-file or an ASCII file containing numeric data with
same number of columns in each row.
Error in code (line 15)
t = load(fullfile(cd, file));
Regards
close all
clear all
clc
% assign the path to your working directory:
cd ('E:\Data_emodnet_fisheries\Vessel_density\');
file = '2017_##_st_01.tif';
file1 = '##.mat';
for it = 1:12
file(6:7) = sprintf('%02.0f',it);
file1(1:2) = sprintf('%02.0f',it);
t = load(fullfile(cd, file));
imageData = read(t);
new_lim = imageData(2700:4000, 3500:4700);
save(file1,'new_lim');
end
3 commentaires
Mathieu NOE
le 15 Mar 2022
hello
FYI - adapt to your own needs...
all the best
% select appropriate options below and adapt code to your specific needs ...
% files = dir('*.tif'); % all tif files in current directory
files = dir('handheld*.tif'); % as example : only 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
Jan
le 15 Mar 2022
The brute clearing header "close all, clear all, clc" is rarely useful. Especially clear all has no benefit, but wastes a lot of time.
"which said that only Mat and ASCII can be open this way" - please post a copy of the error message, because the details matter. This is much better than paraphrasing the message.
Jonathan Demmer
le 15 Mar 2022
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!