pass filename to gui to show image - matlab

I have a gui which already has axes on it...i want to pass a filename to that gui and display it on the axes...here's how i tried it..this is written in an xyz.m file:
close current_gui;
result_image(im2fn);
here, current gui is a gui i was working with and result_image is the gui where i want to show the image, im2fn is a variable having the filename of the image...
In result_gui i wrote the following code..
function result_image_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
axes(handles.axes1);
imshow(im2fn);
The gui is displayed but there is no image shown and also i get an error as Undefined function or variable 'im2fn'....Please Help!!

You are receiving this error because im2fn is not part of the local workspace inside OpeningFcn - you have made an incorrect assumption about the way varargin works. When you use varargin, it will be a cell array of the values you passed. See the link for more examples, but in your case you just want to do something like this:
% Make sure the user passed us something to display
assert(~isempty(varargin), [mfilename ':NoImageToDisplay'], 'You must pass an image to the ''result_image'' GUI');
% Extract data from varargs
im2fn = varargin{1};
% Show it!
imshow(im2fn);

Related

MATLAB GUI handles

I have question about using "handles" in Matlab Callback function.
I don t know how to use the same thing twice. Please help me.
So,I build Matlab GUI and I have callback function for upload image:
function pushbutton2_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
[filename pathname]=uigetfile({'*.jpg';'*.bmp'},'File Selector');
image=strcat(pathname, filename)
handles.data1=imread(image)
axes(handles.axes1);
imshow(handles.data1);
set(handles.edit1,'string',filename)
set(handles.edit2,'string',pathname)
guidata(hObject, handles);
,and I have callback fuction for converting the same image to "Gray Scale":
function Gray_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
axes(handles.axes2);
img=handles.data1;
x=imread(img);
y=rgb2gray(x); %function to convert an rgb image to gray scale
imshow (y)
guidata(hObject, handles);
,but it doesn't work.
Does anyone know what I'm doing wrong?
Your first function says
handles.data1=imread(image)
Then your second function says
img=handles.data1;
x=imread(img);
Since img contains image data, not the name of a file, what does imread(img) mean?
I presume you want to work directly with the image data img here, not use imread at all.

GUI in matlab, loop of images in axes

So I'm building a classifier of images. In the GUI a image loads and insert a value on a text box, and push a button. I'm having a problem loading the image in the axes. Because when the axes function is called the handles is zero(due to:% handles empty - handles not created until after all CreateFcns called). And my problem is, how do I get to just call one image at a time for the axes.
The ideal solution, is I create a handles.images=imagedatastore, and every time I push the button I add to a counter(which I already have made) and then that give the indices to get the image from the datastore. My problem with this is that I can't get the first picture, because in the beginning the handles are empty. I have made the callfunction for the axes:
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
[pict_croped,Nphotos,Date_img] =getcropedimagages;
handles.img =pict_croped;
i=readimage(handles.img,1);
% axes(hObject)
imshow(i)
% Hint: place code in OpeningFcn to populate axes1
but this has two problems, first, I don't really want to call the function that creates the datastore all the time I push the button, second, I still can't get the indice of the counter to be in the function,if I have:
i=readimage(handles.img,handles.counter)
it will give me the error in the first time, of not having handles.counter
Any idea how to solve this?This is the first GUI I'm building.
The issue is very clearly in the comment that GUIDE provides for you. The handles struct isn't populated until all CreateFcn have been run so you'll want to use the OpeningFcn to do any initialization of the graphics objects. You can then add any data you need to the handles struct and save it using guidata so that it's available from within all of your other callback functions.
function OpeningFcn(hObject, eventData, handles)
[pict_croped,Nphotos,Date_img] = getcropedimagages;
handles.img = pict_croped;
i = readimage(handles.img,1);
imshow(i, 'Parent', handles.haxes1)
% "Save" the changes to the handles object
guidata(hObject, handles)
Well, I end up with:
in the opening fucntion:
i = readimage(handles.img,handles.counter);
imshow(, 'Parent', handles.axes1)
and in the button call back:
i = readimage(handles.img,handles.counter);
imshow(i, 'Parent', handles.axes1)
inthe end is a very simple solution, I think I was just mind blocked over the first iteration...

Using addlister in MATLAB GUI seems to "delete" existing handles

