%% muscleContours % This script allows to select the contours on Tiff images of the four % cuff rotator muscles: Supraspinatus (SS), Infraspinatus (IS), Subscapularis (SC) % and Teres Minor (TM). % % INPUT: % subject: ### subject number % type: 'normal' or 'pathologic' % Author: EPFL-LBO % Date: 2016-09-05 %% function [] = muscleContours(subject,type) directory = '../../../data'; % Location of the CT database location = 'P'; CTn = sprintf('P%03d',subject); listDirInCurrDir = dir([directory '/' CTn(1) '/' CTn(2) '/' CTn(3)]); listDirInCurrDir = listDirInCurrDir(3:end); for i=1:length(listDirInCurrDir) if ~isempty(regexp(listDirInCurrDir(i).name,['^[PN]\d{0,2}[' CTn(4) '][-]\w*'], 'once')) listDirInCaseFolder = dir([listDirInCurrDir(i).folder '\' listDirInCurrDir(i).name]); listDirInCaseFolder = listDirInCaseFolder(3:end); % In all CT folders in the current Case Folder, find CT #1 for j=1:length(listDirInCaseFolder) if ~isempty(regexp(listDirInCaseFolder(j).name,'^[C][T][-][PN]\d{1,3}[-]\d*[-][1]', 'once')) outputPath = [listDirInCaseFolder(j).folder '\' listDirInCaseFolder(j).name]; end end end end pathologic_list = dir(sprintf('%s/%s/P*', directory, location)); %Create the case list for i = 1:1:numel(pathologic_list) % Loop over the list of patients and extract the complete name of the specific patient name = pathologic_list(i).name; t = strfind(name,CTn); if t == 1; patient_name = name; end end finaldirectory = [outputPath '\amira\*tif']; muscleName = cellstr(['SS';'IS';'SC';'TM']); close all; % Close all figure windows except those created by imtool. imtool close all; % Close all figure windows created by imtool. workspace; % Make sure the workspace panel is showing. fontSize = 16; imageWindow = [-100 200]; % Choose file [inputFile, inputPath] = uigetfile(finaldirectory,'Select the image for muscle atrophy measurement'); whereisp = strfind(inputFile, 'P'); if isempty(whereisp) whereisp = strfind(inputFile, 'N'); if isempty(whereisp) return end end subject = inputFile(whereisp:whereisp+3); % Read gray scale image. inputImage = imread(sprintf('%s%s',inputPath,inputFile)); if size(inputImage,3) == 3 % Calculate the monochrome luminance by combining the RGB values according to the NTSC standard inputImage = 0.2989*inputImage(:,:,1)... +0.5870*inputImage(:,:,2)... +0.1140*inputImage(:,:,3); end for i=1:4; finished = 'Redo'; while strcmp('Redo',finished) msg = 0; % 0 = no message % clc; % Clear command window. close all; % Close all figure windows except those created by imtool. imtool close all; % Close all figure windows created by imtool. imshow(inputImage,imageWindow); imagetitle = sprintf('Select %s ideal section', char(muscleName(i))); title(imagetitle, 'FontSize', fontSize); set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. if msg == 1 message = sprintf('Select the ideal section of your muscle in the image. \nLeft click to place points and draw a polygon. Click again on the first point to close the shape.\nYou can move the points once the line is closed. Double-click or right-click and "Create Mask" to finish.'); msg = 0; uiwait(msgbox(message)); end binaryImage1 = roipoly(); if isempty(binaryImage1) error('Invalid selection or interface closed') end binaryImage1 = bwmorph(binaryImage1,'majority'); % Get coordinates of the boundary of the polygonal drawn region. structBoundaries1 = bwboundaries(binaryImage1); xy1 = structBoundaries1{1}; % Get n by 2 array of x,y coordinates. x1 = xy1(:, 2); % Columns. y1 = xy1(:, 1); % Rows. hold on plot(x1, y1, 'color','green'); % check contours finished = questdlg('Please verify the contours. Press "Save" if they are satisfactory or "Redo" if you would like to redo the selection. ',... 'Contour verification','Save','Redo','Save'); end %% Report results % mkdir('./results',subject); if exist([outputPath '\muscles'],'dir') == 0 mkdir(outputPath,'muscles'); end outputPath2 = [outputPath '\muscles\' char(muscleName(i)) '\']; mkdir(outputPath2); outputFile = sprintf('ssContours%s',subject); % Check for overwriting overwrite = exist(sprintf('%s%s.mat',outputPath2,outputFile), 'file'); switch overwrite case 2 isoverwrite = questdlg('Overwrite existing file ?', 'Warning', 'No', 'Yes', 'No'); switch isoverwrite case 'Yes' save(sprintf('%s%s.mat',outputPath2,outputFile), 'inputImage','binaryImage1') saveas(gcf,sprintf('%s%s.tif',outputPath2,outputFile)) fprintf('%s%s.mat\n%s%s.tif\n',outputPath2,outputFile,outputPath2,outputFile); end case 0 save(sprintf('%s%s.mat',outputPath2,outputFile), 'inputImage','binaryImage1') saveas(gcf,sprintf('%s%s.tif',outputPath2,outputFile)) fprintf('%s%s.mat\n%s%s.tif\n',outputPath2,outputFile,outputPath2,outputFile); end end close all; end