From 333874aa94b531dc219c7f7aeb6f3f60b17983ff Mon Sep 17 00:00:00 2001
From: Elian Dib <elian.dib@inria.fr>
Date: Wed, 4 Sep 2019 16:02:52 +0200
Subject: [PATCH] Fixed incorrect label size when using color

---
 factorize.m | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/factorize.m b/factorize.m
index 35a153f..769b254 100644
--- a/factorize.m
+++ b/factorize.m
@@ -3,8 +3,8 @@ function [Ref,C,Value,U,S,V] = factorize(Ref,method,k,varargin)
 %   Detailed explanation goes here
 
 [Offset,Color,Label] = SR.SetToFields(Ref);
-Size = cellfun(@size,Color,'UniformOutput',false);
-[ImgSize,ImgRes] = cellfun(@(x) deal(x(1:3),x(4:end)),Size,'UniformOutput',false);
+ColSize = cellfun(@size,Color,'UniformOutput',false);
+LabSize = cellfun(@size,Label,'UniformOutput',false);
 RefSize = size(Ref);
 numLab = numel(Label);
 numDims = ndims(Color{1});
@@ -23,16 +23,22 @@ if (isempty(colDims)&&isempty(rowDims)); colDims = 1:min(3,numDims); end
 if isempty(colDims); colDims = setdiff(1:numDims,rowDims); end
 if isempty(rowDims); rowDims = setdiff(1:numDims,colDims); end
 
+[ColImgSize,LabImgSize] = deal(cell(1,numLab));
+[ColImgSize(:),LabImgSize(:)] = deal({nan(1,numel(colDims))});
+
 for lab = 1:numLab
-    ImgSize{lab} = Size{lab}(colDims);
-    ImgRes {lab} = Size{lab}(rowDims);
+    ColImgSize{lab} = ColSize{lab}(colDims);
+    LabImgSize{lab} = LabSize{lab}(colDims);
     
-    Label{lab} = Label{lab}.*ones(Size{lab});
+    LabelMask  = repmat(Label{lab},size(Color{lab})./size(Label{lab}));
+    LabelMask  = permute(LabelMask ,[colDims,rowDims]);
     Label{lab} = permute(Label{lab},[colDims,rowDims]);
-    Label{lab} = reshape(Label{lab},prod(ImgSize{lab}),prod(ImgRes{lab}));
     Color{lab} = permute(Color{lab},[colDims,rowDims]);
-    Color{lab} = reshape(Color{lab},prod(ImgSize{lab}),prod(ImgRes{lab}));
-    Color{lab}(Label{lab}==0) = nan;
+    LabelMask  = reshape(LabelMask ,prod(ColImgSize{lab}),[]);
+    Label{lab} = reshape(Label{lab},prod(LabImgSize{lab}),[]);
+    Color{lab} = reshape(Color{lab},prod(ColImgSize{lab}),[]);
+    LabelMask  = any(LabelMask==0,2);
+    Color{lab}(LabelMask,:) = nan;
 end
 
 switch method
@@ -52,17 +58,16 @@ ImgRes(:) = deal({[k,1]});
 
 switch method
     case 'global'
-        Color = Matrix2Set(Value{1},ImgSize,ImgRes);
+        Color = Matrix2Set(Value{1},ColImgSize,ImgRes);
         C = C{1};
     case 'local'
         Color = Value;
 end
 
 for lab = 1:numLab
-    Color{lab} = reshape(Color{lab},[ImgSize{lab},ImgRes{lab}]);
+    Color{lab} = reshape(Color{lab},[ColImgSize{lab},k]);
     Label{lab} = repmat(Label{lab}(:,1),1,k);
-    ImgRes{lab} = [k,1];
-    Label{lab} = reshape(Label{lab},[ImgSize{lab},ImgRes{lab}]);
+    Label{lab} = reshape(Label{lab},[LabImgSize{lab},k]);
     Color{lab} = ipermute(Color{lab},[colDims,rowDims]);
     Label{lab} = ipermute(Label{lab},[colDims,rowDims]);
 end
-- 
GitLab