No BSD License  

Highlights from
GeoML

image thumbnail
from GeoML by Gabriele Lombardi
A general morphable template tool for image segmentation.

imtype(img, type)
function imgo = imtype(img, type)
% IMTYPE  Chenges the type of an image
%
%  This function allows to set the type of an image. The type is specified
% in the type string that is composed from two characters:
%  - color type:    This character indicates if the image must be a:
%                    - g:   grayscale
%                    - c:   rgb
%                   Other colorspaces can be obtained starting from the rgb
%                   image.
%  - data type:     This character indicates the data type used to memorize
%                   the pixel values. Values are:
%                    - i:   uint8
%                    - I:   uint16
%                    - f:   single
%                    - F:   double
%                   Images of types i and I use values ranging in 0:255 and
%                   0:65535 respectively, images of types f and F uses the
%                   range 0:1.
%
%  Params
%  ------
% IN:
%  img      = The image to modify
%  type     = The image type required
% OUT:
%  imgo     = The modified image
%
%  Pre
%  ---
% -  Type selected must be a string a len 2 with characters described here.
%
%  Post
%  ----
% -  The type of the image is changed as required, color image are also
%   created from greyscale ones.
%
%  SeeAlso
%  -------
% uint8, uint16, single, double
%
%  Examples
%  --------
%   Obtaining a double image in grayscale:
% >> img = imtype(img,'gF');

% Check parameters (required 2)
if nargin~=2 || not(isa(type,'char')) || size(type,2)~=2
    error('Required image and type as parameters!');
end

if type(1)~='c' && type(1)~='g'
    error('Unexpected channel type!');
end

% Check the image
if isa(img,'char')
    % Try to read the image
    try img = imread(img);
    catch error(['Cannot read the image ''' img ''' given as parameter!']); end
end

% Channel conversion
if size(img,3)>1
    if type(1)=='g' imgo=rgb2gray(img); 
    else imgo = img; end
else
    if type(1)=='c' imgo=gray2rgb(img);
    else imgo = img; end
end

% Type conversion
switch type(2)
    case 'i'
        if ~isa(imgo,'uint8')
            % Check if the previous is an integer type
            if isa(imgo,'uint16')
                % Max must be 255
                imgo = imgo/256;
            else
                % Max must be 255
                imgo = imgo*255;
            end
            % Conversion
            imgo = uint8(imgo);
        end
    case 'I'
        if ~isa(imgo,'uint16')
            % Check if the previous is an integer type
            if isa(imgo,'uint8')
                % Max must be 65535
                imgo = uint16(imgo)*256;
            else
                % Max must be 65535
                imgo = uint16(imgo*65535);
            end
        end
    case 'f'
        if ~isa(imgo,'single')
            % Check type
            if isa(imgo,'uint8')
                imgo = single(imgo)/255.0;
            elseif isa(imgo,'uint16')
                imgo = single(imgo)/65535.0;
            else
                imgo = single(imgo);
            end
        end
    case 'F'
        if ~isa(imgo,'double')
            % Check type
            if isa(imgo,'uint8')
                imgo = double(imgo)/255.0;
            elseif isa(imgo,'uint16')
                imgo = double(imgo)/65535.0;
            else
                imgo = double(imgo);
            end
        end
    otherwise
        error('Unexpected data type!');
end

Contact us at files@mathworks.com