Excluding types of extension files on a loop
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am doing my academic research, it is based on medical image processing. My code will go into a folder path and handle all ".jpg" images, however inside the folder I have some files that I wouldn't want to read and I can't remove from there, as my database is too large, I need skip these files through code to not do manually.
clc;
close all;
mainpath = 'path';
original= dir(mainpath);
quantity = length(original);
x = zeros(14,4,quantity);
figures = 1;
for i=1:quantity
imagename=original(i).name;
w(i)={imagename};
[path, filename, ext]=fileparts(imagename);
if (ext == extensao)
img = imread([mainpath,'',imagename]);
[l, c, canais] = size(img);
% if (canais==3)
% img = rgb2gray(img);
% end
maximo = max(max(img));
minimo = min(min(img));
[glcms,SI] = graycomatrix(img,'NumLevels',maximo-minimo,'Offset' ,[0 1],'GrayLimits', [minimo maximo], 'Symmetric', true);
soma = sum(sum(glcms));
glcms = glcms/soma;
x(:,1,figures) = haralickTextureFeatures(glcms);
i = 2;
for angulo = 1:-1:-1
[glcms,SI] = graycomatrix(img,'NumLevels',maximo-minimo,'Offset' ,[-1 angulo],'GrayLimits', [minimo maximo], 'Symmetric', true);
soma = sum(sum(glcms));
glcms = glcms/soma;
x(:,i,figures) = haralickTextureFeatures(glcms);
i = i+1;
end
figures = figures+1;
else
figures = figures+1;
end
end
filename = 'Resultado Test.xlsx';
matrizFinal = zeros(4,quantity);
for j = 1:14
k=1;
for i=1:quantity;
matrizFinal(:,i) = x(j,:,i)
name(i)=w(k)
k=k+1
end
xlswrite(filename,nome,j,'A1');
xlswrite(filename, matrizFinal,j,'A2');
end
0 comentarios
Respuestas (3)
Walter Roberson
el 4 de Oct. de 2021
original = dir( fullfile(mainpath, '*.jpg'));
now it will only find jpg files
Akira Agata
el 4 de Oct. de 2021
You can do that more effective way by using wild card character "*", like:
imgFolder = pwd; % <- set to your image folder path
imgList = dir(fullfile(imgFolder,'*.jpg'));
for kk = 1:numel(imgList)
filePath = fullfile(imgList(kk).folder, imgList(kk).name);
I = imread(filePath);
% [Do something to each image]
end
Image Analyst
el 5 de Oct. de 2021
It's best to do what the others showed you and just get a list that has only JPG files in it in the first place. However just for completeness, I'm going to show you how to skip them in the loop
for i=1:quantity
imagename=original(i).name;
if contains(imagename, '.jpg', 'IgnoreCase', true)
continue; % Skip this one.
end
Now, next is the issue of using JPG images in the first place. DON"T DO IT! If you can at all help it, never use JPG images for image analysis because it is a lossy compression format and you won't necessarily recall the same pixel values that the image had before it was saved to disk. Use PNG (lossless compression) or TIF or BMP.
2 comentarios
Image Analyst
el 5 de Oct. de 2021
Strange. I thought usually medical images were dicom. Are you sure these are genuine, original medical images?
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!