scales image data to colormap, returning colormap indices FORMAT [img, badvals]=pr_scaletocmap(inpimg,mn,mx,cmap,lrn) Inputs inpimg - matrix containing image to scale mn - image value that maps to first value of colormap mx - image value that maps to last value of colormap cmap - 3xN colormap lrn - 1x3 vector, giving colormap indices that should fill: - lrn(1) (L=Left) - values less than mn - lrn(2) (R=Right) - values greater than mx - lrn(3) (N=NaN) - NaN values If lrn value is 0, then colormap values are set to 1, and indices to these values are returned in badvals (below) Output img - inpimg scaled between 1 and (size(cmap, 1)) badvals - indices into inpimg containing values out of range, as specified by lrn vector above $Id: pr_scaletocmap.m,v 1.1 2005/04/20 15:05:00 matthewbrett Exp $
0001 function [img, badvals]=pr_scaletocmap(inpimg,mn,mx,cmap,lrn) 0002 % scales image data to colormap, returning colormap indices 0003 % FORMAT [img, badvals]=pr_scaletocmap(inpimg,mn,mx,cmap,lrn) 0004 % 0005 % Inputs 0006 % inpimg - matrix containing image to scale 0007 % mn - image value that maps to first value of colormap 0008 % mx - image value that maps to last value of colormap 0009 % cmap - 3xN colormap 0010 % lrn - 1x3 vector, giving colormap indices that should fill: 0011 % - lrn(1) (L=Left) - values less than mn 0012 % - lrn(2) (R=Right) - values greater than mx 0013 % - lrn(3) (N=NaN) - NaN values 0014 % If lrn value is 0, then colormap values are set to 1, and 0015 % indices to these values are returned in badvals (below) 0016 % 0017 % Output 0018 % img - inpimg scaled between 1 and (size(cmap, 1)) 0019 % badvals - indices into inpimg containing values out of range, as 0020 % specified by lrn vector above 0021 % 0022 % $Id: pr_scaletocmap.m,v 1.1 2005/04/20 15:05:00 matthewbrett Exp $ 0023 0024 if nargin < 4 0025 error('Need inpimg, mn, mx, and cmap'); 0026 end 0027 0028 cml = size(cmap,1); 0029 0030 if nargin < 5 0031 lrn = [1 cml 0]; 0032 end 0033 0034 img = (inpimg-mn)/(mx-mn); % img normalized to mn=0,mx=1 0035 if cml==1 % values between 0 and 1 -> 1 0036 img(img>=0 & img<=1)=1; 0037 else 0038 img = img*(cml-1)+1; 0039 end 0040 outvals = {img<1, img>cml, isnan(img)}; 0041 img= round(img); 0042 badvals = zeros(size(img)); 0043 for i = 1:length(lrn) 0044 if lrn(i) 0045 img(outvals{i}) = lrn(i); 0046 else 0047 badvals = badvals | outvals{i}; 0048 img(outvals{i}) = 1; 0049 end 0050 end 0051 return