圖像處理(一)
彩色圖片轉灰度圖片
三種實現方式
-
最大值法
\(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\times im(i,j,1) + 0.5870\times im(i,j,2) + 0.1140\times 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 \n",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也就是加權平均的方法,對光線明暗的處理是最好的。