在3D图像集中绘制一条线

时间:2015-06-02 17:50:14

标签: matlab plot dicom

换句话说,同一条线应出现在每个图像的顶部。我使用imshow3D功能预览我的图片。

1 个答案:

答案 0 :(得分:2)

这是一种没有imshow3D的方法(只需使用下面的函数创建一个新的.m文件)。我想你会更容易理解代码背后的内容。

诀窍是在回调中添加一个按钮和函数imline,以便以交互方式绘制一条线。然后,当用户滚动堆栈时,我们使用函数line来实际使用先前绘制的线的位置绘制一条线。

我使用了Matlab附带的mri数据,但这应该适用于您的数据/ dicom图像/无论如何。

代码:

function ScrollMRI(~)
clc
clear
close all

%// Load demo data
S = load('mri');

%// Get dimensions and number of slices.
ImageHeight = S.siz(1); %// Not used here
ImageWidth = S.siz(2); %// Not used here
NumSlices = S.siz(3);

S.D = squeeze(S.D);

%// Create GUI
hFig = figure('Position',[100 100 400 400],'Units','normalized');

%// create axes with handle
handles.axes1 = axes('Position', [0.2 0.2 0.6 0.6]);

%// create y slider with handle
handles.y_slider = uicontrol('style', 'Slider', 'Min', 1, 'Max', NumSlices, 'Value',1, 'Units','normalized','position', [0.08 0.2 0.08 0.6], 'callback', @(s,e) UpdateY);
handles.SlideryListener = addlistener(handles.y_slider,'Value','PostSet',@(s,e) YListenerCallBack);

%// Create pusbutton to draw line
handles.DrawLineButton= uicontrol('style', 'push','position', [40 40 100 30],'String','Draw line', 'callback', @(s,e) DrawLine);

%// Flag to know whether pushbutton has been pushed
handles.LineDrawn = false;

%// Show 1st slice
imshow(S.D(:,:,1))

guidata(hFig,handles);

%// Listeners callbacks followed by sliders callbacks. Used to display each
%// slice smoothly.
    function YListenerCallBack

        handles = guidata(hFig);

        %// Get current slice
        CurrentSlice = round(get(handles.y_slider,'value'));

        hold on
        imshow(S.D(:,:,CurrentSlice));

        %// If button was button, draw line
        if handles.LineDrawn
            line([handles.LinePos(1,1) handles.LinePos(2,1)],[handles.LinePos(1,2) handles.LinePos(2,2)],'LineWidth',2,'Color','y');
        end
        drawnow

        guidata(hFig,handles);

    end

    function UpdateY(~)

        handles = guidata(hFig); %// Get handles.
        CurrentSlice = round(get(handles.y_slider,'value'));

        hold on
        imshow(S.D(:,:,CurrentSlice));

        if handles.LineDrawn
            line([handles.LinePos(1,1) handles.LinePos(2,1)],[handles.LinePos(1,2) handles.LinePos(2,2)],'LineWidth',2,'Color','y');
        end
        drawnow

        guidata(hFig,handles);

    end

%// Pushbutton callback to draw line.
    function DrawLine(~)
        handles = guidata(hFig); %// Get handles.

        hLine = imline(gca);

        %// Get position of line and store it in handles structure.
        handles.LinePos = hLine.getPosition(); %// handles.LinePos is a 2-by-2 array [X1 Y1; X2 Y2]

        %// Set tag to true.
        handles.LineDrawn = true;
        guidata(hFig,handles);
    end

end

以下是按下按钮之前的GUI屏幕截图:

enter image description here

然后按下它并画一条线:

enter image description here

当你滚动堆栈时,该行保持在相同的位置。

希望有助于您入门!