├── HOG.m ├── MAIN.m ├── README.md ├── SVM_TEST.m ├── SVM_Toolbox.rar ├── getmapping.m ├── lbp.m ├── lbptest.m ├── my_add.m ├── myplot.m ├── neg.mat ├── neg.rar ├── newSVM.m ├── pos.mat ├── pos.rar ├── readimg2.m ├── readneg.m ├── readpos.m ├── test1.jpg ├── test2.jpg ├── test3.jpg └── testvector.mat /HOG.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/HOG.m -------------------------------------------------------------------------------- /MAIN.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/MAIN.m -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # objective-detection 2 | 利用LBP进行特征提取,SVM进行2分类器建模;利用滑动窗口实现目标检测 3 | -------------------------------------------------------------------------------- /SVM_TEST.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/SVM_TEST.m -------------------------------------------------------------------------------- /SVM_Toolbox.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/SVM_Toolbox.rar -------------------------------------------------------------------------------- /getmapping.m: -------------------------------------------------------------------------------- 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 | 18 | function mapping = getmapping(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 | 30 | table = 0:2^samples-1; 31 | newMax = 0; %number of patterns in the resulting LBP code 32 | index = 0; 33 | 34 | if strcmp(mappingtype,'u2') %Uniform 2 35 | newMax = samples*(samples-1) + 3; 36 | for i = 0:2^samples-1 37 | if(i==196) 38 | l=1; 39 | end 40 | a=bitshift(i,1,'uint8'); 41 | b=bitget(i,samples); 42 | j = bitset(bitshift(i,1,'uint8'),1,bitget(i,samples)); %rotate left 43 | c=bitget(bitxor(i,j),1:samples); 44 | numt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and 45 | %0->1 transitions 46 | %in binary string 47 | %x is equal to the 48 | %number of 1-bits in 49 | %XOR(x,Rotate left(x)) 50 | if numt <= 2 51 | table(i+1) = index; 52 | index = index + 1; 53 | else 54 | table(i+1) = newMax - 1; 55 | end 56 | end 57 | end 58 | 59 | if strcmp(mappingtype,'ri') %Rotation invariant 60 | tmpMap = zeros(2^samples,1) - 1; 61 | for i = 0:2^samples-1 62 | rm = i; 63 | r = i; 64 | for j = 1:samples-1 65 | r = bitset(bitshift(r,1,'uint8'),1,bitget(r,samples)); %rotate 66 | %left 67 | if r < rm 68 | rm = r; 69 | end 70 | end 71 | if tmpMap(rm+1) < 0 72 | tmpMap(rm+1) = newMax; 73 | newMax = newMax + 1; 74 | end 75 | table(i+1) = tmpMap(rm+1); 76 | end 77 | end 78 | 79 | if strcmp(mappingtype,'riu2') %Uniform & Rotation invariant 80 | newMax = samples + 2; 81 | for i = 0:2^samples - 1 82 | j = bitset(bitshift(i,1,'uint8'),1,bitget(i,samples)); %rotate left 83 | numt = sum(bitget(bitxor(i,j),1:samples)); 84 | if numt <= 2 85 | table(i+1) = sum(bitget(i,1:samples)); 86 | else 87 | table(i+1) = samples+1; 88 | end 89 | end 90 | end 91 | 92 | mapping.table=table; 93 | mapping.samples=samples; 94 | mapping.num=newMax; -------------------------------------------------------------------------------- /lbp.m: -------------------------------------------------------------------------------- 1 | %LBP returns the local binary pattern image or LBP histogram of an image. 2 | % J = LBP(I,R,N,MAPPING,MODE) returns either a local binary pattern 3 | % coded image or the local binary pattern histogram of an intensity 4 | % image I. The LBP codes are computed using N sampling points on a 5 | % circle of radius R and using mapping table defined by MAPPING. 6 | % See the getmapping function for different mappings and use 0 for 7 | % no mapping. Possible values for MODE are 8 | % 'h' or 'hist' to get a histogram of LBP codes 9 | % 'nh' to get a normalized histogram 10 | % Otherwise an LBP code image is returned. 11 | % 12 | % J = LBP(I) returns the original (basic) LBP histogram of image I 13 | % 14 | % J = LBP(I,SP,MAPPING,MODE) computes the LBP codes using n sampling 15 | % points defined in (n * 2) matrix SP. The sampling points should be 16 | % defined around the origin (coordinates (0,0)). 17 | % 18 | % Examples 19 | % -------- 20 | % I=imread('rice.png'); 21 | % mapping=getmapping(8,'u2'); 22 | % H1=LBP(I,1,8,mapping,'h'); %LBP histogram in (8,1) neighborhood 23 | % %using uniform patterns 24 | % subplot(2,1,1),stem(H1); 25 | % 26 | % H2=LBP(I); 27 | % subplot(2,1,2),stem(H2); 28 | % 29 | % SP=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1]; 30 | % I2=LBP(I,SP,0,'i'); %LBP code image using sampling points in SP 31 | % %and no mapping. Now H2 is equal to histogram 32 | % %of I2. 33 | 34 | function result = lbp(varargin) % image,radius,neighbors,mapping,mode) 35 | % Version 0.3.2 36 | % Authors: Marko Heikkil?and Timo Ahonen 37 | 38 | % Changelog 39 | % Version 0.3.2: A bug fix to enable using mappings together with a 40 | % predefined spoints array 41 | % Version 0.3.1: Changed MAPPING input to be a struct containing the mapping 42 | % table and the number of bins to make the function run faster with high number 43 | % of sampling points. Lauge Sorensen is acknowledged for spotting this problem. 44 | 45 | 46 | % Check number of input arguments. 47 | error(nargchk(1,5,nargin)); 48 | image=varargin{1}; 49 | d_image=double(image); 50 | 51 | if nargin==1 52 | spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1]; 53 | neighbors=8; 54 | mapping=0; 55 | mode='h'; 56 | end 57 | 58 | if (nargin == 2) && (length(varargin{2}) == 1) 59 | error('Input arguments'); 60 | end 61 | 62 | if (nargin > 2) && (length(varargin{2}) == 1) 63 | radius=varargin{2}; 64 | neighbors=varargin{3}; 65 | 66 | spoints=zeros(neighbors,2); 67 | 68 | % Angle step. 69 | a = 2*pi/neighbors; 70 | 71 | for i = 1:neighbors 72 | spoints(i,1) = -radius*sin((i-1)*a); 73 | spoints(i,2) = radius*cos((i-1)*a); 74 | end 75 | 76 | if(nargin >= 4) 77 | mapping=varargin{4}; 78 | if(isstruct(mapping) && mapping.samples ~= neighbors) 79 | error('Incompatible mapping'); 80 | end 81 | else 82 | mapping=0; 83 | end 84 | 85 | if(nargin >= 5) 86 | mode=varargin{5}; 87 | else 88 | mode='h'; 89 | end 90 | end 91 | 92 | if (nargin > 1) && (length(varargin{2}) > 1) 93 | spoints=varargin{2}; 94 | neighbors=size(spoints,1); 95 | 96 | if(nargin >= 3) 97 | mapping=varargin{3}; 98 | if(isstruct(mapping) && mapping.samples ~= neighbors) 99 | error('Incompatible mapping'); 100 | end 101 | else 102 | mapping=0; 103 | end 104 | 105 | if(nargin >= 4) 106 | mode=varargin{4}; 107 | else 108 | mode='h'; 109 | end 110 | end 111 | 112 | % Determine the dimensions of the input image. 113 | [ysize xsize] = size(image); 114 | 115 | 116 | 117 | miny=min(spoints(:,1)); 118 | maxy=max(spoints(:,1)); 119 | minx=min(spoints(:,2)); 120 | maxx=max(spoints(:,2)); 121 | 122 | % Block size, each LBP code is computed within a block of size bsizey*bsizex 123 | bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1; 124 | bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1; 125 | 126 | % Coordinates of origin (0,0) in the block 127 | origy=1-floor(min(miny,0)); 128 | origx=1-floor(min(minx,0)); 129 | 130 | % Minimum allowed size for the input image depends 131 | % on the radius of the used LBP operator. 132 | if(xsize < bsizex || ysize < bsizey) 133 | error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)'); 134 | end 135 | 136 | % Calculate dx and dy; 137 | dx = xsize - bsizex; 138 | dy = ysize - bsizey; 139 | 140 | % Fill the center pixel matrix C. 141 | C = image(origy:origy+dy,origx:origx+dx); 142 | d_C = double(C); 143 | 144 | bins = 2^neighbors; 145 | 146 | % Initialize the result matrix with zeros. 147 | result=zeros(dy+1,dx+1); 148 | 149 | %Compute the LBP code image 150 | 151 | for i = 1:neighbors 152 | y = spoints(i,1)+origy; 153 | x = spoints(i,2)+origx; 154 | % Calculate floors, ceils and rounds for the x and y. 155 | fy = floor(y); cy = ceil(y); ry = round(y); 156 | fx = floor(x); cx = ceil(x); rx = round(x); 157 | % Check if interpolation is needed. 158 | if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6) 159 | % Interpolation is not needed, use original datatypes 160 | N = image(ry:ry+dy,rx:rx+dx); 161 | D = N > C; 162 | else 163 | % Interpolation needed, use double type images 164 | ty = y - fy; 165 | tx = x - fx; 166 | 167 | % Calculate the interpolation weights. 168 | w1 = (1 - tx) * (1 - ty); 169 | w2 = tx * (1 - ty); 170 | w3 = (1 - tx) * ty ; 171 | w4 = tx * ty ; 172 | % Compute interpolated pixel values 173 | a1=d_image(fy:fy+dy,fx:fx+dx); 174 | a2=d_image(fy:fy+dy,cx:cx+dx); 175 | a3=d_image(cy:cy+dy,fx:fx+dx); 176 | a4=d_image(cy:cy+dy,cx:cx+dx); 177 | N = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ... 178 | w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx); 179 | D = N > d_C; 180 | end 181 | % Update the result matrix. 182 | v = 2^(i-1); 183 | result = result + v*D; 184 | end 185 | %Apply mapping if it is defined 186 | if isstruct(mapping) 187 | bins = mapping.num; 188 | for i = 1:size(result,1) 189 | for j = 1:size(result,2) 190 | result(i,j) = mapping.table(result(i,j)+1); 191 | end 192 | end 193 | end 194 | 195 | if (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh')) 196 | % Return with LBP histogram if mode equals 'hist'. 197 | result=hist(result(:),0:(bins-1)); 198 | if (strcmp(mode,'nh')) 199 | result=result/sum(result); 200 | end 201 | else 202 | %Otherwise return a matrix of unsigned integers 203 | if ((bins-1)<=intmax('uint8')) 204 | result=uint8(result); 205 | elseif ((bins-1)<=intmax('uint16')) 206 | result=uint16(result); 207 | else 208 | result=uint32(result); 209 | end 210 | end 211 | 212 | end -------------------------------------------------------------------------------- /lbptest.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/lbptest.m -------------------------------------------------------------------------------- /my_add.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/my_add.m -------------------------------------------------------------------------------- /myplot.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/myplot.m -------------------------------------------------------------------------------- /neg.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/neg.mat -------------------------------------------------------------------------------- /neg.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/neg.rar -------------------------------------------------------------------------------- /newSVM.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/newSVM.m -------------------------------------------------------------------------------- /pos.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/pos.mat -------------------------------------------------------------------------------- /pos.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/pos.rar -------------------------------------------------------------------------------- /readimg2.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/readimg2.m -------------------------------------------------------------------------------- /readneg.m: -------------------------------------------------------------------------------- 1 | function [imglist]=readneg(rootpath) 2 | if nargin<1 3 | disp('Not enough parameters!'); 4 | return; 5 | end 6 | 7 | filelist=dir(rootpath);%get the filelist from rootpath 8 | [filenum,~]=size(filelist);%get the filelist's count 9 | 10 | num=filenum; 11 | 12 | for i=1:filenum 13 | if strcmp(filelist(i).name,'Thumbs.db') 14 | num=filenum-1; 15 | end 16 | end 17 | 18 | negvector=zeros(num-2,3780); 19 | tempind=0; 20 | imglist=cell(0);%define the var of imagedata list 21 | for i=1:filenum 22 | %ignore two special files: current catalog and father catalog 23 | if strcmp(filelist(i).name,'.')|| strcmp(filelist(i).name,'..')||strcmp(filelist(i).name,'Thumbs.db') 24 | %do nothing 25 | else 26 | tempind=tempind+1;%count for picture 27 | temp=imread(strcat(rootpath,'\',filelist(i).name)); 28 | temp=imresize(temp,[128 64]); 29 | imglist{tempind}=temp; 30 | negvector(tempind,:)=HOG(imglist{tempind}); 31 | end 32 | end 33 | 34 | save neg.mat negvector; 35 | -------------------------------------------------------------------------------- /readpos.m: -------------------------------------------------------------------------------- 1 | function [imglist]=readpos(rootpath) 2 | if nargin<1 3 | disp('Not enough parameters!'); 4 | return; 5 | end 6 | 7 | filelist=dir(rootpath);%get the filelist from rootpath 8 | [filenum,~]=size(filelist);%get the filelist's count 9 | 10 | num=filenum; 11 | 12 | for i=1:filenum 13 | if strcmp(filelist(i).name,'Thumbs.db') 14 | num=filenum-1; 15 | end 16 | end 17 | 18 | posvector=zeros(num-2,3780); 19 | tempind=0; 20 | imglist=cell(0);%define the var of imagedata list 21 | for i=1:filenum 22 | %ignore two special files: current catalog and father catalog 23 | if strcmp(filelist(i).name,'.')|| strcmp(filelist(i).name,'..')||strcmp(filelist(i).name,'Thumbs.db') 24 | %do nothing 25 | else 26 | tempind=tempind+1;%count for picture 27 | temp=imread(strcat(rootpath,'\',filelist(i).name)); 28 | temp=imresize(temp,[128 64]); 29 | imglist{tempind}=temp; 30 | posvector(tempind,:)=HOG(imglist{tempind}); 31 | 32 | end 33 | end 34 | 35 | save pos.mat posvector; 36 | % fprintf('pos.mat has been generated\n'); 37 | -------------------------------------------------------------------------------- /test1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/test1.jpg -------------------------------------------------------------------------------- /test2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/test2.jpg -------------------------------------------------------------------------------- /test3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/test3.jpg -------------------------------------------------------------------------------- /testvector.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fish-kong/objective-detection/4c72e1c0a1ee882283cf1a608c10a93e4059fb96/testvector.mat --------------------------------------------------------------------------------