how to open image according to the selected image

sir @Walter Roberson and @Image analyst i am using the gui and code for selecting an image is as follows:
global im
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file');
MyImage = strcat(pathname, filename);
%This code checks if the user pressed cancel on the dialog.
if isequal(filename,0) || isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','failed','modal') )
hold on;
else
hold off;
uiwait(msgbox('User selected image sucessfully','sucess','modal'));
end
im=imread(MyImage);
im=im2double(im); %converts to double
%for backup process :)
imshow(im,'Parent',handles.axes2);
as according to the following code i want to open an image present in a folder suppose the user have selected image1 then in the second section:
function pushbutton2_Callback(hObject, eventdata,handles,varargin)
global im
load abc
end
in load abc i want to code that if the user selected suppose image1 then the
figure;
imshow(image4 );
not getting how to do this please guide

5 Comments

Can you clarify this question a little more? I'm going to assume that everything in the top works as desired. Once you get an image can you explain what you actually want?
Is the image you are loading dependent on which image you loaded in the first part or do you just want to load and show a specific image everytime?
And try to avoid using globals. Save the data that is obtained in one callback (your image) to the handles structure using guidata so that it can be available in another callback. For example, if your first push button callback does something to get an image, then save it as
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file');
MyImage = strcat(pathname, filename);
%This code checks if the user pressed cancel on the dialog.
if isequal(filename,0) || isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','failed','modal') )
hold on;
else
hold off;
uiwait(msgbox('User selected image sucessfully','sucess','modal'));
end
im=imread(MyImage);
handles.im=im2double(im); %converts to double
%for backup process :)
imshow(handles.im,'Parent',handles.axes2);
guidata(hObject,handles); % <--- save the updated struct
Now in your second callback you would do
function pushbutton2_Callback(hObject,eventdata,handles)
if isfield(handles,'im')
% do something with handles.im
end
I made a gui without guide and made my handle structure a global variable. Thanks for this tip.
I don't understand the question. So the user selected image1.png from the dialog box and you read it into global variable im. And then you want to display image4. What is image4??? And why did you call load() to load in the "abc.mat" file???
I distinctly remember having answered this same poorly-worded question twice already.

Sign in to comment.

Answers (1)

Your line
MyImage = strcat(pathname, filename);
is incorrect. The path that is returned by uigetfile does not always have a directory separator at the end of it. You need to be add the directory separator character between the two. The easiest way to do that is to use fullfile() instead of strcat()
MyImage = fullfile(pathname, filename);

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 20 Jul 2016

Answered:

on 24 Jul 2016

Community Treasure Hunt

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

Start Hunting!