I need to make a matlab gui that reads and displays a directory of Dicom files. The gui needs to have a file menu. 2. In the File menu, there is a file Open function which can read the directory of DICOM files. I have no idea how to do this. Can someone help me with this?
Here is some code to get you going. You should absolutely follow this link and try the code provided for yourself. I think this will greatly help you for the remainder of your project and help you understand what is going on as well.
That being said, the following creates a simple figure with an axes to display images. There is also a menu with a button used to open files, in this case DICOM files (.dcm). The hardest part is taken care of my Matlab; you only need to call a function (uigetfile) in the callback of that "open" button and then call the function dicomread to read the content of a dicom file.
I'll leave the rest to you but this should help you get started.If something is unclear please don't hesitate to ask.
Code:
function DicomReadGUI
%// Create figure
hFigure = figure('Position',[200 200 600 600],'MenuBar','none', ...
'Toolbar','none','HandleVisibility','callback');
%// Add an axes just to display an image.
hAxes = axes('Position',[.1 .1 .8 .8],'Parent',hFigure);
%// Add menu in which you will add the "open" button
hFileMenu = uimenu('Parent',hFigure,'HandleVisibility','callback','Label','File');
%// Add a button to browse and open files
hOpenMenuitem = uimenu('Parent',hFileMenu,...
'Label','Open','HandleVisibility','callback', ...
'Callback', #hOpenMenuitemCallback);
%// Callback of the "open" button
function hOpenMenuitemCallback(hObject,eventdata)
%// Browse the computer and select .dcm files.
FileToRead = uigetfile('*.dcm')
[YourImage, ColorMap] = dicomread(FileToRead);
%// Display image in Axes1
imshow(YourImage,'Parent',hAxes)
end
end
And screenshot of the GUI with the button used to unroll a menu from which you can select files to open (circled in red):
Related
How to add and delete specific image in folder(desktop) through matlab gui?
For example, I have a folder name FLOWER. Then when I open the gui, I can add new image and delete image that have in folder FLOWER. To design the gui i have no problem, but I'm stuck at the code to add and delete image.
Link for screenshot folder:
[https://i.stack.imgur.com/TGDkf.png][1]
To create something, you could call the imwrite function(If I understand you correctly), and delete function could let you delete it.
Here are some demos:
%%Add and Delete image in folder
clc; clear;
%%Case1 : Save a figure
%Create
plot(1:10,1:10);
A=getframe(gcf);
%Write
imwrite(A.cdata,'D:/FLOWER/img1.jpg');
%%Case2 : Create a image
%Create
B=zeros(100:100);
B(1:50,:)=1;
%Write
imwrite(mat2gray(B),'D:/FLOWER/img2.jpg');
%%Case3 : Load and save a image
%Load
C=imread('D:/FLOWER/flower.png');
%Do something with image
%imshow(C);
%C=C/2;
%Write
imwrite(C,'D:/FLOWER/img3.jpg');
%pause
disp('You could check your image now')
pause()
%delete image
delete('D:/FLOWER/img1.jpg')
delete('D:/FLOWER/img2.jpg')
delete('D:/FLOWER/img3.jpg')
To run this demo, "flower.png" have to exist in the folder D:/FLOWER/ and then you will find a copy of it with the name of "img3.jpg"
You could also try copyfile('source','destination') in case3.(if you needn't show/change the image in the GUI)
P.S. you may change the absolute path of your folder since I changed my desktop's path.
I'm working on creating a GUI in matlab using GUIDE. However, i'm not exactly sure how to do the following, and was looking for some tips/advice.
Problem
I want to open a directory and display all the images in that directory in the GUI interface when if it's selected. However, since I will never know exactly how many images there are I am not entirely sure how to do this in the GUI.
Essentially, I want to open the directory and all the images to be displayed in a grid on the GUI similar to that in iphoto.
Current code
Currently, I can open a directory fine, and get all the required information as follows:
directory = uigetdir(pwd, 'Directory Selector');
files = dir(fullfile(directory, '*.jpg'));
strcat(strcat(directory, '/') , files.name) %outputs each file's location
I'm just not sure how to translate this information into the GUI without writing numerous handles.axes1. I understand that since I know this info I could loop over them, but would I not have to create the axes to begin with?
You probably don't want to do this with individual controls - the reason is that MATLAB will have to render each and every one, which will be slow if the directory has a lot of images. Clearly, you can only display a certain number of images on screen at once. You would also have to write your own scrolling code (or some kind of pagination control).
If you have MATLAB > R2008, you can put images in uitable cells using HTML:
% Example for a control with a 'String' property
set(handles.myControl, 'String', '<html><b>Logo</b>: <img src="http://UndocumentedMatlab.com/images/logo_68x60.png"/></html>');
See also this post and this Undocumented MATLAB page.
A different option would be to use the Windows common controls ListView.
A simpler way of doing this would be to have a single image and a listbox of files; an example is here
You can add components to a GUI pprogrammatically. There's more information here.
Each new axes can be added with something like this:
ah = axes('Parent',hObject,'Position',[left bottom width height]);
where left, bottom, width and height define the size and position of the axes. You'll need to change the position for each axes you create and keep track of the axes handles.
I want to implement the following code using matlab GUIDE:
axes(handles.axes1);
imshow(image1);
axes(handles.axes2);
imshow(image2);
As I am very new to GUIDE so I am having no idea how to do this. Any guidance.
Open guide selecting empty gui
Drag and drop two axes objects anywhere in the figure.
Right clicking on any axes object and seeing it properties allows to set size, position, and name of the object.
Guide always generate figure file in parallel with .m file containing all callback functions, among them function to be called on opening the gui (just before it is visible) -you can put you imshow() code there...
The whole thing is very intuitive, and has a great help provided by matlab.
I have an image and after drawing some features(ellipses and text) on it I want to save it as JPEG.
h= figure(1);
imagesc(im_name);
colormap('gray');
hold on
for i=1:no_of_points;
//draw features and write some text
end
hold off
imsave (h);
I am getting a figure with features drawn on it but when I save it, it is an image (which is my orignal image 'im_name') without new features on it.
I tried also
.
.
.
imsave (h);
hold off
Thanx in advance for your help.
Maybe you should try the function saveas
saveas
Save figure or Simulink block diagram using specified format
Alternatives
Use File > Save As on the figure window menu to access the Save As
dialog, in which you can select a graphics format. For details, see
Exporting in a Specific Graphics Format in the MATLAB Graphics
documentation. Sizes of files written to image formats by this GUI and
by saveas can differ due to disparate resolution settings. Syntax
saveas(h,'filename.ext') saveas(h,'filename','format')
Description
saveas(h,'filename.ext') saves the figure or Simulink block diagram
with the handle h to the file filename.ext. The format of the file is
determined by the extension, ext. Allowable values for ext are listed
in this table.
You can pass the handle of any Handle Graphics object to saveas, which
then saves the parent figure to the object you specified should h not
be a figure handle. This means that saveas cannot save a subplot
without also saving all subplots in its parent figure.
When using the saveas function the resolution isn't as good as when manually saving the figure with File-->Save As..., It's more recommended to use hgexport instead, as follows:
hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
This will do exactly as manually saving the figure.
Source.
I have two figs in two different files.
By clicking a button on first fig I want to show the second one... how to do this? is it possible?
If YES than how to exchange with data between two figures?
There are a number of ways to share data among GUIs. In general, you need to somehow make the graphics handle(s) from one GUI available to the other GUI so it can get/set certain object properties. Here's a very simple example that involves one GUI creating another and passing it an object handle:
function gui_one
hFigure = figure('Pos',[200 200 120 70],... %# Make a new figure
'MenuBar','none');
hEdit = uicontrol('Style','edit',... %# Make an editable text box
'Parent',hFigure,...
'Pos',[10 45 100 15]);
hButton = uicontrol('Style','push',... %# Make a push button
'Parent',hFigure,...
'Pos',[10 10 100 25],...
'String','Open new figure',...
'Callback',#open_gui_two);
%#---Nested functions below---
function open_gui_two(hObject,eventData)
gui_two(hEdit); %# Pass handle of editable text box to gui_two
end
end
%#---Subfunctions below---
function gui_two(hEdit)
displayStr = get(hEdit,'String'); %# Get the editable text from gui_one
set(hEdit,'String',''); %# Clear the editable text from gui_one
hFigure = figure('Pos',[400 200 120 70],... %# Make a new figure
'MenuBar','none');
hText = uicontrol('Style','text',... %# Make a static text box
'Parent',hFigure,...
'Pos',[10 27 100 15],...
'String',displayStr);
end
After saving the above code to an m-file, you can create the first GUI by typing gui_one. You will see a small figure window with an editable text box and a button. If you type something in the text box, then hit the button, a second GUI will appear next to it. This second GUI uses the handle to the editable text box that is passed to it from the first GUI to get the text string, display it, and clear the string from the first GUI.
This is just a simple example. For more information on programming GUIs in MATLAB, take a look at the MathWorks online documentation as well as the links in the answers to this SO question.