[数字图像处理](一)彩色图像转灰度图像的三种方式与效果分析

图像处理(一)

彩色图片转灰度图片


三种实现方式

  • 最大值法

    (imMax = max( im(i,j,1),im(i,j,2),im(i,j,3) ))

  • 平均法

    (imEva = frac{im(i,j,1)}{3} + frac{im(i,j,2)}{3} + frac{im(i,j,3)}{3})

  • 加权平均值法

    (imKeyEva = 0.2989 imes im(i,j,1) + 0.5870 imes im(i,j,2) + 0.1140 imes im(i,j,3))

matlba实现

clc;
close all;
clear all;

%   相对路径读入图片(和代码在同一文件夹下)
im = imread('p2.jpg');
%---查看图片,检测是否成功读入
%   对显示的图片进行排版
subplot(2,3,4);
imshow(im);
%   对图片进行命名
title('原图');
[col,row,color] = size(im);%col为图片的行数,row为图片的列数,color对于彩色图片一般为3,每层对应RGB
%利用matlab自带的函数进行 rgb_to_gray;
im_matlab = rgb2gray(im);
subplot(2,3,1);
imshow(im_matlab);
title('matlab自带rgb2gray');
%--------------------------------------------------------
%---用最大值法
%   创建一个全为1的矩阵,长宽等同于原图的
im_max = ones(col,row);

for i = 1:1:col
    for j = 1:1:row
        im_max(i,j) = max( im(i,j,:)  );
    end
end
%   将矩阵变为8byte无符号整型变量(不然无法显示图片)
%   !!!最好在计算操作结束后再变化,不然会有精度问题!!!!!
im_max = uint8(im_max);
subplot(2,3,2);
imshow(im_max);
title('最大值法');
%--------------------------------------------------------
%    平均值法 
im_eva = ones(col,row);

for i = 1:1:col
    for j = 1:1:row
        im_eva(i,j) = im(i,j,1)/3 + im(i,j,2)/3 + im(i,j,3)/3 ;
        %   两种的结果其实一样,但是如果先转换为uint8就会出现精度问题
        %sum1 = im(i,j,1)/3 + im(i,j,2)/3 + im(i,j,3)/3
        %sum2 = ( im(i,j,1) + im(i,j,2)+ im(i,j,3) )/3;
        %fprintf( " %.4f  %.4f 
",sum1 ,sum2 ) ;
    end
end
im_eva = uint8(im_max);
subplot(2,3,3);
imshow(im_eva);
title('平均值法');
%--------------------------------------------------------
%    加权平均法(rgb2gray所使用的权值)
im_keyeva = ones(col,row);
%   加权算法先转换为uint8计算效果更好
im_keyeva = uint8(im_max);
for i = 1:1:col
    for j = 1:1:row
        im_keyeva(i,j) = 0.2989*im(i,j,1) + 0.5870*im(i,j,2) + 0.1140*im(i,j,3) ;
    end
end

subplot(2,3,5);
imshow(im_keyeva);
title('加权平均法');


附matlab——rgb2gray源码


function I = rgb2gray(X)
%RGB2GRAY Convert RGB image or colormap to grayscale.
%   RGB2GRAY converts RGB images to grayscale by eliminating the
%   hue and saturation information while retaining the
%   luminance.
%
%   I = RGB2GRAY(RGB) converts the truecolor image RGB to the
%   grayscale intensity image I.
%
%   NEWMAP = RGB2GRAY(MAP) returns a grayscale colormap
%   equivalent to MAP.
%
%   Class Support
%   -------------  
%   If the input is an RGB image, it can be of any numeric type. 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.
%
%   Notes
%   -----
%   RGB2GRAY converts RGB values to grayscale values by forming a weighted 
%   sum of the R, G, and B components:
%
%   0.2989 * R + 0.5870 * G + 0.1140 * B
%
%   The coefficients used to calculate grayscale values in RGB2GRAY are 
%   identical to those used to calculate luminance (E'y) in 
%   Rec.ITU-R BT.601-7 after rounding to 3 decimal places.
%
%   Rec.ITU-R BT.601-7 calculates E'y using the following formula: 
%
%   0.299 * R + 0.587 * G + 0.114 * B 
%
%   Example
%   -------
%   I = imread('example.tif');
%
%   J = rgb2gray(I);
%   figure, imshow(I), figure, imshow(J);
%
%   indImage = load('clown');
%   gmap = rgb2gray(indImage.map);
%   figure, imshow(indImage.X,indImage.map), figure, imshow(indImage.X,gmap);
%
%   See also RGB2IND, RGB2LIGHTNESS.

%   Copyright 1992-2020 The MathWorks, Inc.

narginchk(1,1);

isRGB = parse_inputs(X);
if isRGB
    I = matlab.images.internal.rgb2gray(X);
else
    % Color map
    % 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,:);
    I = X * coef';
    I = min(max(I,0),1);
    I = repmat(I, [1 3]);
end

%--------------------------------------------------------------------------
function is3D = parse_inputs(X)

is3D = (ndims(X) == 3);
if is3D
    % RGB
    if (size(X,3) ~= 3)
        error(message('MATLAB:images:rgb2gray:invalidInputSizeRGB'))
    end
    % RGB can be single, double, int8, uint8,
    % int16, uint16, int32, uint32, int64 or uint64
    validateattributes(X, {'numeric'}, {}, mfilename, 'RGB');
elseif ismatrix(X)
    % MAP
    if (size(X,2) ~= 3 || size(X,1) < 1)
        error(message('MATLAB:images:rgb2gray:invalidSizeForColormap'))
    end
    % MAP must be double
    if ~isa(X,'double')
        error(message('MATLAB:images:rgb2gray:notAValidColormap'))
    end
else
    error(message('MATLAB:images:rgb2gray:invalidInputSize'))
end



总结


通过上面的代码结合实际的测试,果然,matlab自带的rgb2gray也就是加权平均的方法,对光线明暗的处理是最好的。

原文地址:https://www.cnblogs.com/hoppz/p/14634795.html