Demo illustrating HAAR features
HAAR's features of image I
Usage
------
z = haar(I , [rect_param] , [F] , [standardize] );
Inputs
-------
I Images (Ny x Nx x N) in UINT8 format
rect_param Features rectangles parameters (10 x nR), where nR is the total number of rectangles of all features (see gui_features_dictionnary function).
(default Vertical(2 x 1), Horizontal(1 x 2) features)
F Features lists (6 x nF) int UINT32 (default full haar features of size(ny x nx) with 2 patterns, see haar_featlist function)
standardize Standardize Input Images 1 = yes, 0 = no (default = 1) Outputs
-------
z Haar features matrix (nF x P) in INT16 format for each positions (y,x) in [1+h,...,ny-h]x[1+w,...,nx-w] and (w,h) integral block size.
Contents
First example : compute Haar features for nP type of patterns
clear, close all
load viola_24x24
load haar_dico_2
nP = length(unique(rect_param(1 , :)));
Ny = 24;
Nx = 24;
N = 200;
F = haar_featlist(Ny , Nx , rect_param);
pos = find(y == 1);
neg = find(y == -1);
I = (X(: , : , [pos(1:N) , neg(1:N)]));
tic,z = haar(I , rect_param , F );,toc
imagesc(z)
title(sprintf('Haar''s features with %d different patterns' , nP));
colormap(gray)
disp('Press key to continue')
pause
Elapsed time is 1.537184 seconds.
Press key to continue
Second example : Display Haar features for a particular pattern scale
load viola_24x24
load haar_dico_2
[Ny,Nx,P] = size(X);
pattern = [2 ; 1];
F = haar_featlist(Ny , Nx , rect_param);
ind = find(sum(F([4;5] , :) == repmat(pattern , 1 , size(F , 2))) == 2);
FF = F(: , ind);
y = FF(3 , :);
x = FF(2 , :);
ind = (y + 1 + (x)*Ny)';
z = haar(X , rect_param , F(: , ind));
Ihaar = zeros(Ny , Nx , P , class(z));
for i = 1:P
Ihaar(ind + (i-1)*Ny*Nx) = z(: , i);
Ihaar(: , : , i) = Ihaar(: , : , i)';
end
figure
display_database(X);
title(sprintf('Original database (click zoom to see images)'));
figure
display_database(Ihaar);
title(sprintf('Haar''s features with pattern [%d %d] (click zoom to see images)' , pattern(1) , pattern(2)));
disp('Press key to continue')
pause
Press key to continue
Third example : Display a particular Haar feature computed on all the database
load viola_24x24
load haar_dico_2
nP = length(unique(rect_param(1 , :)));
Ny = 24;
Nx = 24;
nF = 1;
F = haar_featlist(Ny , Nx , rect_param);
indpos = find(y == 1);
indneg = find(y == -1);
tic,z = haar(X , rect_param , F(: , nF) );,toc
plot(indpos , z(indpos) , indneg , z(indneg) , 'r')
legend('Faces' , 'Non-faces')
title(sprintf('Haar feature = %d for %d different patterns' , nF , nP));
colormap(gray)
disp('Press key to continue')
pause
Elapsed time is 0.038745 seconds.
Press key to continue
Fourth example : Haar feature's computed on Face/Non-Face image
load viola_24x24
load haar_dico_2
Ny = 24;
Nx = 24;
F = haar_featlist(Ny , Nx , rect_param);
nF = size(F , 2);
indpos = find(y==1);
indneg = find(y==-1);
tic,zpos = haar(X(: , : , indpos(1)) , rect_param , F );,toc
tic,zneg = haar(X(: , : , indneg(1)) , rect_param , F );,toc
figure
plot((1:nF)' , zpos , (1:nF)' , zneg , 'r')
title(sprintf('Haar''s features computed on Face/Non-Face images'));
legend('Positive' , 'Negative')
disp('Press key to continue')
pause
Elapsed time is 0.004138 seconds.
Elapsed time is 0.003773 seconds.
Press key to continue
Fifth example : Display Best Feature values computed on Viola-Jones database separating the 2 classes
load viola_24x24
load haar_dico_2
load model_detector_haar_24x24
bestFeat = model.param(1 , 1);
thresh = model.param(2 , 1);
[Ny,Nx,P] = size(X);
F = haar_featlist(Ny , Nx , rect_param);
indpos = find(y==1);
indneg = find(y==-1);
z = haar(X , rect_param , F(: , bestFeat));
figure
plot(indpos , z(indpos) , indneg , z(indneg) , 'r' , (1:length(z)) , thresh*ones(1,length(z)) , 'g')
legend('Faces' , 'Non-faces' , '\theta')
title(sprintf('Best Haar Feature = %d' , bestFeat))
figure
[Nneg , Xneg] = hist(double(z(indneg)) , 100 , 'r' );
bar(Xneg , Nneg)
set(get(gca , 'children') , 'facecolor' , [1 0 1])
hold on
[Npos , Xpos] = hist(double(z(indpos)) , 100 );
bar(Xpos , Npos);
plot(thresh*ones(1,2) , [0 , max([Nneg , Npos])] , 'g' , 'linewidth' , 2)
hold off
legend(get(gca , 'children') , '\theta', 'Faces' , 'Non-faces' )
title(sprintf('Best Haar Feature = %d' , bestFeat))
disp('Press key to continue')
pause
Press key to continue
Sixth example : Display best Haar's Features from Adaboosting & FastAdaboosting
load viola_24x24
load haar_dico_2
y = int8(y);
II = image_integral_standard(X);
[Ny , Nx , P] = size(II);
Nimage = 110;
nb_feats = 3;
T = nb_feats;
F = haar_featlist(Ny , Nx , rect_param);
I = X(: , : , Nimage);
index = randperm(length(y));
tic,model0 = haar_adaboost_binary_model_cascade(II(: , : , index) , y(index) , rect_param , F , T);,toc
G = Haar_matG(Ny , Nx , rect_param);
tic,model1 = fast_haar_adaboost_binary_model_cascade(II(: , : , index) , y(index) , G , T);,toc
figure
imagesc(I)
hold on
best_feats = (F(: , model0(1 , 1:nb_feats)));
x = double(best_feats(2 , :)) + 0.5 ;
y = double(best_feats(3 , :)) + 0.5;
w = best_feats(4 , :);
h = best_feats(5 , :);
indR = fix(best_feats(6 , :) + 1)/10 + 1;
R = rect_param(4 , indR);
for f = 1 : nb_feats
for r = 0:R(f)-1
coeffw = w(f)/rect_param(2 , indR(f) + r);
coeffh = h(f)/rect_param(3 , indR(f) + r);
xr = (x(f) + double(coeffw*rect_param(6 , indR(f) + r)));
yr = (y(f) + double(coeffh*rect_param(7 , indR(f) + r))) ;
wr = double(coeffw*(rect_param(8 , indR(f) + r) - 0));
hr = double(coeffh*(rect_param(9 , indR(f) + r) - 0));
s = rect_param(10 , indR(f) + r);
if (s == 1)
color = [0.9 0.9 0.9];
else
color = [0.1 0.1 0.1];
end
hh = rectangle('Position', [xr, yr , wr , hr] );
p = patch([xr , xr+wr , xr + wr , xr] , [yr , yr , yr + hr , yr + hr] , color);
alpha(p , 0.8);
set(hh , 'linewidth' , 2 , 'EdgeColor' , [1 0 0])
end
end
hold off
title(sprintf('Best %d Haar features with Adaboost' , nb_feats) , 'fontsize' , 13)
colormap(gray)
figure
imagesc(I)
hold on
best_feats = (F(: , model1(1 , 1:nb_feats)));
x = double(best_feats(2 , :)) + 0.5 ;
y = double(best_feats(3 , :)) + 0.5;
w = best_feats(4 , :);
h = best_feats(5 , :);
indR = fix(best_feats(6 , :) + 1)/10 + 1;
R = rect_param(4 , indR);
for f = 1 : nb_feats
for r = 0:R(f)-1
coeffw = w(f)/rect_param(2 , indR(f) + r);
coeffh = h(f)/rect_param(3 , indR(f) + r);
xr = (x(f) + double(coeffw*rect_param(6 , indR(f) + r)));
yr = (y(f) + double(coeffh*rect_param(7 , indR(f) + r))) ;
wr = double(coeffw*(rect_param(8 , indR(f) + r) - 0));
hr = double(coeffh*(rect_param(9 , indR(f) + r) - 0));
s = rect_param(10 , indR(f) + r);
if (s == 1)
color = [0.9 0.9 0.9];
else
color = [0.1 0.1 0.1];
end
hh = rectangle('Position', [xr, yr , wr , hr] );
p = patch([xr , xr+wr , xr + wr , xr] , [yr , yr , yr + hr , yr + hr] , color);
alpha(p , 0.8);
set(hh , 'linewidth' , 2 , 'EdgeColor' , [1 0 0])
end
end
hold off
title(sprintf('Best %d Haar features with FastAdaboost' , nb_feats) , 'fontsize' , 13)
colormap(gray)
disp('Press key to continue')
pause
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\haar_adaboost_binary_model_cascade.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
1/1
1/1001
1/2001
1/3001
1/4001
1/5001
1/6001
1/7001
1/8001
1/9001
1/10001
1/11001
1/12001
1/13001
1/14001
1/15001
1/16001
1/17001
1/18001
1/19001
1/20001
1/21001
1/22001
1/23001
1/24001
1/25001
1/26001
1/27001
1/28001
1/29001
1/30001
1/31001
1/32001
1/33001
1/34001
1/35001
1/36001
1/37001
1/38001
1/39001
1/40001
1/41001
1/42001
1/43001
1/44001
1/45001
1/46001
1/47001
1/48001
1/49001
1/50001
1/51001
1/52001
1/53001
1/54001
1/55001
1/56001
1/57001
1/58001
1/59001
1/60001
1/61001
1/62001
1/63001
1/64001
1/65001
1/66001
1/67001
1/68001
1/69001
1/70001
1/71001
1/72001
1/73001
1/74001
1/75001
1/76001
1/77001
1/78001
1/79001
1/80001
1/81001
1/82001
1/83001
1/84001
1/85001
1/86001
2/1
2/1001
2/2001
2/3001
2/4001
2/5001
2/6001
2/7001
2/8001
2/9001
2/10001
2/11001
2/12001
2/13001
2/14001
2/15001
2/16001
2/17001
2/18001
2/19001
2/20001
2/21001
2/22001
2/23001
2/24001
2/25001
2/26001
2/27001
2/28001
2/29001
2/30001
2/31001
2/32001
2/33001
2/34001
2/35001
2/36001
2/37001
2/38001
2/39001
2/40001
2/41001
2/42001
2/43001
2/44001
2/45001
2/46001
2/47001
2/48001
2/49001
2/50001
2/51001
2/52001
2/53001
2/54001
2/55001
2/56001
2/57001
2/58001
2/59001
2/60001
2/61001
2/62001
2/63001
2/64001
2/65001
2/66001
2/67001
2/68001
2/69001
2/70001
2/71001
2/72001
2/73001
2/74001
2/75001
2/76001
2/77001
2/78001
2/79001
2/80001
2/81001
2/82001
2/83001
2/84001
2/85001
2/86001
3/1
3/1001
3/2001
3/3001
3/4001
3/5001
3/6001
3/7001
3/8001
3/9001
3/10001
3/11001
3/12001
3/13001
3/14001
3/15001
3/16001
3/17001
3/18001
3/19001
3/20001
3/21001
3/22001
3/23001
3/24001
3/25001
3/26001
3/27001
3/28001
3/29001
3/30001
3/31001
3/32001
3/33001
3/34001
3/35001
3/36001
3/37001
3/38001
3/39001
3/40001
3/41001
3/42001
3/43001
3/44001
3/45001
3/46001
3/47001
3/48001
3/49001
3/50001
3/51001
3/52001
3/53001
3/54001
3/55001
3/56001
3/57001
3/58001
3/59001
3/60001
3/61001
3/62001
3/63001
3/64001
3/65001
3/66001
3/67001
3/68001
3/69001
3/70001
3/71001
3/72001
3/73001
3/74001
3/75001
3/76001
3/77001
3/78001
3/79001
3/80001
3/81001
3/82001
3/83001
3/84001
3/85001
3/86001
Elapsed time is 1173.694266 seconds.
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\int8tosparse.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\fast_haar_adaboost_binary_model_cascade.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
Elapsed time is 1.894494 seconds.
Press key to continue
Seventh example : FastAdaboosting on Haar's features with 2 different types of pattern
load viola_24x24
load haar_dico_2
[Ny , Nx , P] = size(X);
T = 100;
II = image_integral_standard(X);
y = int8(y);
indp = find(y == 1);
indn = find(y ==-1);
F = haar_featlist(Ny , Nx , rect_param);
G = Haar_matG(Ny , Nx , rect_param);
tic,model = fast_haar_adaboost_binary_model_cascade(II , y , G , T);,toc
[yest , fx] = haar_adaboost_binary_predict_cascade(II , model , rect_param , F);
tp = sum(yest(indp) == y(indp))/length(indp)
fp = 1 - sum(yest(indn) == y(indn))/length(indn)
perf = sum(yest == y)/length(y)
[tpp , fpp] = basicroc(y , fx);
figure
plot(fx)
figure
plot(fpp , tpp , 'linewidth' , 2)
axis([-0.02 , 1.02 , -0.02 , 1.02])
title(sprintf('ROC for Fastadaboosting with T = %d' , T))
figure
plot(abs(model(3 , :)) , 'linewidth' , 2)
grid on
xlabel('Weaklearner m')
ylabel('|a_m|')
title('Weights of the Weaklearner')
Elapsed time is 56.433729 seconds.
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\haar_adaboost_binary_predict_cascade.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
tp =
0.9552
fp =
0.0215
perf =
0.9696
Eigth example : Multi-Exit Asymetric Boosting
load viola_24x24
Xpos = X(: , : , find(y == 1));
load jensen_24x24
indp = find(y == 1);
indn = find(y ==-1);
Xpos = cat(3 , Xpos , X(: , : , indp));
negatives_path = fullfile(pwd , 'Negatives');
options.Npos = 5000;
options.Nneg = 5000;
options.alpha0 = 0.8;
options.beta0 = 0.01;
options.maxweak = 200;
options.maxnbexit = 5;
options.maxK = 5;
options.typefeat = 0;
options.algoboost = 2;
options.probaflipIpos = 0.5;
options.probaswitchIneg = 0.005;
options.scalemin = 1;
options.scalemax = 5;
Pd_th = (1 - options.beta0)^options.maxnbexit
Pfa_th = options.alpha0^options.maxnbexit
[model , exit_nodes] = multi_exit_asymetric_boosting(Xpos , negatives_path , options);
Pd_exp = prod(1-model.stat(: , 3)/options.Npos)
Pfa_exp = prod(model.stat(: , 4)/options.Nneg)
T = size(model.param , 2);
nb_exits = size(exit_nodes , 2);
fx_exit = eval_haar(X , model , exit_nodes);
[tpp_exit , fpp_exit , th_exit] = basicroc(y , fx_exit);
Error_exit = (1-tpp_exit(2:end-1)) + fpp_exit(2:end-1);
[mini_exit , index_exit] = min(Error_exit);
th_exit_opt = th_exit(index_exit);
yest_exit = sign(fx_exit - th_exit_opt);
tp_exit = sum(yest_exit(indp) == y(indp))/length(indp)
fp_exit = 1 - sum(yest_exit(indn) == y(indn))/length(indn)
perf_exit = sum(yest_exit == y)/length(y)
fx_full = eval_haar(X , model);
[tpp_full , fpp_full , th_full] = basicroc(y , fx_full);
Error_full = (1-tpp_full(2:end-1)) + fpp_full(2:end-1);
[mini_full , index_full] = min(Error_full);
th_full_opt = th_full(index_full);
yest_full = sign(fx_full - th_full_opt);
tp_full = sum(yest_full(indp) == y(indp))/length(indp)
fp_full = 1 - sum(yest_full(indn) == y(indn))/length(indn)
perf_full = sum(yest_full == y)/length(y)
figure
plot(1:length(y) , fx_exit , 1:length(y) , fx_full , 'r')
title(sprintf('Output of the strong classifier for test data with T = %d, n_{exit} = %d' , T , nb_exits))
legend('Full' , 'Multi-exit' , 'Location' , 'NorthEast');
figure
plot(fpp_full , tpp_full , fpp_exit , tpp_exit , 'r' , 'linewidth' , 2)
axis([-0.02 , 1.02 , -0.02 , 1.02])
title(sprintf('ROC for Multi-Exit Asymetric FastAdaBoosting with T = %d, n_{exit} = %d' , T , nb_exits))
legend('Full' , 'Multi-exit' , 'Location' , 'SouthEast');
figure
plot(th_full , Error_full , th_full_opt , mini_full , 'k+' , th_exit , Error_exit , 'r' , th_exit_opt , mini_exit , 'k+', 'linewidth' , 2)
xlabel('\lambda')
ylabel('Error')
model.postprocessing = 1;
scalingbox = [1.5 , 1.3 , 2];
I = (rgb2gray(imread('class57.jpg')));
tic,[D , stat] = detector_haar(I , model , scalingbox , exit_nodes);,toc
min_detect = 1;
figure
imagesc(I)
colormap(gray)
hold on
plot_rectangle(D(: , find(D(4 , :)>=min_detect)));
hold off
Pd_th =
0.9510
Pfa_th =
0.3277
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\rgb2gray.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\imresize.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\eval_haar.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\fast_haar_ada_weaklearner.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
alpham =
0.8824
betam =
0.0022
alpham =
0.8824
betam =
0.0022
alpham =
0.8824
betam =
0.0022
alpham =
0.8824
betam =
0.0022
alpham =
0.5778
betam =
0.0124
alpham =
0.6944
betam =
0.0038
alpham =
0.6888
betam =
0.0076
alpham =
0.8578
betam =
1.0000e-003
alpham =
0.8012
betam =
0.0014
alpham =
0.7796
betam =
0.0012
alpham =
0.8798
betam =
0.0030
alpham =
0.8946
betam =
4.0000e-004
alpham =
0.7690
betam =
0.0026
alpham =
0.8112
betam =
0.0024
alpham =
0.9146
betam =
8.0000e-004
alpham =
0.8686
betam =
6.0000e-004
alpham =
0.7430
betam =
0.0020
Pd_exp =
0.9829
Pfa_exp =
0.2131
tp_exit =
0.9356
fp_exit =
0.0778
perf_exit =
0.9245
tp_full =
0.9528
fp_full =
0.0348
perf_full =
0.9631
Warning: Calling MEX-file 'C:\utilisateurs\SeBy\matlab\fdtool\detector_haar.dll'.
MEX-files with .dll extensions will not execute in a future version of MATLAB.
Elapsed time is 0.330574 seconds.