Color gray image
Gradation of color image there are three main methods: a maximum value method, average, weighted average method.
Maximum method | The maximum take three color components RGB, i.e., R = G = B = max (R, G, B) |
---|---|
Average method | The average of three RGB color components, i.e., R = G = B = (R + G + B) / 3 |
Weighted average method | The average of three colors of RGB components by weight, i.e., R = G = B = WrR + WgG + WbB, when Wr = 0.587, Wg = 0.299, Wb = 0.114, the better. |
Achieve results
-
Original color image []
-
Maximum grayscale image
-
Average grayscale image (left) , a weighted average gray scale image (right)
Reference Code
Method maximum reference code
im=imread('autumn.tif');
[x,y,z]=size(im);
immax=ones(x,y);
%最大值灰度图像
for i=1:x
for j=1:y
immax(i,j)=max(im(i,j,:));
end
end
im(:,:,1)=immax;
im(:,:,2)=immax;
im(:,:,3)=immax;
figure('name','最大值灰度图像');
imshow(im);
Average, weighted average method reference code
noted before performing image processing to the read image data using the Double () or im2double () type conversion! !
Otherwise, uint8 type of image data of the operation result exceeds 255 will overflow! !
im=imread('autumn.tif');
figure('name','原图像');
imshow(im);
%提取R、G、B三色分量
imR=im2double(im(:,:,1));
imG=im2double(im(:,:,2));
imB=im2double(im(:,:,3));
%平均值灰度图像
%取完平均值之后还要乘以255,将灰度值范围变回[0,255]并取整
imRGB=round((imR+imG+imB)/3*255);
im(:,:,1)=imRGB;
im(:,:,2)=imRGB;
im(:,:,3)=imRGB;
figure('name','灰度图像');
subplot(1,2,1);
imshow(im);
title('平均值灰度图像');
%加权平均值灰度图像
%Wr=0.587,Wg=0.299,Wb=0.114,加权平均后还要乘以255,将灰度值范围变回[0,255]并取整
imRGB2=round((0.587*imR+0.299*imG+0.114*imB)*255);
im(:,:,1)=imRGB2;
im(:,:,2)=imRGB2;
im(:,:,3)=imRGB2;
subplot(1,2,2);
imshow(im);
title('加权平均值灰度图像');