prepareNaturalImages.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. function prepareNaturalImages()
  2. %PREPARENATURALIMAGES
  3. % Generates a folder with .raw files withing the path that contains the
  4. % images
  5. %--------------------------------------------------------------------------
  6. options.sqside = 512; % change this number for images of different size
  7. options.imgmean = 0.5;
  8. options.imgstd = 0.2;
  9. %--------------------------------------------------------------------------
  10. %select the images
  11. fstr={'*.jpg;*.tif;*.imc','All allowed image files';...
  12. '*.jpg', 'Berkeley image files'; ...
  13. '*.tif', 'McGill image files';...
  14. '*.imc', 'vanHateren image files'};
  15. [filenames, pathname]= uigetfile(fstr,'Select the images you want to convert',...
  16. 'MultiSelect', 'on');
  17. if isequal(filenames,0); error('No images selected! Program is terminating...');
  18. else, disp('Images selected, proceeding to loading...'); end
  19. filenames=cellstr(filenames);
  20. %--------------------------------------------------------------------------
  21. %create saving folder
  22. rawFolderName = fullfile(pathname, 'rawFolder');
  23. if ~exist(rawFolderName,'dir'); mkdir(rawFolderName); end
  24. %--------------------------------------------------------------------------
  25. for ii=1:numel(filenames)
  26. disp(['Loading and processing image ' filenames{ii} '...']);tic;
  27. imMat = getProperGrayImg(fullfile(pathname, filenames{ii}), options.sqside);
  28. valMat = double(imMat)/255;
  29. %--------------------------------------------------------------------------
  30. %Scaling and shifting
  31. reMat = options.imgmean + (valMat-mean(valMat(:)))*options.imgstd/std(valMat(:));
  32. saveMat = imcast(reMat,'uint8');
  33. %save original
  34. name_raw = fullfile(rawFolderName, [strtok([ filenames{ii}], '.') '.raw']);
  35. fid = fopen(name_raw,'w+');
  36. fwrite(fid, saveMat, 'uint8');
  37. fclose(fid);
  38. %--------------------------------------------------------------------------
  39. end
  40. end
  41. %--------------------------------------------------------------------------
  42. function imgMat=getProperGrayImg(imgpath, tsize)
  43. [~,~,ext] = fileparts(imgpath);
  44. switch ext
  45. case '.imc'; imtype='vanHateren';
  46. case '.jpg'; imtype='Berkeley';
  47. case '.tif'; imtype='McGill';
  48. end
  49. switch imtype
  50. case 'vanHateren'
  51. w = 1536; h = 1024;
  52. f1 = fopen(imgpath, 'rb', 'ieee-be');
  53. imRaw = fread(f1, [w, h], 'uint16');
  54. fclose(f1); clear f1;
  55. imRaw = imRaw'/4095;
  56. imRaw(imRaw>1) = 1;
  57. imRaw = imcast(imRaw,'uint8');
  58. ih = size(imRaw,1); iw = size(imRaw,2); ifin = 1000;
  59. otherwise
  60. imRaw = imread(imgpath);
  61. ih = size(imRaw,1);
  62. iw = size(imRaw,2);
  63. ifin = min([ih iw]);
  64. end
  65. imRawClipped = imRaw(round((ih-ifin)/2+1:(ih+ifin)/2),...
  66. round((iw-ifin)/2+1:(iw+ifin)/2), :);
  67. imRawResized = uint8(imageresize(imRawClipped, tsize/ifin));
  68. imgMat = imRawResized(1:tsize, 1:tsize,:);
  69. if ndims(imgMat)>2;
  70. imgMat=rgb2gray(imgMat);
  71. end
  72. end