Excluding types of extension files on a loop

8 次查看(过去 30 天)
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
Unrecognized function or variable 'quantidade'.

回答(3 个)

Walter Roberson
Walter Roberson 2021-10-4
original = dir( fullfile(mainpath, '*.jpg'));
now it will only find jpg files
  1 个评论
Flávio Nóbrega
Flávio Nóbrega 2021-10-5
Thank you, it worked right away, so simple, i remember that in the beginning of writing code i tried this but i was getting the same error message and i removed.
:)

请先登录,再进行评论。


Akira Agata
Akira Agata 2021-10-4
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
Image Analyst 2021-10-5
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 个评论
Flávio Nóbrega
Flávio Nóbrega 2021-10-5
I see, thank you for that, it was very helpful. As for the image type, my database only contains .jpg images, if you have any recommendations for a .png database of lung disease patients it would help.
:)
Image Analyst
Image Analyst 2021-10-5
Strange. I thought usually medical images were dicom. Are you sure these are genuine, original medical images?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by