Displaying images with GUI in matlab

I have a code working for displaying dicom images..
i want to display these in GUI, with help of pushbutton
please help me..
thanks in advance!!
the code is
files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));
for i = 1 : numel(files)
[X, map] = dicomread(fullfile('C:','Program Files','MATLAB','images',files(i).name));
if ~isempty(map)
subplot(2,2,i);
subimage(X,map);
else
subplot(2,2,i);
imshow(X, map);
end
end

2 Comments

Please format your code as explained in the "Markup help" link on this page. What is your question?
The code is as follows....
files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));
for i = 1 : numel(files)
[X, map] = dicomread(fullfile('C:','ProgramFiles','MATLAB','images',files(i).name));
if ~isempty(map)
subplot(2,2,i);
subimage(X,map);
else
subplot(2,2,i);
imshow(X, map);
end
end

Sign in to comment.

 Accepted Answer

Hi,
You asked me 'what about for huge number of images in a folder??'
I think of course we cannot create so many axes in this figure,
so I decided to display them in single axes.
Just use button 'back' and 'next' to display your next or previous image.
Here the code :
function varargout = untitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else gui_mainfcn(gui_State, varargin{:}); end
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));
for i = 1 : length(handles.files)
handles.X{i} = dicomread(fullfile('C:','Program Files','MATLAB','images',handles.files(i).name));
end
imshow(handles.X{1},[]);
handles.index = 1;
Cek(hObject, eventdata, handles);
guidata(hObject, handles);
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
handles.output = hObject;
handles.index = handles.index - 1;
Cek(hObject, eventdata, handles);
imshow(handles.X{handles.index},[]);
guidata(hObject, handles);
function pushbutton2_Callback(hObject, eventdata, handles)
handles.output = hObject;
handles.index = handles.index + 1;
Cek(hObject, eventdata, handles);
imshow(handles.X{handles.index},[]);
guidata(hObject, handles);
function Cek(hObject, eventdata, handles)
handles.output = hObject;
n = length(handles.files);
if handles.index > 1, set(handles.pushbutton1,'enable','on');
else set(handles.pushbutton1,'enable','off'); end
if handles.index < n, set(handles.pushbutton2,'enable','on');
else set(handles.pushbutton2,'enable','off'); end
guidata(hObject, handles);
Please design the correct GUI as shown in the picture.
the tag for axes is axes1, tag for 'back' button is pushbutton1, tag for 'next' button is pushbutton2.
If you design it correctly, I believe no error will occurs.

27 Comments

