matlab - Draw a line in a 3D set of images -
in other words same line should appear atop each image. im using imshow3d
function preview images.
here way without imshow3d
(just create new .m file function below). think easier understand going on behind code then.
the trick add pushbutton , function imline in callback in order interactively draw line. then, user scrolls through stack use function line
draw line using position of drawn line.
i used mri
data ships matlab should work data/dicom images/whatever.
code:
function scrollmri(~) clc clear close %// load demo data s = load('mri'); %// dimensions , 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 handle handles.axes1 = axes('position', [0.2 0.2 0.6 0.6]); %// create y slider 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 draw line handles.drawlinebutton= uicontrol('style', 'push','position', [40 40 100 30],'string','draw line', 'callback', @(s,e) drawline); %// flag know whether pushbutton has been pushed handles.linedrawn = false; %// show 1st slice imshow(s.d(:,:,1)) guidata(hfig,handles); %// listeners callbacks followed sliders callbacks. used display each %// slice smoothly. function ylistenercallback handles = guidata(hfig); %// current slice currentslice = round(get(handles.y_slider,'value')); hold on imshow(s.d(:,:,currentslice)); %// if button 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); %// 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 draw line. function drawline(~) handles = guidata(hfig); %// handles. hline = imline(gca); %// position of line , store in handles structure. handles.linepos = hline.getposition(); %// handles.linepos 2-by-2 array [x1 y1; x2 y2] %// set tag true. handles.linedrawn = true; guidata(hfig,handles); end end
here screenshot of gui before pressing button:
and after pressing , drawing line:
the line stays @ same position scroll through stack.
hope helps started!
Comments
Post a Comment