demoWhitenedHOG.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. % author: Alexander Freytag
  2. % date : 27-02-2014 (dd-mm-yyyy)
  3. %% Training
  4. % We will train a model from a single instance.
  5. % So let's create a tiny list of positive examples
  6. pos(1).im = 'train.jpg';
  7. pos(1).x1 = 70;
  8. pos(1).y1 = 202;
  9. pos(1).x2 = 255;
  10. pos(1).y2 = 500;
  11. %read image ...
  12. im = readImage(pos(1).im);
  13. figTrain = figure;
  14. set ( figTrain, 'name', 'Training Image');
  15. % ... and show initial bounding box
  16. showboxes(im ,[pos(1).x1 pos(1).y1 pos(1).x2 pos(1).y2]);
  17. %settings for feature extraction
  18. settings.i_binSize = 8;
  19. settings.interval = 10; % same as on who demo file
  20. settings.order = 20;
  21. % note:
  22. % change the representation as you desire. This repo comes along with HOG
  23. % features as default, however, any diffferent feature type can be plugged
  24. % in as well given the proper wrapper funtion.
  25. % Examples can be found in our repository about patch discovery
  26. settings.fh_featureExtractor = ...
  27. struct('name','Compute HOG features using WHO code', ...
  28. 'mfunction',@computeHOGs_WHOorig, ...
  29. 'b_leaveBoundary' , true );
  30. % try locate previously trained bg-struct
  31. try
  32. fileToBG = fullfile(pwd, 'data/bg11.mat');
  33. load( fileToBG );
  34. % compatibility to older versions
  35. if ( isfield(bg,'sbin') && ~isfield(bg, 'i_binSize') );
  36. bg.i_binSize = bg.sbin;
  37. end
  38. catch
  39. % if not possible, leave bg empty and compute it from scratch lateron
  40. bg=[];
  41. end
  42. % no negative examples, use 'universal' negative model
  43. neg = [];
  44. model = learn_dataset( pos, neg, bg, settings );
  45. %show learned model, i.e., visualized HOG feature for positive and negative weights
  46. b_closeImg = false;
  47. showWeightVectorHOG( model.w, b_closeImg )
  48. %% Testing
  49. test(1).im='test.jpg';
  50. %perform detection
  51. boxes=test_dataset( test, model, settings );
  52. %convert output from 1x1-cell to double array
  53. boxes = boxes{1};
  54. %only take highest response
  55. %[~,bestIdx] = max ( boxes(:,5) );
  56. %boxes = boxes(bestIdx,:);
  57. %show detections
  58. im = readImage(test(1).im);
  59. figTest = figure;
  60. set ( figTest, 'name', 'Test Image');
  61. showboxes(im, boxes);
  62. % that's it :)