function varargout = airwaySegmenter(varargin) % AIRWAYSEGMENTER MATLAB code for airwaySegmenter.fig % AIRWAYSEGMENTER, by itself, creates a new AIRWAYSEGMENTER or raises the existing % singleton*. % % H = AIRWAYSEGMENTER returns the handle to a new AIRWAYSEGMENTER or the handle to % the existing singleton*. % % AIRWAYSEGMENTER('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in AIRWAYSEGMENTER.M with the given input arguments. % % AIRWAYSEGMENTER('Property','Value',...) creates a new AIRWAYSEGMENTER or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before airwaySegmenter_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to airwaySegmenter_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 airwaySegmenter % Last Modified by GUIDE v2.5 21-Nov-2017 16:25:09 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @airwaySegmenter_OpeningFcn, ... 'gui_OutputFcn', @airwaySegmenter_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 airwaySegmenter is made visible. function airwaySegmenter_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 airwaySegmenter (see VARARGIN) % Choose default command line output for airwaySegmenter handles.output = hObject; handles = instantiate(handles); % Update handles structure guidata(hObject, handles); % UIWAIT makes airwaySegmenter wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = airwaySegmenter_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 generate. function generate_Callback(hObject, eventdata, handles) % hObject handle to generate (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if(isfield(handles, 'generatedObjectHandle')) delete(handles.generatedObjectHandle); end [col, row] = ginput(1); col = round(col); row = round(row); h = waitbar(0, 'Regiongrowing'); handles.bw(:,:,1:3) = 0; handles.bw(:,:,end-3:end) = 0; handles.bw(row, col, handles.cp) objectRegionGrowed = regiongrowing(handles.bw, 0.5, [row col handles.cp]); waitbar(0.5, h, 'Reducing Mesh'); sum(objectRegionGrowed(:)) if(sum(objectRegionGrowed(:)) < 10000000) objectRegionGrowedReduced = objectRegionGrowed(1:1:end, 1:1:end, 1:1:end); waitbar(0.5, h, 'Generating Surface'); handles.objectGenerated = isosurface(objectRegionGrowedReduced(:,:,end:-1:1), 0.5); waitbar(0.8, h, 'Smoothing Surface'); handles.objectGeneratedSmooth = smoothpatch(handles.objectGenerated); axes(handles.secScreen); waitbar(0.9, h, 'Plotting Surface'); handles.generatedObjectHandle = patch(handles.objectGeneratedSmooth, 'edgecolor','none' ,'facecolor', 'b'); else msgbox('Too much attached to the object, please cuttoff more'); end close(h); guidata(hObject,handles); function handles =instantiate(handles) axes(handles.mainScreen) handles.mainImage = imshow([0 0], [0 1]); handles.press = false; axes(handles.secScreen) handles.cp = 1; camlight('left') camlight('right') daspect([1 1 1]) lighting phong; % --- Executes on button press in loadData. function loadData_Callback(hObject, eventdata, handles) % hObject handle to loadData (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) pathName = uigetdir('C:\Users\RB\Desktop\1.2.826.0.1.3680043.2.594.28972.3299.6332.20534.12470'); [handles.volume, handles.info] = loadDicom(pathName, true); handles.bw = handles.volume < 1300; handles = startMainViewer(handles); assignin('base', 'h' , handles); guidata(handles.figure1, handles) function handles = startMainViewer(handles) axes(handles.mainScreen) delete(handles.mainImage); handles.cp = round(size(handles.bw,3) / 2); handles.mainImage = imshow(handles.bw(:,:,handles.cp), [0 1]); set(handles.mainScreen, 'clim', [0 1]); guidata(handles.figure1, handles) function handles = updateScrollScreen(handles, amount) if(handles.cp + amount < 1) handles.cp = 1; elseif(handles.cp + amount > size(handles.bw,3)) handles.cp = size(handles.bw,3); else handles.cp = handles.cp + amount; end set(handles.mainImage, 'cdata', handles.bw(:,:,handles.cp)); % --- Executes on scroll wheel click while the figure is in focus. function figure1_WindowScrollWheelFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata structure with the following fields (see MATLAB.UI.FIGURE) % VerticalScrollCount: signed integer indicating direction and number of clicks % VerticalScrollAmount: number of lines scrolled for each click % handles structure with handles and user data (see GUIDATA) handles = updateScrollScreen(handles, eventdata.VerticalScrollCount); guidata(hObject, handles); % --- Executes on mouse press over figure background. function figure1_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.press = true; guidata(hObject, handles); % --- Executes on mouse motion over figure - except title and menu. function figure1_WindowButtonMotionFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if(handles.press) currentPosition = get (gca, 'CurrentPoint'); b = get(gcf,'selectiontype'); color = 0; if(strcmpi(b,'normal')) color = 0; elseif(strcmpi(b,'alt')) color = 1; else return; end dx = round(currentPosition(1,1)); dy = round(currentPosition(1,2)); if(dx > 0 && dx < size(handles.bw,1) && dy > 0 && dy < size(handles.bw,2) ) radius = 2; % Paint if(get(handles.maxDepthToggle, 'value') == 1) handles.bw(dy - radius: dy + radius,dx - radius : dx + radius, :) = color; else handles.bw(dy - radius: dy + radius,dx - radius : dx + radius,handles.cp) = color; end handles = updateScrollScreen(handles,0); end guidata(hObject, handles); end % --- Executes on mouse press over figure background, over a disabled or % --- inactive control, or over an axes background. function figure1_WindowButtonUpFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.press = false; guidata(hObject, handles); % --- Executes on mouse press over figure background, over a disabled or % --- inactive control, or over an axes background. function figure1_WindowButtonDownFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.press = true; guidata(hObject, handles); % --- Executes on button press in maxDepthToggle. function maxDepthToggle_Callback(hObject, eventdata, handles) % hObject handle to maxDepthToggle (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of maxDepthToggle % --- Executes on button press in removeEthmoid. function removeEthmoid_Callback(hObject, eventdata, handles) % hObject handle to removeEthmoid (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) h = figure, imshow(imrotate(squeeze(handles.volume(:,round(end/2),:)),-90),[-1200 3000]); [col, row] = ginput(3); xVals = 1:1:size(handles.bw,2); res = round(spline(col, row, xVals)); resCompensated = size(handles.bw,3) - res; hold on; plot(xVals, res, 'r'); % Construct a questdlg with three options choice = questdlg('Is this correct?', ... 'Question', ... 'Yes','No','No'); % Handle response switch choice case 'Yes' tic mask = ones(size(handles.bw)); for i = 1:size(handles.bw,2) mask(size(handles.bw,1) - i + 1,:,1:res(i)) = 0; end handles.bw = handles.bw .* mask; toc case 'No' end close(h) guidata(hObject, handles); % --- Executes on button press in export. function export_Callback(hObject, eventdata, handles) % hObject handle to export (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [fileName, filePath] = uiputfile('*.stl'); stlwrite([filePath fileName], handles.objectGenerated.faces, handles.objectGenerated.vertices); fileName = strrep(fileName, '.stl', '_smooth.stl'); stlwrite([filePath fileName], handles.objectGeneratedSmooth.faces, handles.objectGeneratedSmooth.vertices); msgbox('write completed'); % --- Executes on button press in belowVert. function belowVert_Callback(hObject, eventdata, handles) % hObject handle to belowVert (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) h = figure, imshow(imrotate(squeeze(handles.volume(:,round(end/2),:)),-90),[-1200 3000]); [col, row] = ginput(2); xVals = 1:1:size(handles.bw,2); res = round(spline(col, row, xVals)); resCompensated = res; hold on; plot(xVals, res, 'r'); % Construct a questdlg with three options choice = questdlg('Is this correct?', ... 'Question', ... 'Yes','No','No'); % Handle response switch choice case 'Yes' tic mask = ones(size(handles.bw)); for i = 1:size(handles.bw,2) mask(size(handles.bw,1) - i + 1,:,res(i):end) = 0; end handles.bw = handles.bw .* mask; handles.volume = handles.volume .* mask; toc case 'No' end close(h) guidata(hObject, handles); % --- Executes on button press in threshold. function threshold_Callback(hObject, eventdata, handles) % hObject handle to threshold (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) predicted{1} = num2str(0.5*mode(handles.volume(:), 1)) answer = inputdlg('New threshold', 'Change threshold', 1, predicted); newThreshold = str2num(answer{1}) handles.bw = handles.volume < newThreshold; handles = updateScrollScreen(handles, 0); guidata(hObject, handles);