caffe_prepare_image.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function im_data = caffe_prepare_image( i_img, f_mean )
  2. % function crops_data = caffe_prepare_image( i_img, f_mean )
  3. %
  4. % BRIEF:
  5. % Bring Image into caffe-format for passing it through a deep neural
  6. % network. Assumes f_mean to be of the size of the network input
  7. % Based on prepare_image in classification_demo.m
  8. % Requires Caffe version from 17-07-2015 (hash: 6d92d8fcfe0eea9495ffbc)
  9. %
  10. %
  11. %% start processing of input image
  12. % Convert an image returned by Matlab's imread to im_data in caffe's data
  13. % format: W x H x C with BGR channels
  14. % produce correct ordering of three-channel image
  15. if (size(i_img,3)==1)
  16. % adapt gray scale images
  17. im_data = repmat(i_img,1,1,3);
  18. else
  19. % permute channels from RGB to BGR
  20. im_data = i_img(:, :, [3, 2, 1]);
  21. end
  22. % flip width and height
  23. im_data = permute(im_data, [2, 1, 3]);
  24. % make sure it's single type
  25. im_data = single(im_data); % convert from uint8 to single
  26. % resize image to fit the network's input
  27. i_sizeMean = size( f_mean );
  28. im_data = imresize(im_data, [i_sizeMean(1) i_sizeMean(2)], 'bilinear'); % resize im_data
  29. % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
  30. % is already in W x H x C with BGR channels
  31. % subtract mean_data (already in W x H x C, BGR)
  32. im_data = im_data - f_mean;
  33. end