Convert RGB image to grayscale
im2gray function converts RGB images to grayscale by eliminating
the hue and saturation information while retaining the luminance.
Convert Truecolor (RGB) Image to Grayscale Image
Read a truecolor (RGB) image into the workspace from a file and display it.
RGB = imread('example.tif'); imshow(RGB)
Convert the RGB image into a grayscale image.
I = im2gray(RGB);
Display the converted grayscale image.
RGB — Truecolor image
m-by-n-by-3 numeric array | m-by-n numeric array
Truecolor image, specified as an m-by-n-by-3
im2gray also accepts
m-by-n numeric arrays (grayscale images) and
returns them unmodified.
If you have Parallel Computing Toolbox™ installed,
RGB can also be a
I — Grayscale image
m-by-n numeric array
Grayscale image, returned as an m-by-n numeric
array. If the input to
im2gray is a grayscale image, the output image
I is the same as the input image.
If you have Parallel Computing Toolbox installed, then
I can also be a gpuArray.
im2grayfunction is identical to
rgb2grayexcept that it can accept grayscale images as inputs, returning them unmodified. The
rgb2grayfunction returns an error if the input image is a grayscale image. If you use the
im2grayfunction, code like this loop is no longer necessary.
if ndims(I) == 3 I = rgb2gray(I); end
im2grayfunction does not accept colormaps as an input. To convert a colormap to grayscale, use the
im2gray function converts RGB values to grayscale values by forming
a weighted sum of the R, G, and B
0.2989 * R + 0.5870 * G + 0.1140 * B
These are the same weights used by the
rgb2ntsc (Image Processing Toolbox) function to compute the Y component.
The coefficients used to calculate grayscale values in the
function are identical to those used to calculate luminance (E'y) in Rec.ITU-R BT.601-7 after
rounding to three decimal places.
Rec.ITU-R BT.601-7 calculates E'y using the following formula:
0.299 * R + 0.587 * G + 0.114 * B
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
When generating code, if you choose the generic
MATLAB Host Computer target platform,
generates code that uses a precompiled, platform-specific shared library. Use of a shared
library preserves performance optimizations but limits the target platforms for which code
can be generated.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Introduced in R2020b