Allowing users to open up specific images during execution of code

1 view (last 30 days)
I am coding a project to allow sets images to be segmented, I want the user to be able to import a folder that contains the images, similar in style to the import data function in matlab located in the home tab. I then want to make a dir structure out of this, however I know how to do this.
  3 Comments
DGM
DGM on 16 Nov 2023
Edited: DGM on 16 Nov 2023
I never use it myself, but maybe imageDatastore() is a thing you could use.
Generally, blindly trying to read any random images that might be in a directory can become problematic if you're unable to predict what sorts of images might be present. Size, class, color type, channel count, framecount, orientation could vary. I'm assuming your cases will be fairly predictable and consistent, but it's good to be cautious.
That said, if you hope someone else will give you a "read any images in this directory" code, you'll probably want to narrow down the specific sorts of images you hope to support/exclude.
Consider the answers here:
My own answer there emphasizes input flexibility, but it depends on a FEX library, and is not compatible with an incremental workflow (i.e. operating on one file at a time). Either way, mimread() is ~280 lines of code. That's an example of the volume of code needed to handle a fairly broad set of assumed input cases.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 16 Nov 2023
imagedir = uigetdir('Select an image directory');
if ~ischar(imagedir); return; end %user cancelled
known_extensions = [imformats().ext];
dinfo = dir(imagedir);
dinfo([dinfo.isdir]) = [];
[~, ~, fileext] = fileparts({dinfo.name});
dinfo = dinfo(ismember(lower(filext), lower(known_extensions)));
Now dinfo is a dir struct that has had all folders removed from it, and has been pruned down to only have files whos name ends with one of the image file formats known to imread() on your system. Different operating systems support different image file formats, so the list might not include everything that the user thinks are image files.
The files in dinfo will very likely not contain any dicom files. It is not uncommon for people to think of DICOM files as being "image" files. If you need to support DICOM files, then you cannot do any filtering of the files in the directory by file extension, since DICOM images do not have a standard file extension (".dcm" is common, but not a "standard")

Image Analyst
Image Analyst on 16 Nov 2023
See the FAQ for code samples.
Modify the inside of the loop to call some function, that you write like "AnalyzeSingleImage", that will analyze each image one at a time. In this way you can analyze a whole folder of images. Adapt the code as needed.

Categories

Find more on DICOM Format in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!