显示一个屏幕/框架以在matlab中显示dicom图像

时间:2015-11-18 01:35:50

标签: matlab dicom

我有一个matlab程序来上传一个dicom图像文件夹。我想显示一个黑色的屏幕/框架,在图像将被加载。现在,图像显示在浏览按钮上。

有办法吗?

这是我的代码:

function varargout = ui(varargin)
% UI MATLAB code for ui.fig
%      UI, by itself, creates a new UI or raises the existing
%      singleton*.
%
%      H = UI returns the handle to a new UI or the handle to
%      the existing singleton*.
%
%      UI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UI.M with the given input arguments.
%
%      UI('Property','Value',...) creates a new UI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ui

% Last Modified by GUIDE v2.5 17-Nov-2015 13:11:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ui_OpeningFcn, ...
                   'gui_OutputFcn',  @ui_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
% End initialization code - DO NOT EDIT
end
% --- Executes just before ui is made visible.
function ui_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 ui (see VARARGIN)

% Choose default command line output for ui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end

% --- Outputs from this function are returned to the command line.
function varargout = ui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = handles.output;
end

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global files;
global dname;
dname = uigetdir('Select the dicom image folder');
set(handles.text2, 'String', dname);
files = dir(fullfile(dname, '*.dcm'));
dname = [dname '\'];
global indexSelected;
indexSelected = 1;
filePath = [dname files(1).name];
fileRead = dicomread(filePath);
imshow(fileRead, []);
end

function text2_Callback(hObject, eventdata, handles)
% hObject    handle to text2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of text2 as text
%        str2double(get(hObject,'String')) returns contents of text2 as a double


% --- Executes during object creation, after setting all properties.
end

function text2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global indexSelected;

global files;
global dname;
if(indexSelected == 1)
    indexSelected = length(files);
    filePath = [dname files(indexSelected).name];
    fileRead = dicomread(filePath);
    imshow(fileRead, []);
else
    indexSelected = indexSelected - 1; 
    filePath = [dname files(indexSelected).name];
    fileRead = dicomread(filePath);
    imshow(fileRead, []);
end
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global indexSelected;

global files;
global dname;
if(indexSelected == length(files))
    indexSelected = 1 ;
    filePath = [dname files(1).name];
    fileRead = dicomread(filePath);
    imshow(fileRead,[]);
else
    indexSelected = indexSelected + 1;
    filePath = [dname files(indexSelected).name];
    fileRead = dicomread(filePath);
    imshow(fileRead,[]);
end
end

1 个答案:

答案 0 :(得分:0)

简而言之:

  • 您可以通过在GUI中添加axes来确定您想要显示图像的位置(和大小)
  • 您应该添加callback部分支票,以便了解以下情况:
    • 用户在选择文件夹时选择Cancel
    • 所选文件夹不包含任何.dcm个文件
  • 打开GUI时,
  • pushbutton2pushbutton3应为disabled,如果用户选择了正确的文件夹,则在pushbutton1回调中启用
  • GUIDEenable。您可以使用off或在GUI CreateFcn
  • 中设置global varaibles属性callback来停用它们
  • 您也可以避免使用guidata;您可以使用axes内置函数在tag之间存储和共享变量。

添加轴

添加CreateFcn(例如,set axes1)后,您可以在ticks中设置其外观:

  • 您可以使用set功能
  • 设置黑色背景颜色
  • 您也可以使用callback功能移除X轴和Y轴% --- 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 % Hint: place code in OpeningFcn to populate axes1 % Set axes background color to black set(hObject,'color','k') % Remove X-axis and Y-axis ticks set(gca,'xtick',[],'ytick',[]) % Disable pushbutton2 and pusbutton 3 (they will be enabled in pushbutton1 % callback) set(handles.pushbutton2,'enable','off') set(handles.pushbutton3,'enable','off')

您可以在代码中插入此imshow

pushbutto callback

DICOM中调用uigetdir功能后,0图片将显示在正确的位置。

添加对文件夹选择的检查

您只需测试Cancel返回的值:当用户推送callback

时,它会设置为OpeningFcn

使用GUI数据代替flobal变量

您可以在% Add "indexSelected" to handles struct and initialize it my_gui_data=guidata(gcf) my_gui_data.indexSelected=0; guidata(gcf,my_gui_data); 中将要共享的变量存储到GUI数据结构中。

在GUI guidata中,您可以通过添加以下代码来初始化(如果需要)您要共享的变量:

indexSelected

函数guidata在开头用于从GUI获取GUI数据(一开始它只包含GUI句柄)。

然后,您可以将callback添加到GUI数据结构中,并再次调用dname来设置更新的GUI数据结构。

当您需要检索和/或更新pushbutton1_Callback中的变量时,您只需使用相同的方法。例如,要将my_gui_data=guidata(gcf) my_gui_data.dname=dname; guidata(gcf,my_gui_data); 变量存储在function varargout = ui(varargin) % UI MATLAB code for ui.fig % UI, by itself, creates a new UI or raises the existing % singleton*. % % H = UI returns the handle to a new UI or the handle to % the existing singleton*. % % UI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in UI.M with the given input arguments. % % UI('Property','Value',...) creates a new UI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before ui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to ui_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help ui % Last Modified by GUIDE v2.5 21-Nov-2015 17:35:56 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @ui_OpeningFcn, ... 'gui_OutputFcn', @ui_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 % End initialization code - DO NOT EDIT % --- Executes just before ui is made visible. function ui_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 ui (see VARARGIN) % Choose default command line output for ui handles.output = hObject; % Update handles structure guidata(hObject, handles); % Add "indexSelected" to handles struct and initialize it my_gui_data=guidata(gcf) my_gui_data.indexSelected=0; guidata(gcf,my_gui_data); % Disable pushbutton2 and pusbutton 3 (they will be enabled in pushbutton1 % callback) set(handles.pushbutton2,'enable','off') set(handles.pushbutton3,'enable','off') % UIWAIT makes ui wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = ui_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get GUI data my_gui_data=guidata(gcf) % Use guidata data struct instead of "global" variables % global files; % global dname; dname = uigetdir('Select the dicom image folder'); % Check for directory selection validity if(dname == 0) set(handles.text2,'string','Dir selection aborted') else set(handles.text2, 'String', dname); files = dir(fullfile(dname, '*.dcm')); % Check for files presence if(length(files) == 0) set(handles.text2,'string',['No ".dcm" file in ' dname ' folder']) else % Enable pushbutton2 and pusbutton 3 set(handles.pushbutton2,'enable','on') set(handles.pushbutton3,'enable','on') % Use guidata data struct instead of "global" variables % global indexSelected; % Not needed, use fullfile to build the full file name % dname = [dname '\']; indexSelected = 1; % filePath = [dname files(1).name]; filePath = fullfile(dname,files(1).name); fileRead = dicomread(filePath); imshow(fileRead, []); % Store GUI data my_gui_data.dname=dname; my_gui_data.files=files; my_gui_data.indexSelected=indexSelected; my_gui_data.filePath=filePath; guidata(gcf,my_gui_data); end end % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Use guidata data struct instead of "global" variables % global indexSelected; % % global files; % global dname; my_gui_data=guidata(gcf); indexSelected=my_gui_data.indexSelected; files=my_gui_data.files; dname=my_gui_data.dname; if(indexSelected == 1) indexSelected = length(files); % Use "fullfile" to build the file name % filePath = [dname files(indexSelected).name]; filePath = fullfile(dname,files(indexSelected).name); fileRead = dicomread(filePath); imshow(fileRead, []); else indexSelected = indexSelected - 1; % Use "fullfile" to build the file name % filePath = [dname files(indexSelected).name]; filePath = fullfile(dname,files(indexSelected).name); fileRead = dicomread(filePath); imshow(fileRead, []); end % Store GUI data my_gui_data.filePath=filePath; my_gui_data.indexSelected=indexSelected; guidata(gcf,my_gui_data); % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Use guidata data struct instead of "global" variables % global indexSelected; % % global files; % global dname; my_gui_data=guidata(gcf); indexSelected=my_gui_data.indexSelected; files=my_gui_data.files; dname=my_gui_data.dname; if(indexSelected == length(files)) indexSelected = 1 ; % Use "fullfile" to build the file name % filePath = [dname files(1).name]; filePath = fullfile(dname,files(1).name); fileRead = dicomread(filePath); imshow(fileRead,[]); else indexSelected = indexSelected + 1; % Use "fullfile" to build the file name % filePath = [dname files(indexSelected).name]; filePath = fullfile(dname,files(indexSelected).name); fileRead = dicomread(filePath); imshow(fileRead,[]); end % Store GUI data my_gui_data.filePath=filePath; my_gui_data.indexSelected=indexSelected; guidata(gcf,my_gui_data); % --- 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 % Hint: place code in OpeningFcn to populate axes1 % Set axes background color to black set(hObject,'color','k') % Remove X-axis and Y-axis ticks set(gca,'xtick',[],'ytick',[])

{{1}}

我已经创建了一个GUI(dcom_gui)来测试上述建议:

{{1}}

GUI刚刚开启

enter image description here

具有DCOM图像的GUI

enter image description here

希望这有帮助。