I am quite new to MATLAB GUI programming (using GUIDE sorry) and I have the following issue: The GUI displays on an axis an image sequence stored in a cell array. I have a couple of pushbuttons and a slider to scroll through the sequence. In order to get a 'continuous slider' I use a listener, which kind of works but creates some problems:
1) When I press the slider, a figure is created and the first frame of the sequence is displayed in it, but as I move the slider the sequence is displayed in the axis of my GUI (which is what I want) and the figure becomes empty. Can anybody tell me why this figure is created and how can I avoid it?
2) Once I press the slider button and thus use the listener, all handles inside the GUI are not functionnal as Matlab does not recognize them and I'm stuck with a functionnal slider/display but I can't use the pushbuttons.
Any ideas on why this happens? Here is the code I use in the Create Function of the slider:
function slider2_Frame_Video_Callback(hObject, eventdata, handles)
hListener = addlistener(hObject,'ContinuousValueChange',#(a,b) slider2_Frame_Video_Callback(hObject, eventdata, handles)); % a and b are dummy arguments
guidata(hObject,handles)
In the slider callback, the code looks like this (basically imshow in the current axis):
axes(hAxis)
imshow(Movie{frame},'parent',hAxis);
drawnow
% This does not work either as handles.edit_FrameNumber is not recognized by Matlab
set(handles.edit_FrameNumber, 'String', frame);
guidata(hObject,handles);
Any hints are welcome thanks!
I wonder if part of the problem is that a listener is being instantiated each time the user moves the slider since the listener code is within this callback AND that the callback being provided to the listener (seems like some kind of strange back-and-forth there). So every time the user releases the mouse button after a slide, a new listener is created. This may be causing some problems with the other buttons not being responsive.
Rather than instantiating the listener there, I would do this in the Opening_Fcn of your GUI:
% --- Executes just before frameSlider is made visible.
function frameSlider_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to frameSlider (see VARARGIN)
% Choose default command line output for frameSlider
handles.output = hObject;
if ~isfield(handles,'hListener')
handles.hListener = ...
addlistener(handles.slider1,'ContinuousValueChange',#respondToContSlideCallback);
end
% Update handles structure
guidata(hObject, handles);
My GUI is named frameSlider; yours will be something else. The above creates one listener with a callback to a function that you will need to define in the same *.m file, respondToContSlideCallback.
A sample body for the callback that is to respond to the continuous slide is
% --- Executes on slider movement.
function respondToContSlideCallback(hObject, eventdata)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% first we need the handles structure which we can get from hObject
handles = guidata(hObject);
% test to display the current value along the slider
disp(['at slider coordinate ' num2str(get(hObject,'Value'))]);
If you run this code, the Command Window will display continuously the slider coordinate as you move the slider from end to end.
Your above code has a Movies cell array. How is that being accessed by your callback? Is it a global variable or ..? Where does hist come from? If Movies is the result of some other function call, then it can be saved to handles too (in whichever location it gets loaded from file). I suppose you will also have to map the slider control coordinates to the number of frames that you have (though maybe you have already done this?).

Display "imshow" images in a GUI using GUIDE

I have a piece of code that is 300 lines long. There is 3 different instances of imshow throughout the code that displays figures as the code is run..
The GUI I am creating will be very simple. Currently I have a push button that initiates the m file.
I am trying to get the images to be displayed within the GUI that I am creating and not in separate Figure windows.
I have being looking at tutorials online but cant get a quick fix for my problem, they all get a bit convoluted and I cant figure out what exactly to do.
I have 3 axis inserted onto the GUI, In the "view callbacks" for each axis I can createfcn, deletefcn and buttonDownFcn. When I createFcn it gives me a hint to "place code in OpeningFcn to populate axes1" in the auto generated code.
I have tried to do this but I am not able to find the correct place to write the code.
Can somebody tell me if I am going in the correct direction or if I have it wrong.
Thanks
In order to display these images, you need to declare the parent in imshow. The parent is what you want to act as the canvas for your image, and in your case will be an axes.
I created a very simple gui with three axes and a push button. MATLAB named my axes axes1, axes2 and axes3. Guide saves the handles to these axes so that you can interact with them throughout your gui code. For example, you mentioned the opening function... here is mine with a call to imshow (the only lines I added were the last three ones):
% --- Executes just before myGUI is made visible.
function myGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for myGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
imshow('myImage1.png', 'Parent', handles.axes1)
imshow('myImage2.png', 'Parent', handles.axes2)
imshow('myImage3.png', 'Parent', handles.axes3)
Notice that I can grab the handles of my axes and then declare them to be the parents for the results of my imshow call.
If you're not sure what the names of your handles are, you can check in the GUI editor by right clicking, looking at the property inspector, and the tag property.
If you want to perform a similar operation for when you click on your pushbutton, right click on the button in the editor, and click on View Callbacks -> Callback, and you can add your imshow code there.
Good luck.
If I understand correctly you just want your images to appear in the axes instead of a figure. Try setting the focus to the axes itself before showing the image.
axes(1);%you may need to change the one to your axes handle.
imagesc(imageToBeDisplayed);
axes(2);
imagesc(secondImage);
axes(3);
imagesc(thirdImage);
This way before you call imagesc you make sure your program knows where to send the image. Otherwise it may just create a figure.

Using axes in Matlab GUI

My interest is to display image in axes, I've 2 GUI's input_window and fig5.
In my former GUI I've used one axes to display image using uigetfile ,
axes(handles.axes1);
imshow(fname);
Now by pressing pushbutton in 1st GUI it switches to fig5 GUI, where I've used so many axes to display multiple images, also i want to display fname image in one axes and it should automatically get displayed in fig5 GUI. For this I've used same above codes in Fig5_OpeningFcn
axes(handles.axes1);
imshow(fname);
I'm getting error like Undefined function or variable 'fname'.
please help me how to pass variables between GUI's
You could use setappdata and getappdata, like so:
% In the first figure:
setappdata(0, 'fname', fname);
% Show the second figure...
% In the second figure:
fname = getappdata(0, 'fname');
% Clear the appdata
setappdata(0, 'fname', '');
% Do stuff with fname