Image Turn Grayscale

Source: Internet
Author: User

Myyuanlaipic = Imread (' e:/image/matlab/darkmouse.jpg ');% read images in RGB format
Myfirstgraypic = Rgb2gray (myyuanlaipic);% conversion of RGB to grayscale images with existing functions

[Rows, cols, colors] = size (myyuanlaipic),% gets the parameters of the matrix of the original image
Midgraypic = zeros (rows, cols);% uses the resulting parameters to create a full-zero matrix, which is used to store grayscale images produced by the following methods
Midgraypic = Uint8 (midgraypic);% converts the entire 0 matrix created into uint8 format, since the image is double when created with the above statement

For i = 1:rows
for j = 1:cols
sum = 0;
For k = 1:colors
sum = sum + myyuanlaipic (i, J, K)/3;% key formula for conversion, sum is not more than 255 each time because of the number behind
Midgraypic (i, j) = sum;
Imwrite (midgraypic, ' e:/image/matlab/darkmousegray.png ', ' png ');

% display the original RGB image
Figure (1);
Imshow (Myyuanlaipic);

% shows grayscale images that have been calculated by the system function
Figure (2);
Imshow (Myfirstgraypic);

% display grayscale images after conversion
Figure (3);
Imshow (Midgraypic);

Of course, you can also use RGB to YUV image, so you can get the y image, in order to obtain a grayscale image

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: 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.