g_rgb2gray.m 2.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function I = g_rgb2gray(X)
%G_RGB2GRAY Convert RGB image or colormap to grayscale.
%   G_RGB2GRAY converts RGB images to grayscale by eliminating the
%   hue and saturation information while retaining the
%   luminance.
%
%   If RGB2GRAY exists in the Matlab path, it overrides this function and
%   uses the one from the toolbox instead.
%
%   I = G_RGB2GRAY(RGB) converts the truecolor image RGB to the
%   grayscale intensity image I.
%
%   NEWMAP = G_RGB2GRAY(MAP) returns a grayscale colormap
%   equivalent to MAP.
%
%   Class Support
%   -------------  
%   If the input is an RGB image, it can be uint8, uint16, double, or
%   single. The output image I has the same class as the input image. If the
%   input is a colormap, the input and output colormaps are both of class
%   double.
%
%   Example
%   -------
%   I = imread('board.tif');
%   J = rgb2gray(I);
%   figure, imshow(I), figure, imshow(J);
%
%   [X,map] = imread('trees.tif');
%   gmap = rgb2gray(map);
%   figure, imshow(X,map), figure, imshow(X,gmap);
%
%   See also IND2GRAY, NTSC2RGB, RGB2IND, RGB2NTSC, MAT2GRAY.

%   Original file: Copyright 1992-2013 The MathWorks, Inc. 
%   And some changes by me.

if exist('rgb2gray','file')
    I = rgb2gray(X);
else
X        = parse_inputs(X);
origSize = size(X);

% Determine if input includes a 3-D array 
threeD = (ndims(X)==3);

% Calculate transformation matrix
T    = inv([1.0 0.956 0.621; 1.0 -0.272 -0.647; 1.0 -1.106 1.703]);
coef = T(1,:);

if threeD
  %RGB
  
  % Do transformation
  if isa(X, 'double') || isa(X, 'single')

    % Shape input matrix so that it is a n x 3 array and initialize output matrix  
    X          = reshape(X(:),origSize(1)*origSize(2),3);
    sizeOutput = [origSize(1), origSize(2)];
    I = X * coef';
    I = min(max(I,0),1);

    %Make sure that the output matrix has the right size
    I = reshape(I,sizeOutput);
    
  elseif isa(X,'uint8')
    % Transforms to double to do the transformations as some functions are
    % not available without the Image Processing toolbox.
    I = uint8(g_rgb2gray(double(X)/255)*255);
  elseif isa(X,'uint16')
    I = uint16(g_rgb2gray(double(X)/255)*255);
  end

else
  I = X * coef';
  I = min(max(I,0),1);
  I = [I,I,I];
end

end
%%%
%Parse Inputs
%%%
function X = parse_inputs(X)


if ismatrix(X)
  if (size(X,2) ~=3 || size(X,1) < 1)
    error(' images:rgb2gray:invalidSizeForColormap')
  end
  if ~isa(X,'double')
    error(' images:rgb2gray:notAValidColormap')
  end
elseif (ndims(X)==3)
    if ((size(X,3) ~= 3))
      error(' images:rgb2gray:invalidInputSizeRGB')
    end
else
  error(' images:rgb2gray:invalidInputSize')
end
  

%no logical arrays
if islogical(X)
  error(' images:rgb2gray:invalidType')
end