Index: /hep2cells/combinerm.m
===================================================================
--- /hep2cells/combinerm.m	(revision 40)
+++ /hep2cells/combinerm.m	(revision 40)
@@ -0,0 +1,32 @@
+function w = combinerm(u,varargin)
+
+N = length(u);
+if (N==1)
+   N = length(varargin);
+   tmpu = u; clear u;
+   for i=1:N
+      u{i} = tmpu;
+   end
+end
+u
+if nargin~=N+1
+   error('I need as many untrained mappings as datasets.');
+end
+if (N<2)
+   error('I want to combine at least 2 datasets.');
+end
+
+for i=1:N
+   if ~isa(varargin{i},'dataset')
+      error('Input %d should be a dataset.',i+1);
+   end
+   dim(i) = size(varargin{i},2);
+end
+alldim = sum(dim);
+cdim = cumsum(dim);
+
+w = featsel(alldim,1:dim(1))*u{1};
+for i=2:N
+   w = [w featsel(alldim,cdim(i-1)+(1:dim(i)))*u{i}];
+end
+
Index: /hep2cells/docombine.m
===================================================================
--- /hep2cells/docombine.m	(revision 40)
+++ /hep2cells/docombine.m	(revision 40)
@@ -0,0 +1,63 @@
+% what do the simple approaches give?
+load cellssimplevar;
+gaborvar_train = a; gaborvar_test = b;
+load cellssimplemax;
+gabormax_train = a; gabormax_test = b;
+load cellssimplemean;
+gabormean_train = a; gabormean_test = b;
+gabor_data=[gabor_train;gabor_test];
+load cells_lbp_mask; gencellsimple
+lbp_train = a; lbp_test = b;
+load cells_lbp_gauss3; gencellsimple
+lbpold_train = a; lbpold_test = b;
+
+
+% param:
+%dim_gabor = size(gabor_train,2);
+%dim_lbp = size(lbp_train,2);
+%dim = dim_gabor+dim_lbp;
+%u = [featsel(dim,1:dim_gabor)*ldc featsel(dim,dim_gabor+(1:dim_lbp))*ldc]*meanc;
+%u = fisherm([],4)*knnc;
+%u = combinerm(ldc([],0,1e-5),gaborvar_train,gabormax_train,gabormean_train,lbp_train,lbpold_train)*ldc([],0,1e-5);
+u = combinerm(loglc2,gaborvar_train,gabormax_train,gabormean_train,lbp_train,lbpold_train)*maxc;
+%u = knnc;
+u = loglc2;
+u = scalem([],'variance')*libsvc;
+
+
+
+% train and test:
+w = [gaborvar_train gabormax_train gabormean_train lbp_train lbpold_train]*u;
+error_on_testset = [gaborvar_test gabormax_test gabormean_test lbp_test lbpold_test]*w*testc
+
+
+% crossval on all data a:
+a = [[gaborvar_train;gaborvar_test] [gabormax_train;gabormax_test] ...
+  [gabormean_train;gabormean_test] [lbp_train;lbp_test] ...
+  [lbpold_train;lbpold_test]];
+
+imlab = getident(a,'image');
+[nlab,baglab] = renumlab(imlab);
+nrfolds = length(baglab); % leave one image out
+
+e = zeros(nrfolds,1);
+Iall = (1:size(nlab,1))';
+
+for i=1:nrfolds
+   Jtst = find(nlab==i);
+   Jtrn = Iall; Jtrn(Jtst)=[];
+
+   i
+   x = a(Jtrn,:);
+   z = a(Jtst,:);
+   %f(i) = find(classsizes(z)>0)
+
+   w = x*u;
+   e(i,1) = z*w*testd;
+   %e(i,1)
+end
+e
+mean(e)
+error_on_testset
+
+
Index: /hep2cells/dosimple.m
===================================================================
--- /hep2cells/dosimple.m	(revision 40)
+++ /hep2cells/dosimple.m	(revision 40)
@@ -0,0 +1,53 @@
+% what do the simple approaches give?
+load cellssimplemax;
+%load cells_lbp_gauss3; gencellsimple
+xtrain = a; xtest = b;
+a = [a;b];
+
+%% add the different orientations...
+%b = a(:,1);
+%for i=0:7
+%   I = 1+((i*4):(i*4+3));
+%   b = [b mean(a(:,I),2)];
+%end
+%b = a;
+%a = a(:,[1:5]);
+
+
+% param:
+%u = scalem([],'variance')*knnc([],1);
+%u = fisherm([],4)*knnc;
+%u = scalem([],'variance')*parzenc;
+%u = fisherc;
+%u = ldc;
+u = scalem([],'variance')*libsvc;
+
+% train and test:
+w = xtrain*u;
+error_on_testset = xtest*w*testd
+
+
+% crossval on a:
+imlab = getident(a,'image');
+[nlab,baglab] = renumlab(imlab);
+nrfolds = length(baglab); % leave one image out
+
+e = zeros(nrfolds,1);
+Iall = (1:size(nlab,1))';
+
+for i=1:nrfolds
+   Jtst = find(nlab==i);
+   Jtrn = Iall; Jtrn(Jtst)=[];
+
+   x = a(Jtrn,:);
+   z = a(Jtst,:);
+%   f(i) = find(classsizes(z)>0)
+
+   w = x*u;
+   e(i,1) = z*w*testd;
+end
+e
+mean(e)
+error_on_testset 
+
+
Index: /hep2cells/gencellgabor.m
===================================================================
--- /hep2cells/gencellgabor.m	(revision 40)
+++ /hep2cells/gencellgabor.m	(revision 40)
@@ -0,0 +1,69 @@
+% load the data:
+load cells;
+
+% settings:
+g_s = [1 2 3 5]; % scale
+g_f = [0.1 0.3]; %freq
+g_d = [0 0.25 0.5 0.75]*pi; % direction
+
+% other 
+ns = length(g_s);
+nd = length(g_d);
+nf = length(g_f);
+
+% compute feature for train and test:
+dim = ns*nd*nf;
+N = size(x,1);
+for i=1:N
+   i
+   % get image data from green channel:
+   %im = x{i,1}(:,:,2);
+   im = sum(x{i,1}(:,:,2),3);
+   if (min(im(:))<0)
+      error('pixel values < 0');
+   end
+%   im = im/max(im(:));
+   im = double(hist_equalize(im));
+   %newx:
+   newx = zeros(size(im,1),size(im,2),dim+1);
+   newx(:,:,1) = im;
+   % get gabor features 
+   j=1;
+   for i1=1:ns
+      for i2=1:nf
+         for i3=1:nd
+            j=j+1;
+            newx(:,:,j) = abs(gabor(im,g_s(i1),g_f(i2),g_d(i3)));
+         end
+      end
+   end
+   x{i,1} = newx;
+end
+M = size(z,1);
+for i=1:M
+   i
+   % get image data from green channel:
+   %im = z{i,1}(:,:,2);
+   im = sum(z{i,1}(:,:,2),3);
+   if (min(im(:))<0)
+      error('pixel values < 0');
+   end
+%   im = im/max(im(:));
+   im = double(hist_equalize(im));
+   %newz:
+   newz = zeros(size(im,1),size(im,2),dim+1);
+   newz(:,:,1) = im;
+   % get gabor features 
+   j=1;
+   for i1=1:ns
+      for i2=1:nf
+         for i3=1:nd
+            j=j+1;
+            newz(:,:,j) = abs(gabor(im,g_s(i1),g_f(i2),g_d(i3)));
+         end
+      end
+   end
+   z{i,1} = newz;
+end
+
+save cells_gabor x z;
Index: /hep2cells/gencelllbp.m
===================================================================
--- /hep2cells/gencelllbp.m	(revision 40)
+++ /hep2cells/gencelllbp.m	(revision 40)
@@ -0,0 +1,43 @@
+% load the data:
+load cells;
+
+sig = 3;
+
+% type of LBP map
+maph= getlbpmap(8,'u2'); 
+
+% compute feature for train and test:
+N = size(x,1);
+for i=1:N
+   i
+   % get image data from green channel:
+  % im = sum(x{i,1}(:,:,2),3);
+  % if (min(im(:))<0)
+  %    error('pixel values < 0');
+ %  end
+   %im = double(hist_equalize(im));
+   
+   im = x{i,1}(:,:,2);
+   im = im_gauss(double(im),sig,sig);
+   im = im.*double(x{i,2});
+   x{i,1} = lbp(im,1,8,maph,'nh');
+       
+   
+end
+M = size(z,1);
+for i=1:M
+   i
+  % im = sum(z{i,1}(:,:,2),3);
+  % if (min(im(:))<0)
+  %    error('pixel values < 0');
+  % end
+   %im = double(hist_equalize(im));
+   
+
+   im = z{i,1}(:,:,2);
+   im = im_gauss(double(im),sig,sig);
+   im = im.*double(z{i,2});
+   z{i,1} = lbp(im,1,8,maph,'nh');
+end
+
+save cells_lbp_gauss3 x z;
Index: /hep2cells/gencellmil.m
===================================================================
--- /hep2cells/gencellmil.m	(revision 40)
+++ /hep2cells/gencellmil.m	(revision 40)
@@ -0,0 +1,61 @@
+% generate a mil dataset from cells
+clear all;load cells_gabor;
+
+delta = 5; % how fine to sample the image
+
+N = size(x,1);
+bags = cell(N,1);
+lab = [];
+imlab = [];
+for i=1:N
+   i
+   % store in:
+   thisbag = [];
+   % get gabor features
+   gdat = x{i,1};
+   msk = x{i,2};
+   sz = size(gdat);
+   for i1=1:delta:sz(1)
+      for i2=1:delta:sz(2)
+         if (msk(i1,i2)>0)
+            thisbag = [thisbag; squeeze(gdat(i1,i2,:))'];
+         end
+      end
+   end
+   bags{i} = thisbag;
+   %lab{i} = x{i,4};  % label of bag
+   lab = [lab; x{i,4}];
+   imlab = [imlab; repmat(x{i,3},size(thisbag,1),1)];  % original image label
+end
+a = genmil(bags,strvcat(lab));
+a = setident(a,imlab,'image');
+
+M = size(z,1);
+bags = cell(M,1);
+lab = [];
+imlab = [];
+for i=1:M
+   i
+   % store in:
+   thisbag = [];
+   % get gabor features
+   gdat = z{i,1};
+   msk = z{i,2};
+   sz = size(gdat);
+   for i1=1:delta:sz(1)
+      for i2=1:delta:sz(2)
+         if (msk(i1,i2)>0)
+            thisbag = [thisbag; squeeze(gdat(i1,i2,:))'];
+         end
+      end
+   end
+   bags{i} = thisbag;
+   %lab{i} = z{i,4};  % label of bag
+   lab = [lab; z{i,4}];
+   imlab = [imlab; repmat(z{i,3},size(thisbag,1),1)];  % original image label
+end
+b = genmil(bags,strvcat(lab));
+b = setident(b,imlab,'image');
+
+save cellsmil_delta5 a b;
+
Index: /hep2cells/gencellsimple.m
===================================================================
--- /hep2cells/gencellsimple.m	(revision 40)
+++ /hep2cells/gencellsimple.m	(revision 40)
@@ -0,0 +1,72 @@
+% generate a simple dataset for the cell images
+%clear all; load cells_gabor;
+
+%dtype = 'max';
+dtype = 'nothing';
+
+N = size(x,1);
+if ndims(x{1,1})<3
+   dim = size(x{1,1},2);
+else
+   dim = size(x{1,1},3);
+end
+feat = zeros(N,dim);
+imlab = zeros(N,1);
+for i=1:N
+   i
+   if strcmp(dtype,'nothing')
+      feat(i,:) = x{i,:};
+   else
+      for j=1:dim
+         gim = x{i,1}(:,:,j);
+         gim = gim(find(x{i,2}));
+         switch dtype
+         case 'max'
+            feat(i,j) = max(gim);
+         case 'mean'
+            feat(i,j) = mean(abs(gim));
+         case 'var'
+            feat(i,j) = var(gim);
+         end
+      end
+   end
+   xlab(i,:) = x{i,4};
+   imlab(i,1) = x{i,3};
+end
+a = dataset(feat,xlab);
+a = setident(a,imlab,'image');
+M = size(z,1);
+feat = zeros(M,dim);
+imlab = zeros(M,1);
+for i=1:M
+   i
+   if strcmp(dtype,'nothing')
+      feat(i,:) = z{i,:};
+   else
+      for j=1:dim
+         gim = z{i,1}(:,:,j);
+         gim = gim(find(z{i,2}));
+         switch dtype
+         case 'max'
+            feat(i,j) = max(gim);
+         case 'mean'
+            feat(i,j) = mean(abs(gim));
+         case 'var'
+            feat(i,j) = var(gim);
+         end
+      end
+   end
+   zlab(i,:) = z{i,4};
+   imlab(i,1) = z{i,3};
+end
+b = dataset(feat,zlab);
+b = setident(b,imlab,'image');
+
+switch dtype
+case 'max'
+   save cellssimplemax a b;
+case 'mean'
+   save cellssimplemean a b;
+case 'var'
+   save cellssimplevar a b;
+end
Index: /hep2cells/getlbpmap.m
===================================================================
--- /hep2cells/getlbpmap.m	(revision 40)
+++ /hep2cells/getlbpmap.m	(revision 40)
@@ -0,0 +1,88 @@
+%GETMAPPING returns a structure containing a mapping table for LBP codes.
+%  MAPPING = GETMAPPING(SAMPLES,MAPPINGTYPE) returns a 
+%  structure containing a mapping table for
+%  LBP codes in a neighbourhood of SAMPLES sampling
+%  points. Possible values for MAPPINGTYPE are
+%       'u2'   for uniform LBP
+%       'ri'   for rotation-invariant LBP
+%       'riu2' for uniform rotation-invariant LBP.
+%
+%  Example:
+%       I=imread('rice.tif');
+%       MAPPING=getmapping(16,'riu2');
+%       LBPHIST=lbp(I,2,16,MAPPING,'hist');
+%  Now LBPHIST contains a rotation-invariant uniform LBP
+%  histogram in a (16,2) neighbourhood.
+%
+
+function mapping = getlbpmap(samples,mappingtype)
+% Version 0.1.1
+% Authors: Marko Heikkilä and Timo Ahonen
+
+% Changelog
+% 0.1.1 Changed output to be a structure
+% Fixed a bug causing out of memory errors when generating rotation 
+% invariant mappings with high number of sampling points.
+% Lauge Sorensen is acknowledged for spotting this problem.
+
+
+
+table = 0:2^samples-1;
+newMax  = 0; %number of patterns in the resulting LBP code
+index   = 0;
+
+if strcmp(mappingtype,'u2') %Uniform 2
+  newMax = samples*(samples-1) + 3; 
+  for i = 0:2^samples-1
+    j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left
+    numt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and
+                                               %0->1 transitions
+                                               %in binary string 
+                                               %x is equal to the
+                                               %number of 1-bits in
+                                               %XOR(x,Rotate left(x)) 
+    if numt <= 2
+      table(i+1) = index;
+      index = index + 1;
+    else
+      table(i+1) = newMax - 1;
+    end
+  end
+end
+
+if strcmp(mappingtype,'ri') %Rotation invariant
+  tmpMap = zeros(2^samples,1) - 1;
+  for i = 0:2^samples-1
+    rm = i;
+    r  = i;
+    for j = 1:samples-1
+      r = bitset(bitshift(r,1,samples),1,bitget(r,samples)); %rotate
+                                                             %left
+      if r < rm
+        rm = r;
+      end
+    end
+    if tmpMap(rm+1) < 0
+      tmpMap(rm+1) = newMax;
+      newMax = newMax + 1;
+    end
+    table(i+1) = tmpMap(rm+1);
+  end
+end
+
+if strcmp(mappingtype,'riu2') %Uniform & Rotation invariant
+  newMax = samples + 2;
+  for i = 0:2^samples - 1
+    j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left
+    numt = sum(bitget(bitxor(i,j),1:samples));
+    if numt <= 2
+      table(i+1) = sum(bitget(i,1:samples));
+    else
+      table(i+1) = samples+1;
+    end
+  end
+end
+
+mapping.table=table;
+mapping.samples=samples;
+mapping.num=newMax;
