Convert matrix to grayscale image
converts the matrix
I = mat2gray(
A to a grayscale image
I that contains values in the range 0 (black) to 1 (white).
amax are the values in
A that correspond to 0 and 1 in
Values less than
amin are clipped to 0, and values greater than
amax are clipped to 1.
Convert a Matrix into an Image
Read an image and display it.
I = imread('rice.png'); figure imshow(I)
Perform an operation that returns a numeric matrix. This operation looks for edges.
J = filter2(fspecial('sobel'),I); min_matrix = min(J(:))
min_matrix = -779
max_matrix = max(J(:))
max_matrix = 560
Note that the matrix has data type
double with values outside of the range [0,1], including negative values.
Display the result of the operation. Because the data range of the matrix is outside the default display range of
imshow, every pixel with a positive value displays as white, and every pixel with a negative or zero value displays as black. It is challenging to see the edges of the grains of rice.
Convert the matrix into an image. Display the maximum and minimum values of the image.
K = mat2gray(J); min_image = min(K(:))
min_image = 0
max_image = max(K(:))
max_image = 1
Note that values are still data type
double, but that all values are in the range [0, 1].
Display the result of the conversion. Pixels show a range of grayscale colors, which makes the location of the edges more apparent.
A — Input image
Input image, specified as a numeric matrix.
[amin amax] — Input black and white values
2-element numeric vector
I — Output intensity image
Output intensity image, returned as a numeric matrix with values in the range [0, 1].
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
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 Image Processing on a GPU.
Version HistoryIntroduced before R2006a
R2022a: Generate CUDA code using GPU Coder
mat2gray now supports the generation of
optimized CUDA® code (requires GPU Coder™).
R2021b: Support for thread-based environments
mat2gray now supports thread-based