Reference: http://blog.csdn.net/cool1949/article/details/6649429 RGB image to grayscale image
Today in helping Li Na classmate read the code, suddenly thought of to scrutiny RGB image conversion to grayscale image principle. So began the story of this blog. As a little white, the elaboration may not be very specific and comprehensive. I hope that we could make progress together.
In general, the RGB image is 3 channels, that is, a 3-dimensional matrix, and gray-scale map, we all know that there is only one channel, then how to convert a 3-channel thing into a 1-channel thing.
In fact, there is a conversion formula, in a nutshell, is to RGB3 the component of the channel to a certain proportion of the calculation to grayscale image. As stated in the formula (1), Gray = r*0.299 + g*0.587 + b*0.114 (1). Of course, I have seen several other optimization of the formula, but the practice of their own in the process is not satisfactory. Interested students can try it on their own.
% self-implemented convert from RGB image to grayscale image
function gray = My_rgb2gray (IM)
% Gray = r*0.299 + g*0.587 + b*0.114
[m,n,h] = size (IM); C4/>if h ==1
Gray = im;
End
Gray =zeros (m,n);
For i = 1:m
for j = 1:n
% The accuracy of the first sentence is best
gray (i,j) = 0.299*im (i,j,1) +0.587*im (i,j,2) +0.114*im (i,j,3); c12/>% Gray (i,j) = (IM (i,j,1) *30 + IM (i,j,2) *59+ im (i,j,3) *11+50)/100;
% Gray (i,j) = (IM (i,j,1) *299 + IM (i,j,2) *587 + IM (i,j,3) *114 + +)/;
End
End
Gray = uint8 (gray);
End
The result shown is the following figure