Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Clear Everything
- clear all;
- clc;
- close all;
- %% Automating Region of Interest (ROI) for many pictures of Scalpels
- % Load Scalpel Images and Data
- % location_scalpel = 'G:\My Drive\Surgical Helper\surgicaltooltestdata\Scalpel\*.jpg'; % folder in which your images exists
- % location_scalpel = 'G:\My Drive\Surgical Helper\Surgical-Dataset\Images\Scalpel\Alone\*.jpg';
- % imds_scalpel = imageDatastore(location_scalpel); % Creates a datastore for all images in your folder
- % readall(imds_scalpel); % Read all of the images
- % numFiles = numpartitions(imds_scalpel); % Find number of files
- % Load Scalpel and Curved Mayo Scissor Images and Data
- location_scalpel_scissor = 'G:\My Drive\Surgical Helper\surgicaltooltestdata\Curved Mayo Scissor and Scalpel\*.jpg';
- imds_scalpel_scissor = imageDatastore(location_scalpel_scissor); % Creates a datastore for all images in your folder
- readall(imds_scalpel_scissor); % Read all of the images
- numFiles = numpartitions(imds_scalpel_scissor); % Find number of files
- % Create Data Source
- % imageFiles = imds_scalpel.Files;
- imageFiles = imds_scalpel_scissor.Files;
- dataSource = groundTruthDataSource(imageFiles);
- % Create labelData from Scalpel Truth
- scalpelTruth = cell(88, 1); % preallocate
- for i = 1:44
- % Load Image to define the ROI of
- ROI_Image = readimage(imds_scalpel_scissor,i);
- % Object detection with BRISK Features of the image
- points = detectBRISKFeatures(ROI_Image);
- % points = detectMinEigenFeatures(ROI_Image);
- % Use min/max to determine bounding rectangle
- min_x = min(points.Location(:,1));
- max_x = max(points.Location(:,1));
- min_y = min(points.Location(:,2));
- max_y = max(points.Location(:,2));
- % Bounding rectangle coordinates for Scalpel AKA Scalpel Truth
- scalpelTruth{i}= [min_x min_y (max_x-min_x) (max_y-min_y)];
- % figure(i);
- % imshow(ROI_Image); hold on;
- % plot(points.selectStrongest(20));
- % hold on
- % % Use rectangle to draw bounding rectangle ROI
- % rectangle('Position',scalpelTruth(i,:), 'EdgeColor','red','LineWidth', 0.75 );
- end
- scissorTruth = cell(88, 1); % preallocate
- for i = 45:88
- % Load Image to define the ROI of
- ROI_Image = readimage(imds_scalpel_scissor,i);
- % Object detection with BRISK Features of the image
- points = detectBRISKFeatures(ROI_Image);
- % points = detectMinEigenFeatures(ROI_Image);
- % Use min/max to determine bounding rectangle
- min_x = min(points.Location(:,1));
- max_x = max(points.Location(:,1));
- min_y = min(points.Location(:,2));
- max_y = max(points.Location(:,2));
- % Bounding rectangle coordinates for Curved Mayo Scissor AKA Scissor Truth
- scissorTruth{i}= [min_x min_y (max_x-min_x) (max_y-min_y)];
- % figure(i);
- % imshow(ROI_Image); hold on;
- % plot(points.selectStrongest(20));
- % hold on
- % % Use rectangle to draw bounding rectangle ROI
- % rectangle('Position',scissorTruth1(i-44,:), 'EdgeColor','red','LineWidth', 0.75 );
- end
- labelNames = {'Scalpel';'Curved_Mayo_Scissor'};
- labelData = table(scalpelTruth,scissorTruth,'VariableNames',labelNames)
- % Create Label Definition
- ldc = labelDefinitionCreator();
- addLabel(ldc,'Scalpel',labelType.Rectangle);
- addLabel(ldc,'Curved_Mayo_Scissor',labelType.Rectangle);
- labelDefs = create(ldc);
- % Create your gTruth
- gTruth = groundTruth(dataSource,labelDefs,labelData)
- % Manual Gtruth
- % load('GroundTruth_Scalpel_Scissors.mat')
- %% Train R-CNN Surgical Tool Detector
- % Set network training options to use mini-batch size of 32 to reduce GPU
- % memory usage. Lower the InitialLearnRate to reduce the rate at which
- % network parameters are changed. This is beneficial when fine-tuning a
- % pre-trained network and prevents the network from changing too rapidly.
- options = trainingOptions('sgdm', ...
- 'MiniBatchSize', 32, ...
- 'InitialLearnRate', 1e-6, ...
- 'MaxEpochs', 10);
- % Train the R-CNN detector. Training can take a few minutes to complete.
- trainingdata = [gTruth.DataSource.Source, gTruth.LabelData];
- network=alexnet;
- layers = network.Layers;
- numClassesPlusBackground = numel(labelNames)+1;
- layers = [ ...
- imageInputLayer([28 28 1])
- convolution2dLayer(5,20)
- fullyConnectedLayer(numClassesPlusBackground);
- softmaxLayer()
- classificationLayer()];
- % rcnn = trainRCNNObjectDetector(trainingdata, network, options, 'NegativeOverlapRange', [0 0.3])
- rcnn = trainRCNNObjectDetector(trainingdata, network, options, 'NumStrongestRegions', 2000)
- % Test the R-CNN detector on a test image. Test Image of Scalpel
- img = imread('bisturi9TEST.jpg');
- [bboxes, scores, labels] = detect(rcnn, img, 'MiniBatchSize', 32);
- % Display strongest detection result.
- [scores, idx] = max(scores);
- bboxes = bboxes(idx, :);
- annotation = sprintf('%s: (Confidence = %f)', labels(idx), scores);
- detectedImg = insertObjectAnnotation(img, 'rectangle', bboxes, annotation);
- figure(1);
- imshow(detectedImg)
- title('BRISK Feature Object Detection and AlexNet CNN');
- % Test image 2 Curved Mayo Scissor
- img2 = imread('tesouracurva4TEST.jpg');
- [bboxes, scores, labels] = detect(rcnn, img2, 'MiniBatchSize', 32);
- % Display strongest detection result.
- [scores, idx] = max(scores);
- bboxes = bboxes(idx, :);
- annotation = sprintf('%s: (Confidence = %f)', labels(idx), scores);
- detectedImg2 = insertObjectAnnotation(img2, 'rectangle', bboxes, annotation);
- figure(2);
- imshow(detectedImg2)
- title('BRISK Feature Object Detection and AlexNet CNN');
- % Test image 3 Curved Mayo Scissor
- img3 = imread('scalpel_scissors_combined.jpg');
- [bboxes, scores, labels] = detect(rcnn, img3);
- % Display strongest detection result.
- [scores, idx] = max(scores);
- bboxes = bboxes(idx, :);
- annotation = sprintf('%s: (Confidence = %f)', labels(idx), scores);
- detectedImg3 = insertObjectAnnotation(img3, 'rectangle', bboxes, annotation);
- figure(3);
- imshow(detectedImg3)
- title('BRISK Feature Object Detection and AlexNet CNN');
- %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement