The principle of converting RGB image to grayscale image

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.