【问题标题】:display a screen/frame to display dicom image in matlab在matlab中显示一个屏幕/框架以显示dicom图像
【发布时间】:2015-11-18 01:35:49
【问题描述】:

我有一个 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

【问题讨论】:

    标签: matlab dicom


    【解决方案1】:

    简而言之:

    • 您可以通过在 GUI 中要显示图像的位置(以及大小)添加 axes 来解决您的问题
    • 您应该在callback 中添加一些检查,以便捕获以下情况:
      • 用户选择文件夹时选择Cancel
      • 所选文件夹不包含任何.dcm 文件
    • pushbutton2 和 pushbutton3 在打开 GUI 时应该是 disabled,如果用户选择了正确的文件夹,则在 pushbutton1 回调中启用。您可以使用GUIDE 或在GUI 中设置enable 属性off 禁用它们CreateFcn
    • 你也可以避免使用global varaibles;您实际上可以使用guidata 内置函数在callback 之间存储和共享变量。

    添加坐标区

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

    • 您可以使用set 函数设置黑色背景色
    • 您也可以使用 set 函数删除 X 轴和 Y 轴 ticks

    你可以在你的代码中插入这个callback

    % --- 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')
    

    当您的pushbutto callback 中调用imshow 函数时,DICOM 图像将显示在正确的位置。

    添加对文件夹选择的检查

    你可以简单测试uigetdir返回的值:当用户推送Cancel或时设置为0

    使用 GUI 数据而不是全局变量

    您可以存储要在callback 之间共享的变量,并将它们添加到 GUI 数据结构中。

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

    % Add "indexSelected" to handles struct and initialize it
    my_gui_data=guidata(gcf)
    my_gui_data.indexSelected=0;
    guidata(gcf,my_gui_data);
    

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

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

    当您需要检索和/或更新callback 中的变量时,您只需使用相同的方法。例如,将dname 变量存储在pushbutton1_Callback

    my_gui_data=guidata(gcf)
    my_gui_data.dname=dname;
    guidata(gcf,my_gui_data);
    

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

    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',[])
    

    刚刚打开的 GUI

    带有 DCOM 图像的 GUI

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多