0
換句話說,每條圖像上應該出現同一條線。我使用imshow3D
函數預覽我的圖像。在3D圖像集中畫一條線
換句話說,每條圖像上應該出現同一條線。我使用imshow3D
函數預覽我的圖像。在3D圖像集中畫一條線
這裏有一個方法可以不用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的屏幕截圖按下按鈕之前:
,然後按下它和畫線之後:
T當你在堆棧中滾動時,他的線保持在相同的位置。
希望能幫助你開始!
你試過我的建議嗎? –
抱歉或超晚回覆。我誤解了我的問題。我希望能夠在代碼中選擇兩個點,並且從一個到另一個都有一條線,並且在每個圖像上都有。我能夠做到這一點,通過每個圖像,但行在每個圖像上顯示爲一個點 –