Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/api/middle-segmentation #22

Merged
merged 5 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions api/algorithms/algorithm_LargestSegmentation.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
function largestSegmentation = algorithm_LargestSegmentation(segmentations)
% AUTHOR: Lorenzo Drudi (E-mail: lorenzodrudi11@gmail.com)
% DATE: April 12, 2023
% DATE: April 20, 2023
% NAME: TDSFT (version 1.0)
%
% PARAMETERS:
% segmentations (Cell array: [1, raters] (Cells: matrix [height, width]):
% the segmentations to fuse.
% The function accepts two different input parameters:
%
% 1) segmentations (Cell array: [1, raters] (Cells: matrix [height, width]):
% the cell array containing the segmentations to fuse.
%
% 2) segmentations (Matrix [height, width]):
% the segmentations are already overlapped in a matrix.
% To overlap the segmentations must NOT be filled (the algorithm need NO dense segmentations).
%
% THROWS:
% largestSegmentations:emptyInput (Exception):
Expand All @@ -22,20 +28,25 @@

disp('Getting the largest segmentation...');

% Check if the input is empty
if isempty(segmentations)
ME = MException('largestSegmentation:emptyInput', 'Segmentations array empty');
throw(ME);
return;
end
if iscell(segmentations)

% If there is only one segmentation, return it
if length(segmentations) == 1
largestSegmentation = segmentations{1};
return;
end
% Check if the input is empty
if isempty(segmentations)
ME = MException('largestSegmentation:emptyInput', 'Segmentations array empty');
throw(ME);
return;
end

% If there is only one segmentation, return it
if length(segmentations) == 1
largestSegmentation = segmentations{1};
return;
end

% get the largest segmentation
overlap = overlapSegmentations(segmentations);
% get the largest segmentation
overlap = overlapSegmentations(segmentations);
else
overlap = segmentations;
end
largestSegmentation = getLargestSegmentation(overlap);
end
38 changes: 36 additions & 2 deletions api/algorithms/algorithm_MiddleSegmentation.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
% segmentations to fuse.
%
% algorithm:
% method to use for the last two segmentations left if the segmentations are even.
% algorithm to use for the last two segmentations left if the segmentations are even.
% Available algorithms:
% - 'LargestSegmentation'
% - 'SmallestSegmentation'
%
% THROWS:
% middleSegmentations:emptyInput (Exception):
% throwed if the input is empty.
%
% middleSegmentations:wrongInputs (Exception):
% throwed if the number of segmentations is even and the algorithm is not specified.
%
% middleSegmentations:algorithmNotAvailable (Exception):
% throwed if the algorithm choosed if the number of segmentations is even is not available.
%
% OUTPUT:
% middleSegmentation (Matrix [height, width])):
% the middle segmentation.
Expand All @@ -33,6 +42,13 @@
return;
end

% check if the number of segmentations is even and the algorithm is specified
if mod(length(segmentations), 2) == 0 && nargin < 2
ME = MException('middleSegmentation:wrongInputs', 'If the number of segmentations is even, the algorithm must be specified');
throw(ME);
return;
end

% if there is only one segmentation, return it
if length(segmentations) == 1
middleSegmentation = segmentations{1};
Expand Down Expand Up @@ -67,6 +83,24 @@
nSeg = nSeg - 2;
end

middleSegmentation = imbinarize(overlap);
% compute result
if isequal(nSeg, 2)
% if the number of segmentations is even, use the specified algorithm for the last two segmentations left
algorithm = fromSpacedToFullName(algorithm);
try
if strcmp(algorithm, 'algorithm_LargestSegmentation')
middleSegmentation = getLargestSegmentation(overlap);
elseif strcmp(algorithm, 'algorithm_SmallestSegmentation')
middleSegmentation = getSmallestSegmentation(overlapFilled, nSeg);
else
throw 'Algorithm not available';
end
catch ME
rethrow(ME);
end
else
% if the number of segmentations is odd, return the segmentation left
middleSegmentation = imbinarize(overlap);
end

end
68 changes: 46 additions & 22 deletions api/algorithms/algorithm_SmallestSegmentation.m
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
function smallestSegmentation = algorithm_SmallestSegmentation(segmentations)
function smallestSegmentation = algorithm_SmallestSegmentation(varargin)
% AUTHOR: Lorenzo Drudi (E-mail: lorenzodrudi11@gmail.com)
% DATE: April 12, 2023
% NAME: TDSFT (version 1.0)
%
% PARAMETERS:
% segmentations (Cell array: [1, raters] (Cells: matrix [height, width]):
% segmentations to fuse.
% varargin: The function accept both 1 or 2 argument as follows:
%
% 1 param)
% segmentations (Cell array: [1, raters] (Cells: matrix [height, width]):
% The array where each element is a segmentation.
% 2 param):
% overlap (Matrix [height, width]):
% the overlap of the segmentations (the segmentations are already overlapped).
% Before calling this function, the segmentations must be filled (need to be dense).
% nSeg (Integer):
% the number of segmentations.
%
% OUTPUT:
% smallestSegmentation (Matrix [height, width]):
% the smallest segmentation.
% THROWS:
% smallestSegmentations:emptyInput (Exception):
% throwed if the input is empty.
% smallestSegmentations:wrongInput (Exception):
% throwed if the input param number is wrong.
%
% DESCRIPTION:
% Fuse all % the segmentations together overlapping them and getting the smallest segmentation possible.
% Fuse all the segmentations together overlapping them if needed and getting the smallest segmentation possible.
% The smallest segmentation is the perimeter of the area covered by every segmentation (the common area between every segmentation).

disp('Getting the smallest segmentation...');

% Check if the input is empty
if isempty(segmentations)
ME = MException('smallestSegmentation:emptyInput', 'Segmentations array empty');
throw(ME);
return;
end
% if the segmentations are not overlapped, overlap them
if nargin == 1
segmentations = varargin{1};

% Check if the input is empty
if isempty(segmentations)
ME = MException('smallestSegmentation:emptyInput', 'Segmentations array empty');
throw(ME);
return;
end

% If there is only one segmentation, return it
if length(segmentations) == 1
smallestSegmentation = segmentations{1};
% If there is only one segmentation, return it
if length(segmentations) == 1
smallestSegmentation = segmentations{1};
return;
end

% overlap the filled segmentations
filledSegmentations = [];
for i=1:length(segmentations)
filledSegmentations{i} = imfill(segmentations{i}, 'holes');
end
overlap = overlapSegmentations(filledSegmentations);

% get the smallest segmentation
nSeg = length(segmentations);
elseif varargin == 2
overlap = varargin{1};
nSeg = varargin{2};
else
ME = MException('smallestSegmentation:wrongInput', 'Wrong number of input arguments');
throw(ME);
return;
end

% overlap the filled segmentations
filledSegmentations = [];
for i=1:length(segmentations)
filledSegmentations{i} = imfill(segmentations{i}, 'holes');
end
overlap = overlapSegmentations(filledSegmentations);

% get the smallest segmentation
nSeg = length(segmentations);
smallestSegmentation = getSmallestSegmentation(overlap, nSeg);
end
13 changes: 13 additions & 0 deletions api/algorithms/inputs/algorithm_MiddleSegmentation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"inputs": [
{
"name": "algorithm",
"type": "DropDown",
"value": [
"Smallest Segmentation",
"Largest Segmentation"
],
"optional": false
}
]
}
Loading