warpBlocksToStandardSize.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function warped = warpBlocksToStandardSize( model, pos, fh_featureExtractor )
  2. % function warped = warpBlocksToStandardSize( model, pos, fh_featureExtractor )
  3. %
  4. % BRIEF:
  5. % Warp positive examples to fit model dimensions.
  6. % Used for training root filters from positive bounding boxes.
  7. %
  8. % author: Alexander Freytag
  9. % date: 13-03-2014 ( last updated)
  10. if ( nargin < 3 )
  11. fh_featureExtractor = [];
  12. end
  13. i_modelSize = size(model.w);
  14. i_modelSize = i_modelSize(1:2);
  15. numpos = length(pos);
  16. b_leaveBoundary = getFieldWithDefault ( fh_featureExtractor, 'b_leaveBoundary', false );
  17. if ( b_leaveBoundary )
  18. cropsize = (i_modelSize+2) .* model.i_binSize;
  19. %only needed in this case
  20. pixels = double(i_modelSize * model.i_binSize);
  21. heights = double([pos(:).y2]' - [pos(:).y1]' + 1);
  22. widths = double([pos(:).x2]' - [pos(:).x1]' + 1);
  23. else
  24. cropsize = (i_modelSize) .* model.i_binSize;
  25. end
  26. warped = [];
  27. lastreadimg='';
  28. for i = 1:numpos
  29. % fprintf('%s: warp: %d/%d\n', name, i, numpos);
  30. if(~strcmp(pos(i).im, lastreadimg))
  31. im = readImage(pos(i).im);
  32. lastreadimg = pos(i).im;
  33. end
  34. if ( b_leaveBoundary )
  35. padx = model.i_binSize * widths(i) / pixels(2);
  36. pady = model.i_binSize * heights(i) / pixels(1);
  37. else
  38. padx=0;
  39. pady=0;
  40. end
  41. x1 = round(double(pos(i).x1)-padx);
  42. x2 = round(double(pos(i).x2)+padx);
  43. y1 = round(double(pos(i).y1)-pady);
  44. y2 = round(double(pos(i).y2)+pady);
  45. window = subarray(im, y1, y2, x1, x2, 1);%note: 0 as last option is currently not supported
  46. warped{end+1} = imresize(window, cropsize, 'bilinear');%, 'Antialiasing', false);
  47. end
  48. if numpos == 1,
  49. assert(~isempty(warped));
  50. end
  51. end
  52. function B = subarray(A, i1, i2, j1, j2, pad)
  53. % B = subarray(A, i1, i2, j1, j2, pad)
  54. % Extract subarray from array
  55. % pad with boundary values if pad = 1
  56. % pad with zeros if pad = 0
  57. dim = size(A);
  58. %i1
  59. %i2
  60. is = i1:i2;
  61. js = j1:j2;
  62. if pad
  63. is = max(is,1);
  64. js = max(js,1);
  65. is = min(is,dim(1));
  66. js = min(js,dim(2));
  67. B = A(is,js,:);
  68. else
  69. % todo
  70. end
  71. end