Excluding types of extension files on a loop

조회 수: 8 (최근 30일)
Flávio Nóbrega
Flávio Nóbrega 2021년 10월 4일
댓글: Image Analyst 2021년 10월 5일
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