How to find out the image name of a randomly selected image and save it to workspace by PushButton?

10 vues (au cours des 30 derniers jours)
Hello,
I have created a command that allows me to display different images from a folder to a GUI. There are a total of 4 randomly selected images from 4 different folders.
The command is:
files = dir('/Users/Documents/MATLAB/IR1/**/*.dcm')
num_files = numel(files);
filename = files(randi(num_files)).name;
image = dicomread(filename);
for image_order = randperm(num_files);
k = image_order
filename = files(k).name;
image = dicomread(filename);
end
With the PushButton the user can select the images by his own order.
My question now would be, how does the image name (of the selected image) save itself in the workspace? And how can one set it so that exactly the order appears that one has selected?
Unfortunately I don't know the commands for it, even after a long search I couldn't find an answer.

Réponse acceptée

Guillaume
Guillaume le 6 Août 2019
The code you show doesn't appear to be related to a GUI, so it's unclear how it relates to your question.
how does the image name (of the selected image) save itself in the workspace
Nothing saves itself. You have to explicitly store whatever information you want to keep in a variable.
In the code you show, if you want to keep the index of the random images you're using you have to store the output of randi and randperm rather than discarding them as soon as you don't need them:
files = dir('/Users/Documents/MATLAB/IR1/**/*.dcm')
num_files = numel(files);
chosen_singlefile = randi(num_files); %keep the chosen index around
filename = files(chosen_singlefile).name;
image = dicomread(filename);
chosen_permutation = randperm(num_files); %keep the chosen ordering around
for image_order = chosen_permutation
k = image_order
filename = files(k).name;
image = dicomread(filename);
end
  3 commentaires
Guillaume
Guillaume le 6 Août 2019
The code in your callback:
  • get the content of an image (CData) displayed in an axis (axes1)
  • copy it into another axis (axes5)
I don't see any image name anywhere. If by name, you mean the name of the file that was used to create the image in axes1, then again, you will have to preserve that yourself. If you're not already using it for something else, you could store that name in the UserData property of the Image. e.g. in the code that originally display the image in axes1:
%no idea what your code is for getting the image
filename = something;
img = imread(filename); %by the way don't use image as variable name. It's a matlab function
axes(handles.axes1);
img = mat2gray(im2double(img);
himg = imshow(img);
himg.UserData = filename;
Then in your callback:
function PickOne_Callback(~, ~, handles)
imh = findobj(handles.axes1, 'type', 'image');
image1 = get(imh, 'CData');
origname = imh.UserData; %get filename back
%shown selected image on another axes
imshow(image1, 'Parent', handles.axes5)
%save to workspace
assignin('base','IR1', origname)
Or you could just store the image name in the handle structure.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by