imagescX.m 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. function [ cdata ] = imagescX ( img, cmap, clim )
  2. % imagesc_fixed Plot image independent of colormap used in figure
  3. % Usage:
  4. % imagesc_fixed ( img, cmap, clim )
  5. % INPUT:
  6. % img = 2D matrix of image
  7. % cmap = colormap, defaults to 'jet'
  8. % OUTPUT:
  9. % [] = plot the image to current axes
  10. % cdata = RGB matrix
  11. %
  12. % Development status: Partially functional
  13. % Copyleft (c) Xiaohu Mo
  14. % Version: 0.7
  15. if nargin < 2
  16. cmap = jet(256);
  17. end
  18. img = squeeze(img);
  19. if ndims(img) > 2
  20. cdata = [];
  21. return;
  22. end
  23. if isinteger(img)
  24. img = single(img);
  25. end
  26. cmap_len = size(cmap, 1);
  27. if nargin < 3
  28. cmin = min(img(:));
  29. cmax = max(img(:));
  30. else
  31. cmin = clim(1);
  32. cmax = clim(2);
  33. end
  34. % calculate color
  35. % TODO cmap_len - 1 or not? See doc caxis
  36. img_indexed = fix((img-cmin)/(cmax-cmin)*(cmap_len-1)) + 1;
  37. img_indexed(img_indexed < 1) = 1;
  38. img_indexed(img_indexed > cmap_len) = cmap_len;
  39. img_indexed(isnan(img_indexed)) = 1; % TODO when cmax == cmin
  40. cdata = zeros(size(img,1), size(img,2), 3);
  41. cdata(:,:,1) = reshape(cmap(img_indexed, 1), size(img));
  42. cdata(:,:,2) = reshape(cmap(img_indexed, 2), size(img));
  43. cdata(:,:,3) = reshape(cmap(img_indexed, 3), size(img));
  44. if nargout == 0
  45. image('CData', cdata);
  46. end