Thank you Mr.Chandra..
have you gone through the previous errors?
in first program..which you have given for displaying 4 images..
i used panel in that . it wont be the problem right??
The previous error sounds
??? Reference to non-existent field 'axes3'
It seem there is no axes with 'tag' axes3.
Please check it carefully.
Maybe you have renamed it?
hi..
i tried for above 'BACK' and 'NEXT' button program
you have written for pushbutton1, pushbutton2.
i didnt understand what is "Cek"??
you had to write call back function for Axes1 right??
is "Cek" is AXES1??
function varargout = Back_button(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Back_button_OpeningFcn, ...
'gui_OutputFcn', @Back_button_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function Back_button_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));
for i = 1 : length(handles.files)
handles.X{i} = dicomread(fullfile('C:','Program Files','MATLAB','images',handles.files(i).name));
end
imshow(handles.X{1},[]);
handles.index = 1;
Cek(hObject, eventdata, handles);
guidata(hObject, handles);
function varargout = Back_button_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function axes1_CreateFcn(hObject, eventdata, handles)
handles.output = hObject;
n = length(handles.files);
if handles.index > 1, set(handles.pushbutton1,'enable','on');
else set(handles.pushbutton1,'enable','off'); end
if handles.index < n, set(handles.pushbutton2,'enable','on');
else set(handles.pushbutton2,'enable','off'); end
guidata(hObject, handles);
function pushbutton1_Callback(hObject, eventdata, handles)
handles.output = hObject;
handles.index = handles.index - 1;
Cek(hObject, eventdata, handles);
imshow(handles.X{handles.index},[]);
guidata(hObject, handles);
function pushbutton2_Callback(hObject, eventdata, handles)
handles.output = hObject;
handles.index = handles.index + 1;
Cek(hObject, eventdata, handles);
imshow(handles.X{handles.index},[]);
guidata(hObject, handles);
Hi,
No, it is not axes callback.
I created it to check the condition before you press the button next or back.
If the index is < 2 then it will set back button disabled.
If the index is > number of your files, then it will set next button disabled.
Is it clear?
ok..
and there is no any call back function for AXES! right??
Yes, exactly :)
function Cek(hObject, eventdata, handles)
we have to write this above function??
why i am asking means ..when you put a pushbutton
function pushbutton1_Callback(hObject, eventdata, handles) like this it will comes in callback function no..
so i asked you whether it is inbuilt in callback or need to write ????
You can place it anywhere just like pushbutton1_callback in this code.
But, It's better if you place it in the bottom of the code.
Usually, everyone do this.
So , you should write this.
Its working Mr.Chandra..:) :)
now i am getting how to write call back function:)
will try for other design which i have told you very long back..
this is only my final Task.
Glad to help you :)
Thank you:)
can we display all images from our database ( folder) at a time??
i just changed the path for displaying images..
its giving error..
??? Reference to non-existent field 'X'.
Error in ==> Back_button>Back_button_OpeningFcn at 64
imshow(handles.X{1},[]);
Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in ==> Back_button at 42
gui_mainfcn(gui_State, varargin{:});
WHAT DOES IT MEAN?????
Wow, I think this is impossible.
If we have 100 images in a folder then we must create 100 axes to display all of them in a time :(
look at line : handles.files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));
Have you change it correctly?
Ya i have changed..
handles.files = dir(fullfile('C:','Program
Files','MATLAB','jpeg_images\*.JPEG'));
handles.X{i} = imread(fullfile('C:','Program Files','MATLAB','jpeg_images',handles.files(i).name));
These are JPEG images so i am using imread() function
but it is giving error..the error is-->
??? Reference to non-existent field 'X'.
I have few suggestions for you :
1] please check this line by removing semicolon (;) at line
handles.files = dir(fullfile('C:','Program Files','MATLAB','jpeg_images\*.JPEG'));
If it returns 0x1 struct, means the files not found or maybe you are incorrect in write the folder address.
2] Try to use *.jpg instead of *.JPEG.
Please check the extention your jpeg files in the folder.
3] If the error message still appears, try to replace
handles.files = dir(fullfile('C:','Program
Files','MATLAB','jpeg_images\*.JPEG'));
with
handles.files = dir(strcat('C\Program
Files\MATLAB\jpeg_images\*.JPEG'));
after removing the ; it gives 0x1 struct.
Okay, now try to replace
handles.files = dir(fullfile('C:','Program Files','MATLAB','jpeg_images\*.JPEG'));
with
handles.files = dir(fullfile('C:','Program Files','MATLAB','jpeg_images\*.jpg'));
Please check it. Is this works?
wow!!!!!!!!!!!!!! really you are great!!!!!!
its working now:)
how much you have experience in this field???? your mail id?
It learning Matlab and image processing since 2009.
And my email : nasa078@gmail.com
I'm realy glad to help you :)
naver mind..
thanks!!
hey i am just fresher joined this company as a trainer. i am a new to this matlab.. but getting things quickly with your help thanks!!!!!!!!
its working.
wil try for displaying all images. and zooming .
k Thank you:)
Hi.... we can display 'n' number of images .. no need to take AXES.
we can take additional 'figure' means frame in GUI and give the path name to display on that....
Think on this..and let me know
Hi,
I have a problem similar to REHANA M.B, so I tried your Code...like REHANA the only thing I changed is that I used imread instead of dicomread, because I wanted to display jpg-images.
As a matter of fact, when starting the GUI no image was displayed and when trying the next Button or the previous Button I got the message :
"??? Reference to non-existent field 'index'.
Error in ==> disp_image>next_Callback at 36
handles.index = handles.index + 1;
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> disp_image at 11
else gui_mainfcn(gui_State, varargin{:}); end
Error in ==>
@(hObject,eventdata)disp_image('next_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback"
do you maybe have any suggestions what mistake i might have made? in case it matters, i used Matlab 7.10.0...thank you in advance :)

Sign in to comment.

More Answers (1)

Hi,
If you want to display it on the GUI, first you have to design the GUI.
Make a GUI with four axes, as shown in the image below :
Then add a pushbutton and write the callback
function pushbutton1_Callback(hObject, eventdata, handles)
handles.output = hObject;
files = dir(fullfile('C:','Program Files','MATLAB','images\*.dcm'));
hlist = {handles.axes1, handles.axes2, handles.axes3, handles.axes4};
for i = 1 : 4
X = dicomread(fullfile('C:','Program Files','MATLAB','images',files(i).name));
axes(hlist{i});
imshow(X, []);
end
guidata(hObject, handles);

2 Comments

Thank you..
but for every image we have to use axes? then what about for huge number of images in a folder??
can we display n number of images on a single axes??
it is giving the error
??? Reference to non-existent field 'axes3'.
Error in ==> gui_new>pushbutton1_Callback at 83
hlist = {handles.axes1, handles.axes2, handles.axes3, handles.axes4};
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> gui_new at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)gui_new('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!