Excluding types of extension files on a loop

12 Ansichten (letzte 30 Tage)
Flávio Nóbrega
Flávio Nóbrega am 4 Okt. 2021
Kommentiert: Image Analyst am 5 Okt. 2021
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'.

Antworten (3)

Walter Roberson
Walter Roberson am 4 Okt. 2021
original = dir( fullfile(mainpath, '*.jpg'));
now it will only find jpg files
  1 Kommentar
Flávio Nóbrega
Flávio Nóbrega am 5 Okt. 2021
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.
:)

Melden Sie sich an, um zu kommentieren.


Akira Agata
Akira Agata am 4 Okt. 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
Image Analyst am 5 Okt. 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 Kommentare
Flávio Nóbrega
Flávio Nóbrega am 5 Okt. 2021
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 am 5 Okt. 2021
Strange. I thought usually medical images were dicom. Are you sure these are genuine, original medical images?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by