Advertisement
Guest User

Not detecting multiple surgical tools

a guest
Jun 20th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.07 KB | None | 0 0
  1. % Clear Everything
  2.  
  3. clear all;
  4. clc;
  5. close all;
  6.  
  7.  
  8. %% Automating Region of Interest (ROI) for many pictures of Scalpels
  9.  
  10. % Load Scalpel Images and Data
  11. % location_scalpel = 'G:\My Drive\Surgical Helper\surgicaltooltestdata\Scalpel\*.jpg';       %  folder in which your images exists
  12. % location_scalpel = 'G:\My Drive\Surgical Helper\Surgical-Dataset\Images\Scalpel\Alone\*.jpg';
  13.  
  14. % imds_scalpel = imageDatastore(location_scalpel);         %  Creates a datastore for all images in your folder
  15. % readall(imds_scalpel);                       % Read all of the images
  16. % numFiles = numpartitions(imds_scalpel);     % Find number of files
  17.  
  18.  
  19. % Load Scalpel and Curved Mayo Scissor Images and Data
  20. location_scalpel_scissor = 'G:\My Drive\Surgical Helper\surgicaltooltestdata\Curved Mayo Scissor and Scalpel\*.jpg';
  21. imds_scalpel_scissor = imageDatastore(location_scalpel_scissor);         %  Creates a datastore for all images in your folder
  22. readall(imds_scalpel_scissor);                       % Read all of the images
  23. numFiles = numpartitions(imds_scalpel_scissor);     % Find number of files
  24.  
  25. % Create Data Source
  26.  
  27. % imageFiles = imds_scalpel.Files;
  28. imageFiles = imds_scalpel_scissor.Files;
  29. dataSource = groundTruthDataSource(imageFiles);
  30.  
  31.  
  32.  
  33. % Create labelData from Scalpel Truth
  34.  
  35. scalpelTruth = cell(88, 1); % preallocate
  36.  
  37. for i = 1:44
  38.     % Load Image to define the ROI of
  39.     ROI_Image = readimage(imds_scalpel_scissor,i);
  40.    
  41.     % Object detection with BRISK Features of the image
  42.     points = detectBRISKFeatures(ROI_Image);
  43.     % points = detectMinEigenFeatures(ROI_Image);
  44.    
  45.     % Use min/max to determine bounding rectangle
  46.     min_x = min(points.Location(:,1));
  47.     max_x = max(points.Location(:,1));
  48.     min_y = min(points.Location(:,2));
  49.     max_y = max(points.Location(:,2));
  50.    
  51.     % Bounding rectangle coordinates for Scalpel AKA Scalpel Truth
  52.     scalpelTruth{i}= [min_x min_y (max_x-min_x) (max_y-min_y)];
  53.  
  54. %     figure(i);
  55. %     imshow(ROI_Image); hold on;
  56. %     plot(points.selectStrongest(20));
  57. %     hold on
  58. %     % Use rectangle to draw bounding rectangle ROI
  59. %     rectangle('Position',scalpelTruth(i,:), 'EdgeColor','red','LineWidth', 0.75 );
  60. end
  61.  
  62. scissorTruth = cell(88, 1); % preallocate
  63.  
  64. for i = 45:88
  65.     % Load Image to define the ROI of
  66.     ROI_Image = readimage(imds_scalpel_scissor,i);
  67.    
  68.     % Object detection with BRISK Features of the image
  69.     points = detectBRISKFeatures(ROI_Image);
  70.     % points = detectMinEigenFeatures(ROI_Image);
  71.    
  72.     % Use min/max to determine bounding rectangle
  73.     min_x = min(points.Location(:,1));
  74.     max_x = max(points.Location(:,1));
  75.     min_y = min(points.Location(:,2));
  76.     max_y = max(points.Location(:,2));
  77.    
  78.     % Bounding rectangle coordinates for Curved Mayo Scissor AKA Scissor Truth
  79.     scissorTruth{i}= [min_x min_y (max_x-min_x) (max_y-min_y)];
  80.  
  81. %     figure(i);
  82. %     imshow(ROI_Image); hold on;
  83. %     plot(points.selectStrongest(20));
  84. %     hold on
  85. %     % Use rectangle to draw bounding rectangle ROI
  86. %     rectangle('Position',scissorTruth1(i-44,:), 'EdgeColor','red','LineWidth', 0.75 );
  87. end
  88.  
  89. labelNames = {'Scalpel';'Curved_Mayo_Scissor'};
  90. labelData = table(scalpelTruth,scissorTruth,'VariableNames',labelNames)
  91.  
  92. % Create Label Definition
  93. ldc = labelDefinitionCreator();
  94. addLabel(ldc,'Scalpel',labelType.Rectangle);
  95. addLabel(ldc,'Curved_Mayo_Scissor',labelType.Rectangle);
  96. labelDefs = create(ldc);
  97.  
  98.  
  99. % Create your gTruth
  100. gTruth = groundTruth(dataSource,labelDefs,labelData)
  101.  
  102.  
  103. % Manual Gtruth
  104. % load('GroundTruth_Scalpel_Scissors.mat')
  105.  
  106. %% Train R-CNN Surgical Tool Detector
  107.  
  108. % Set network training options to use mini-batch size of 32 to reduce GPU
  109. % memory usage. Lower the InitialLearnRate to reduce the rate at which
  110. % network parameters are changed. This is beneficial when fine-tuning a
  111. % pre-trained network and prevents the network from changing too rapidly.
  112.  
  113. options = trainingOptions('sgdm', ...
  114.   'MiniBatchSize', 32, ...
  115.   'InitialLearnRate', 1e-6, ...
  116.   'MaxEpochs', 10);
  117.  
  118.  
  119. % Train the R-CNN detector. Training can take a few minutes to complete.
  120.  
  121. trainingdata = [gTruth.DataSource.Source, gTruth.LabelData];
  122. network=alexnet;
  123.  
  124. layers = network.Layers;
  125.  
  126. numClassesPlusBackground = numel(labelNames)+1;
  127.  
  128. layers = [ ...
  129.     imageInputLayer([28 28 1])
  130.     convolution2dLayer(5,20)        
  131.     fullyConnectedLayer(numClassesPlusBackground);
  132.     softmaxLayer()
  133.     classificationLayer()];
  134.  
  135.  
  136. % rcnn = trainRCNNObjectDetector(trainingdata, network, options, 'NegativeOverlapRange', [0 0.3])
  137. rcnn = trainRCNNObjectDetector(trainingdata, network, options, 'NumStrongestRegions', 2000)
  138.  
  139.  
  140. % Test the R-CNN detector on a test image. Test Image of Scalpel
  141. img = imread('bisturi9TEST.jpg');
  142.  
  143. [bboxes, scores, labels] = detect(rcnn, img, 'MiniBatchSize', 32);
  144.  
  145. % Display strongest detection result.
  146. [scores, idx] = max(scores);
  147.  
  148. bboxes = bboxes(idx, :);
  149. annotation = sprintf('%s: (Confidence = %f)', labels(idx), scores);
  150.  
  151. detectedImg = insertObjectAnnotation(img, 'rectangle', bboxes, annotation);
  152.  
  153. figure(1);
  154. imshow(detectedImg)
  155. title('BRISK Feature Object Detection and AlexNet CNN');
  156.  
  157. % Test image 2 Curved Mayo Scissor
  158. img2 = imread('tesouracurva4TEST.jpg');
  159.  
  160. [bboxes, scores, labels] = detect(rcnn, img2, 'MiniBatchSize', 32);
  161.  
  162. % Display strongest detection result.
  163. [scores, idx] = max(scores);
  164.  
  165. bboxes = bboxes(idx, :);
  166. annotation = sprintf('%s: (Confidence = %f)', labels(idx), scores);
  167.  
  168. detectedImg2 = insertObjectAnnotation(img2, 'rectangle', bboxes, annotation);
  169.  
  170. figure(2);
  171. imshow(detectedImg2)
  172. title('BRISK Feature Object Detection and AlexNet CNN');
  173.  
  174. % Test image 3 Curved Mayo Scissor
  175. img3 = imread('scalpel_scissors_combined.jpg');
  176.  
  177. [bboxes, scores, labels] = detect(rcnn, img3);
  178.  
  179. % Display strongest detection result.
  180. [scores, idx] = max(scores);
  181.  
  182. bboxes = bboxes(idx, :);
  183. annotation = sprintf('%s: (Confidence = %f)', labels(idx), scores);
  184.  
  185. detectedImg3 = insertObjectAnnotation(img3, 'rectangle', bboxes, annotation);
  186.  
  187. figure(3);
  188. imshow(detectedImg3)
  189. title('BRISK Feature Object Detection and AlexNet CNN');
  190.  
  191.  
  192.  
  193. %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement