source: hep2cells/getlbpmap.m @ 82

Last change on this file since 82 was 40, checked in by dtax, 12 years ago

HEp 2 cell classification, ICPR 2012 competition

File size: 2.7 KB
Line 
1%GETMAPPING returns a structure containing a mapping table for LBP codes.
2%  MAPPING = GETMAPPING(SAMPLES,MAPPINGTYPE) returns a
3%  structure containing a mapping table for
4%  LBP codes in a neighbourhood of SAMPLES sampling
5%  points. Possible values for MAPPINGTYPE are
6%       'u2'   for uniform LBP
7%       'ri'   for rotation-invariant LBP
8%       'riu2' for uniform rotation-invariant LBP.
9%
10%  Example:
11%       I=imread('rice.tif');
12%       MAPPING=getmapping(16,'riu2');
13%       LBPHIST=lbp(I,2,16,MAPPING,'hist');
14%  Now LBPHIST contains a rotation-invariant uniform LBP
15%  histogram in a (16,2) neighbourhood.
16%
17
18function mapping = getlbpmap(samples,mappingtype)
19% Version 0.1.1
20% Authors: Marko Heikkilä and Timo Ahonen
21
22% Changelog
23% 0.1.1 Changed output to be a structure
24% Fixed a bug causing out of memory errors when generating rotation
25% invariant mappings with high number of sampling points.
26% Lauge Sorensen is acknowledged for spotting this problem.
27
28
29
30table = 0:2^samples-1;
31newMax  = 0; %number of patterns in the resulting LBP code
32index   = 0;
33
34if strcmp(mappingtype,'u2') %Uniform 2
35  newMax = samples*(samples-1) + 3;
36  for i = 0:2^samples-1
37    j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left
38    numt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and
39                                               %0->1 transitions
40                                               %in binary string
41                                               %x is equal to the
42                                               %number of 1-bits in
43                                               %XOR(x,Rotate left(x))
44    if numt <= 2
45      table(i+1) = index;
46      index = index + 1;
47    else
48      table(i+1) = newMax - 1;
49    end
50  end
51end
52
53if strcmp(mappingtype,'ri') %Rotation invariant
54  tmpMap = zeros(2^samples,1) - 1;
55  for i = 0:2^samples-1
56    rm = i;
57    r  = i;
58    for j = 1:samples-1
59      r = bitset(bitshift(r,1,samples),1,bitget(r,samples)); %rotate
60                                                             %left
61      if r < rm
62        rm = r;
63      end
64    end
65    if tmpMap(rm+1) < 0
66      tmpMap(rm+1) = newMax;
67      newMax = newMax + 1;
68    end
69    table(i+1) = tmpMap(rm+1);
70  end
71end
72
73if strcmp(mappingtype,'riu2') %Uniform & Rotation invariant
74  newMax = samples + 2;
75  for i = 0:2^samples - 1
76    j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left
77    numt = sum(bitget(bitxor(i,j),1:samples));
78    if numt <= 2
79      table(i+1) = sum(bitget(i,1:samples));
80    else
81      table(i+1) = samples+1;
82    end
83  end
84end
85
86mapping.table=table;
87mapping.samples=samples;
88mapping.num=newMax;
Note: See TracBrowser for help on using the repository browser.