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 [2].
% 'dual_no_GT': The dual transform, but DON'T use binarization to get an estimation of text regions [2].
% '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
% [2] R. Farrahi Moghaddam, et.al., "A maximal-information  
% color to gray conversion method for document images: Toward  
% an optimal grayscale representation for document image  
% binarization," 2013, [arXiv preprint http://arxiv.org/abs/1306.6058  
% arXiv:1306.6058, June 2013]
%
% 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
        % scale to full range
        ugray0 = imadjust(ugray0, stretchlim(ugray0), [0 1]);
    case 'dual'
        if (not(exist('my_convert_using_pca_to_dual_plus_file.exe', 'file')))
            temp_zipfile = websave([tempname(), '.zip'], 'http://www.synchromedia.ca/system/files/my_convert_using_pca_to_dual_plus_file.zip');
            unzip(temp_zipfile);
        end
        temp_workfile = [tempname(), '.png'];
        temp_workfileDual = [tempname(), '.png'];
        imwrite(u, [temp_workfile]);
        system(['cmd.exe /c my_convert_using_pca_to_dual_plus_file.exe ', temp_workfile, ' ', temp_workfileDual]);
        ugray0 = mat2gray(imread(temp_workfileDual));
%     case 'dual_no_GT'
%         if (not(exist('my_convert_using_pca_to_dual_plus_file.exe', 'file')))
%             temp_zipfile = websave([tempname(), '.zip'], 'http://www.synchromedia.ca/system/files/my_convert_using_pca_to_dual_plus_file.zip');
%             unzip(temp_zipfile);
%         end
%         temp_workfile = [tempname(), '.png'];
%         temp_workfileDual = [tempname(), '.png'];
%         imwrite(u, [temp_workfile]);
%         system(['cmd.exe /c my_convert_using_pca_to_dual_plus_file.exe ', temp_workfile, ' ', temp_workfileDual]);
%         ugray0 = mat2gray(imread(temp_workfileDual));      
    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