Not able to use imread on images in subfolders

4 ビュー (過去 30 日間)
Leo
Leo 2024 年 10 月 11 日
コメント済み: Voss 2024 年 10 月 11 日
I compiled an application for the purpose of comparing the histogram of two different images.
This is to be used in manufacturing where daily we're adding a new folder and create new images that will need to be compared.
I put the Matlab application in the parent folder but I keep getting the attached error that "Unable to find file" when I'm using uigetfile in my program to select files (which are in subfolders). I had a fix in the matlab application by using the following:
SearchPath = uigetdir('C*');
addpath(SearchPath)
It worked until I compiled the program. "addpath" does not work in Compiler.
I can't add in all these subfolders to the paths because mutliple subfolders get added everyday.
Is there anyway for imread to work on reading subfolders? Is there something I'm missing?
  1 件のコメント
Stephen23
Stephen23 2024 年 10 月 11 日
編集済み: Stephen23 2024 年 10 月 11 日
"Is there anyway for imread to work on reading subfolders?"
Of course: use absolute/relative filenames. FULLFILE is key here.
"Is there something I'm missing?"
The MATLAB Search Path is for code, not for data files:
  • Do not pollute the Search path with folders filled with data files.
  • Do not change the Search Path just to access data files.
Use absolute/relative filenames.

サインインしてコメントする。

採用された回答

Voss
Voss 2024 年 10 月 11 日
Take the first two outputs from uigetfile (the second output is the location of the selected file), and construct the full path file name using fullfile.
Example:
[filename,pathname] = uigetfile('*_PRE DUT.tif');
if isequal(filename,0) % user cancelled
return
end
fullPreDutName = fullfile(pathname,filename);
[filename,pathname] = uigetfile('*_POST DUT.tif');
if isequal(filename,0) % user cancelled
return
end
fullPostDutName = fullfile(pathname,filename);
% ...
preDUTimage = im2uint8(imread(fullPreDutName));
% ...
postDUTimage = im2uint8(imread(fullPostDutName));
% ...
No need for addpath anywhere.
  2 件のコメント
Leo
Leo 2024 年 10 月 11 日
This worked perfectly! Thank you so much.
Voss
Voss 2024 年 10 月 11 日
You're welcome!

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2024 年 10 月 11 日
No, there is no way to get imread() to work on reading subfolders.
You should be using roughly
SearchPath = uigetdir('C*');
if isnumeric(SearchPath)
return; %user cancel
end
dinfo = dir(SearchPath);
dinfo([dinfo.isfolder]) = []; %get rid of . and .. and other folders
files = fullfile({dinfo.folder}, {dinfo.name});
numfiles = numel(files);
all_images= cell(numfiles,1);
used_images = 0;
for K = 1 : numfiles
thisfile = files{K};
try
thisimage = imread(thisfile);
used_images = used_images + 1;
all_images{used_images} = thisimage;
catch ME
end
end
all_images = all_images(1:used_images);

Steven Lord
Steven Lord 2024 年 10 月 11 日
Instead of adding the path to the file that contains your data to the MATLAB search path, construct the full path to the data files (or the directory) using fullfile. I'm setting SearchPath to a known fixed string because MATLAB Answers doesn't support uigetdir, but your application could use that instead.
SearchPath = fullfile(matlabroot, 'toolbox', 'matlab', 'general')
SearchPath = '/MATLAB/toolbox/matlab/general'
Now to construct the path to one of the files in that directory, say bench.dat:
benchDataPath = fullfile(SearchPath, 'bench.dat')
benchDataPath = '/MATLAB/toolbox/matlab/general/bench.dat'
Can I use this directory to access the file?
lines = readmatrix(benchDataPath, Range='1:3', OutputType = "string", Delimiter="")
lines = 2x1 string array
"MATLAB(R) Benchmark Data." "Copyright 1984-2024 The MathWorks(R), Inc."
[The output skipped an empty line; lines(2) is actually the third line of the file.]

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by