%------------------------------------------------------------% %-------- Matlab Image Intensity Levels converter -----------% %------------------------------------------------------------% % % !!!! TIF version !!!! % % V0.20 2004/12/16 % Changelog: % from V0.11 (2004/12/16)to V0.20: removed extrapolation for dynamic range < 256 % from V0.1 (2004/12/15) to V0.11: added +0.5 for correct rounding % % % % Description: % This Matlab script can be used to convert a 16-bit tif image into a 8bit* % tif or jpg (100% quality) by defining the intensity range equivalent to % 0.."256" (if the dynamic range before is greater than 256 intensity levels, the % levels are lowered linearly otherwise the dynamic range is kept the % same but clipped within 256 intensity levels) % (*: the bit level may be adjusted too if desired - possible with tif images) % % % Input arguments: % 1st: "path" - path string to the folder where the images to be converted % are contained (e.g. 'c:\pivimg\') % 2nd: "Imin" - original level equivalent to 0 % 3rd: "Imax" - original level equivalent to 255 % % Example 1: % to convert the original levels ranged 100 to 500 into 0 to 255 % (linearly interpolated / shifted) with all files in folder 'd:\myimages\' % type: LevelConv('d:\myimages\', 100, 500) % the intensity level 100 will now correspond to 0 and 500 to 255, with all levels % inbetween interpolated linearly % % Example 2: % if the original levels now ranged 100 to 200 % type: LevelConv('d:\myimages\', 100, 200) % the intensity level 100 will now correspond to 0 and 200 to 100, with all % original levels <100 now being zero and the ones >355 now equal to 255; % the reason for this is not to artificially increase the actual dynamic range and % thus introduce additional potential errors % % % basic function description (based on the assumption of a 16 bit tif originating % from a 10 bit camera and shifted by multiplication with 2^6) % note images aquired with a different software than epix might behave differently % 1. the 16-bit tif is read % 2. the 16-bit tif is shifted to 10 bit by multiplication with 2^-6 % 3. the lower level is set to zero by subtracting the lower level (e.g. 100) % 4. the range of 0-255 is achieved by multiplication (linear shifting) function LevelConv(path, Imin, Imax) % function definition % Adjustable parameters imgReadFormat = 'tif'; % format of image to read, choose from: 'tif', 'jpg', ... imgWriteFormat = 'tif'; % format to write the image to; carefull: set quality etc % as well! PicResBit = 16; % picture resolution CamResBit = 10; % camera resolution in bit % adjust Imax (new in V0.20) if ((Imax - Imin) < 255) Imax = Imin + 255; end; % get files from path files = dir ([path, '*.', imgReadFormat]); % process all files in the path for i=1:size(files,1) X = imread([path,files(i).name]); % read a particular image file ImgFName = [path, '8bit', ... files(i).name(1:findstr(files(i).name,imgReadFormat)-1), imgWriteFormat] % image file name to be written display not suppressed to allow viewing progress X = uint8( ( double(X)*2^(CamResBit-PicResBit) - Imin ) ... /(Imax-Imin)*255 + 0.5); % image manipulation as described % note any levels above 255 will be cropped to 255 as unlike other software % Matlab won't rebegin at zero due to bit shifting (makes life easier) imwrite(X,ImgFName,imgWriteFormat); % writing the new image end