Code covered by the BSD License  

Highlights from
Universal Color to Gray Conversion

image thumbnail

Universal Color to Gray Conversion

by

 

11 May 2010 (Updated )

This function provides some methods to convert a color image to a gray-value image.

universal_color_to_gray_converter(u, method_flag, method_name)
function ugray0 = universal_color_to_gray_converter(u, method_flag, method_name)
% This function provides different methods to convert a color images to a gray-value image.
%
% The implemneted methods are:
% 'normal', 'standard': rgb2gray of matlab; ITU-R Recommendation BT.601.
% 'avg', 'average': the output is the average of all three channels.
% 'minavg', 'min_avg', 'min_average', 'minimum_average': The minimum-average method introduced in [1] for docuemtn image processing.
% 'dual', 'dual_transform': The dual transform with color_reduction [?].
% 'dual_no_GT': The dual transform, but DON'T use binarization to get an estimation of text regions [?].
% 'maxrgb', 'maxRGB': Maximum of Red, Green or Blue of each pixel.
% 'pca', 'PCA': PCA method
%
% April 15th, 2010, By Reza Farrahi Moghaddam, Synchromedia Lab, ETS, Montreal, Canada
% May 15th, 2015: Bug fixed.
%
%
%
% [1] R. Farrahi Moghaddam and M. Cheriet, A multi-scale
% framework for adaptive binarization of degraded document images, Pattern
% Recognition, 43, pp. 2186--2198, 2010, DOI: 10.1016/j.patcog.2009.12.024
%
%
% USAGE:
% ugray0 = universal_color_to_gray_converter(u, method_flag, method_name);
% where
%       u is the input color image.
%       method_flag is 'method'
%       method_name is one of above mentioned methods.
%       ugray0 is the output gray-level image.
% OR
% ugray0 = universal_color_to_gray_converter(u);
% where
%       u is the input color image.
%       ugray0 is the output gray-level image; in this case, the method is
%       'min_avg'; 



%
if (nargin == 1)
    method_name = 'minimum_average';
end

%
switch method_name
    case ''
        method_name = 'normal';    
    case 'standard'
        method_name = 'normal';  
    case 'normal'
        method_name = 'normal';          
    case 'avg'
        method_name = 'average';
    case 'minavg'
        method_name = 'minimum_average';
    case 'min_avg'
        method_name = 'minimum_average';
    case 'min_average'
        method_name = 'minimum_average';
    case 'minimum_average'
        method_name = 'minimum_average';        
    case 'dual_transform'
        method_name = 'dual';      
    case 'dual'
        method_name = 'dual';  
    case 'dual_no_GT'
        method_name = 'dual_no_GT';          
    case 'maxrgb'
        method_name = 'maxRGB';    
    case 'pca'
        method_name = 'PCA';   		
    otherwise 
        fprintf('error\n');
end

%
[xm, ym, zm] = size(u);
if (zm ~= 3)&&(strcmpi('normal', method_name))
    method_name = 'average';
end

%
switch method_name
    case 'normal'
        ugray0 = rgb2gray(u);    
    case 'average'
        if (max(max(max(u))) > 1.5)
            u = double(u) / 255;
        end
        if (zm > 1)
            ugray0 = sum(double(u), 3) / double(zm); % ((double(u(:,:,1))+double(u(:,:,2))+double(u(:,:,3)))/3);
        else%z
            ugray0 = im2double(u);
        end%z
    case 'minimum_average'
        if (max(max(max(u))) > 1.5)
            u = double(u) / 255;
        end
        if (zm > 1)
            ugray0 = ( double(min(u,[],3)) + ...
                (double(u(:,:,1))+double(u(:,:,2))+double(u(:,:,3)))/3)/2;
        else%z
            ugray0 = im2double(u);
        end%z
    % case 'dual'
        % if (max(max(max(u))) > 1.5)
            % u = double(u) / 255;
        % end
        % % rough bin
        % [temp ugray0] = my_convert_using_pca_to_dual(u);
        % %
    % case 'dual_no_GT'
        % if (max(max(max(u))) > 1.5)
            % u = double(u) / 255;
        % end
        % prior.mask_the_border_percentage_vert_top = 0;        
        % prior.mask_the_border_percentage_vert_bottom = 0; 
        % prior.mask_the_border_percentage_hori = 0; 
        % % rough bin
        % [temp ugray0] = my_convert_using_pca_to_dual(u, prior, zeros(size(u)));
        % %        
    case 'maxRGB'
        if (max(max(max(u))) > 1.5)
            u = double(u) / 255;
        end
        if (zm > 1)
            ugray0 = ( double(max(u,[],3)));
        else%z
            ugray0 = im2double(u);
        end%z        
    % case 'PCA'
        % if (max(max(max(u))) > 1.5)
            % u = double(u) / 255;
        % end
        % % rough bin
        % [temp ugray0] = my_convert_using_pca(u);
        % %		
    otherwise
        fprintf('error\n');
end

end







Contact us