Professional Documents
Culture Documents
The colormap is an m-by-3 matrix of class double. Each row of the colormap matrix specifies the red,
green, and blue (RGB) values for a single color:
color = [R G B]
R, G, and B are real scalars that range from 0 (black) to 1.0 (full intensity).
The pixels in the image are represented by integers, which are pointers (indices) to color values stored in the
colormap. The relationship between the values in the image matrix and the colormap depends on whether the image
matrix is of class double or uint8. If the image matrix is of class double, the value 1 points to the first row in the
colormap, the value 2 points to the second row, and so on. If the image matrix is of class uint8, there is an offset; the
value 0 points to the first row in the colormap, the value 1 points to the second row, and so on. The uint8 convention
is also used in graphics file formats, and enables 8-bit indexed images to support up to 256 colors. In the image
above, the image matrix is of class double, so there is no offset. For example, the value 5 points to the fifth row of the
colormap.
Intensity Images
MATLAB stores an intensity image as a single matrix, with each element of the matrix corresponding to
one image pixel. The matrix can be of class double, in which case it contains values in the range [0,1], or
of class uint8, in which case the data range is [0,255]. The elements in the intensity matrix represent
various intensities, or gray levels, where the intensity 0 represents black and the intensity 1 (or 255)
represents full intensity, or white.
Binary Images
In a binary image, each pixel assumes one of only two discrete values. Essentially, these two values
correspond to on and off. A binary image is stored as a two-dimensional matrix of 0’s (off pixels) and 1’s
(on pixels).
A binary image can be considered a special kind of intensity image, containing only black and white.
Other interpretations are possible, however; you can also think of a binary image as an indexed image
with only two colors.
A binary image can be stored in an array of class double or uint8. However, a uint8 array is preferable,
because it uses far less memory. In the Image Processing Toolbox, any function that returns a binary
image returns it as a uint8 logical array. The toolbox uses the presence of the logical flag to signify that
the data range is [0,1]. (If the logical flag is off, the toolbox assumes the data range is [0,255].)
Like an indexed image, an RGB image represents each pixel color as a set of three values, representing
the red, green, and blue intensities that make up the color. Unlike an indexed image, however, these
intensity values are stored directly in the image array, not indirectly in a colormap.
In MATLAB, the red, green, and blue components of an RGB image reside in a single m-by-n-by-3
array. m and n are the numbers of rows and columns of pixels in the image, and the third dimension
consists of three planes, containing red, green, and blue intensity values. For each pixel in the image, the
red, green, and blue elements combine to create the pixel’s actual color.
For example, to determine the color of the pixel (112,86), look at the RGB triplet stored in (112,86,1:3).
Suppose (112,86,1) contains the value 0.1238, (112,86,2) contains 0.9874, and (112,86,3) contains
0.2543. The color for the pixel at (112,86) is:
An RGB array can be of class double, in which case it contains values in the range [0,1], or of class uint8,
in which case the data range is [0,255]. The figure below shows an RGB image of class double:
Working with Image Data
This section discusses ways of working with the data arrays that represent images, including:
Reading in image data from files, and writing image data out to files
Working with uint8 arrays in MATLAB and the Image Processing Toolbox
You can use the MATLAB imread function to read image data from files. imread can read these graphics
file formats:
BMP
HDF
JPEG
PCX
TIFF
XWD
To write image data from MATLAB to a file, use the imwrite function. imwrite can write the same file
formats that imread reads.
In addition, you can use the imfinfo function to return information about the image data in a file.
See the reference entries for imread, imwrite, and imfinfo for more information about these functions.
For certain operations, it is helpful to convert an image to a different image type. For example, if you
want to filter a color image that is stored as an indexed image, you should first convert it to RGB format.
When you apply the filter to the RGB image, MATLAB filters the intensity values in the image, as is
appropriate. If you attempt to filter the indexed image, MATLAB simply applies the filter to the indices in
the indexed image matrix, and the results may not be meaningful.
The Image Processing Toolbox provides several functions that enable you to convert any image to another
image type. These functions have mnemonic names; for example, ind2gray converts an indexed image to
a grayscale intensity format.
Note that when you convert an image from one format to another, the resulting image may look different
from the original. For example, if you convert a color indexed image to an intensity image, the resulting
image is grayscale, not color.
Function Purpose
Create a binary image from a grayscale intensity image by dithering; create an indexed image
dither
from an RGB image by dithering
gray2ind Create an indexed image from a grayscale intensity image
grayslice Create an indexed image from a grayscale intensity image by thresholding
Create a binary image from an intensity image, indexed image, or RGB image, based on a
im2bw
luminance threshold
ind2gray Create a grayscale intensity image from an indexed image
ind2rgb Create an RGB image from an indexed image
mat2gray Create a grayscale intensity image from data in a matrix, by scaling the data
rgb2gray Create a grayscale intensity image from an RGB image
rgb2ind Create an indexed image from an RGB image
The Image Processing Toolbox represents colors as RGB values, either directly (in an RGB image) or
indirectly (in an indexed image). However, there are other methods for representing colors. For example,
a color can be represented by its hue, saturation, and value components (HSV). Different methods for
representing colors are called color spaces. The toolbox provides a set of routines for converting between
RGB and other color spaces. The image processing functions themselves assume all color data is RGB,
but you can process an image that uses a different color space by first converting it to RGB, and then
converting the processed image back to the original color space.
Coordinate Systems
Locations in an image can be expressed in various coordinate systems, depending on context. This section
discusses the two main coordinate systems used in the Image Processing Toolbox, and the relationship
between them. These systems are:
Pixel Coordinates
Generally, the most convenient method for expressing locations in an image is to use pixel coordinates. In
this coordinate system, the image is treated as a grid of discrete elements, ordered from top to bottom and
left to right. For example:
For pixel coordinates, the first component r (the row) increases downward, while the second
component c (the column) increases to the right. Pixel coordinates are integer values and range between 1
and the length of the row or column.
There is a one-to-one correspondence between pixel coordinates and the coordinates MATLAB uses for
matrix subscripting. This correspondence makes the relationship between an image’s data matrix and the
way the image displays easy to understand. For example, the data for the pixel in the fifth row, second
column is stored in the matrix element (5,2).
Spatial Coordinates
In the pixel coordinate system, a pixel is treated as a discrete unit, uniquely identified by a single
coordinate pair, such as (5,2). From this perspective, a location such as (5.3,2.2) is not meaningful.
At times, however, it is useful to think of a pixel as a square patch, having area. From this perspective, a
location such as (5.3,2.2) is meaningful, and is distinct from (5,2). In this spatial coordinate system,
locations in an image are positions on a plane, and they are described in terms of x and y.
This figure illustrates the spatial coordinate system used for images. Notice that y increases downward:
This spatial coordinate system corresponds quite closely to the pixel coordinate system in many ways. For
example, the spatial coordinates of the center point of any pixel are identical to the pixel coordinates for
that pixel.
There are some important differences, however. In pixel coordinates, the upper-left corner of an image is
(1,1), while in spatial coordinates, this location by default is (0.5,0.5). This difference is due to the pixel
coordinate system being discrete, while the spatial coordinate system is continuous. Also, the upper-left
corner is always (1,1) in pixel coordinates, but you can specify a nondefault origin for the spatial
coordinate system. See "Using a Non default Spatial Coordinate System" on page 1-19 for more
information.
Another potentially confusing difference is largely a matter of convention: the order of the horizontal and
vertical components is reversed in the notation for these two systems. Pixel coordinates are expressed as
(r,c), while spatial coordinates are expressed as (x,y).
gray2ind
Syntax
Description
[X, map] = gray2ind(I,n) converts the grayscale image I to an indexed image X. n specifies the size of the
colormap, gray(n). n must be an integer between 1 and 65536. If n is omitted, it defaults to 64.
[X, map] = gray2ind(BW,n) converts the binary image BW to an indexed image X. n specifies the size of the
colormap, gray(n). If n is omitted, it defaults to 2.
gray2ind scales and then rounds the intensity image to produce an equivalent indexed image.
Class Support
The input image I can be logical, uint8, uint16, int16, single, or double and must be a real and nonsparse. The image I can
have any dimension. The class of the output image X is uint8 if the colormap length is less than or equal to 256; otherwise it
is uint16.
Examples
Convert a grayscale image into an indexed image and then view the result.
I = imread('cameraman.tif');
imshow(X, map);
ind2gray
Syntax
I = ind2gray(X, map)
Description
I = ind2gray(X,map) converts the image X with colormap map to a grayscale image I.ind2gray removes the hue and
saturation information from the input image while retaining the luminance.
X can be of class uint8, uint16, single, or double. map is double. I is of the same class as X.
Examples
load trees
I = ind2gray(X, map);
imshow(X ,map)
figure, imshow(I)
mat2gray
Syntax
Description
I = mat2gray(A, [amin amax]) converts the matrix A to the intensity image I. The returned matrix I contains values in the
range 0.0 (black) to 1.0 (full intensity or white).amin and amax are the values in A that correspond to 0.0 and 1.0 in I. Values
less than amin become 0.0, and values greater than amax become 1.0.
I = mat2gray(A) sets the values of amin and amax to the minimum and maximum values in A.
gpuarrayI = mat2gray(gpuarrayA,___) performs the operation on a GPU. This syntax requires the Parallel Computing
Toolbox™.
Class Support
The input array A can be logical or numeric. The output image I is double.
The input gpuArray gpuarrayA can be logical or numeric. The output gpuArray imagegpuarrayI is double.
Example
Convert a Matrix into an Image
Read an image and, for this example, turn it into a numeric matrix.
I = imread('rice.png');
J = filter2(fspecial('sobel'),I);
Convert the matrix into an image.
K = mat2gray(J);
Display the original image and the result of the conversion.
rgb2gray
Convert RGB image or colormap to grayscale
Syntax
I = rgb2gray(RGB)example
newmap = rgb2gray(map)example
Description
example
I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale intensity image I. The rgb2gray function converts
RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel
Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.
example
Examples
collapse all
Read the sample file, peppers.png, and display the RGB image.
RGB = imread('peppers.png');
imshow(RGB)
Convert the RGB image to a grayscale image and display it.
I = rgb2gray(RGB);
figure
imshow(I)
Convert RGB Colormap to Grayscale Colormap
Read an indexed image with an RGB colormap. Then, convert the colormap to grayscale.
Read the sample file, corn.tif, which is an indexed image with an RGB colormap.
[X,map] = imread('corn.tif');
Display the image.
imshow(X,map)
Convert the RGB colormap to a grayscale colormap and redisplay the image.
newmap = rgb2gray(map);
imshow(X,newmap)
Input Arguments
collapse all
If you have Parallel Computing Toolbox installed, RGB can also be a gpuArray.
If you have Parallel Computing Toolbox installed, map can also be a gpuArray.
Output Arguments
collapse all
If you have Parallel Computing Toolbox installed, then I can also be a gpuArray.
If you have Parallel Computing Toolbox installed, then newmap can also be a gpuArray.
More About
collapse all
Tips
rgb2gray supports the generation of C code using MATLAB Coder™.
®
Algorithms
rgb2gray converts RGB values to grayscale values by forming a weighted sum of theR, G, and B components:
These are the same weights used by the rgb2ntsc function to compute the Ycomponent.
ind2rgb
Syntax
RGB = ind2rgb(X,map)
Description
RGB = ind2rgb(X,map) converts the matrix X and corresponding colormap map to RGB (truecolor) format.
Class Support
label2rgb
Syntax
RGB = label2rgb(L)
RGB = label2rgb(L, map)
RGB = label2rgb(L, map, zerocolor)
RGB = label2rgb(L, map, zerocolor, order)
Description
RGB = label2rgb(L) converts a label matrix, L, such as those returned by labelmatrix, bwlabel, bwlabeln, or watershed,
into an RGB color image for the purpose of visualizing the labeled regions. The label2rgb function determines the color to
assign to each object based on the number of objects in the label matrix and range of colors in the colormap.
The label2rgb function picks colors from the entire range.
RGB = label2rgb(L, map) defines the colormap map to be used in the RGB image.map can have any of the following values:
String containing the name of a MATLAB colormap function, such as 'jet' or'gray' (See colormap for a list of
®
supported colormaps.)
RGB = label2rgb(L, map, zerocolor) defines the RGB color of the elements labeled 0 (zero) in the input label matrix L. As
the value of zerocolor, specify an RGB triple or one of the strings listed in this table.
Value Color
'b' Blue
'c' Cyan
'g' Green
'k' Black
'm' Magenta
'r' Red
'w' White
'y' Yellow
If you do not specify zerocolor, the default value for zero-labeled elements is [1 1 1](white).
RGB = label2rgb(L, map, zerocolor, order) controls how label2rgb assigns colormap colors to regions in the label matrix.
If order is 'noshuffle' (the default),label2rgb assigns colormap colors to label matrix regions in numerical order.
If orderis 'shuffle', label2rgb assigns colormap colors pseudorandomly.
Class Support
The input label matrix L can have any numeric class. It must contain finite, nonnegative integers. The output of label2rgb is of
class uint8.
Examples
collapse all
I = imread('rice.png');
figure, imshow(I)
BW = im2bw(I, graythresh(I));
CC = bwconncomp(BW);
L = labelmatrix(CC);
Convert the label matrix into RGB image, using default settings.
RGB = label2rgb(L);
figure, imshow(RGB)
More About
collapse all
Tips
This function supports the generation of C code using MATLAB Coder™. For more information, see Code Generation
for Image Processing.
When generating code, for best results when using the standard syntax RGB = label2rgb(L, map, zerocolor, order):
o Submit at least two input arguments: the label matrix, L, and the colormap matrix, map.
o map must be an n-by-3, double, colormap matrix. You cannot use a string containing the name of a
MATLAB colormap function or a function handle of a colormap function.
o If you set the boundary color zerocolor to the same color as one of the regions, label2rgb will not issue a
warning.
demosaic
Syntax
Description
RGB = demosaic(I, sensorAlignment) converts a Bayer pattern encoded image to a truecolor image using gradient-corrected
linear interpolation. I is an M-by-N array of intensity values that are Bayer pattern encoded. I must have at least 5 rows and 5
columns.
A Bayer filter mosaic, or color filter array, refers to the arrangement of color filters that let each sensor in a single-sensor digital
camera record only red, green, or blue data. The patterns emphasize the number of green sensors to mimic the human eye's
greater sensitivity to green light. The demosaic function uses interpolation to convert the two-dimensional Bayer-encoded image
into the truecolor image, RGB, which is an M-by-N-by-3 array.
sensorAlignment is one of the following text strings that specifies the Bayer pattern. Each string represents the order of the red,
green, and blue sensors by describing the four pixels in the upper-left corner of the image (left-to-right, top-to-bottom).
Value 2–by-2 Sensor Alignment
'gbrg'
'grbg'
'bggr'
'rggb'
Class Support
I can be uint8 or uint16, and it must be real. RGB has the same class as I.
Examples
collapse all
Convert a Bayer pattern encoded image that was photographed by a camera with a sensor alignment of 'bggr' .
I = imread('mandi.tif');
J = demosaic(I,'bggr');
imshow(I);
figure, imshow(J);
imquantize
Quantize image using specified quantization levels and output values
Syntax
quant_A = imquantize(A,levels)example
quant_A = imquantize(___,values)example
[quant_A,index] = imquantize(___)example
Description
example
quant_A = imquantize(A,levels) quantizes image A using specified quantization values contained in the N element
vector levels. Output image quant_A is the same size as A and contains N + 1discrete integer values in the
range 1 to N + 1 which are determined by the following criteria:
Note that imquantize assigns values to the two implicitly defined end intervals:
A(k) ≤ levels(1)
example
example
quant_A = values(index)
Examples
collapse all
thresh = multithresh(I,2);
Segment the image into three levels using imquantize .
seg_I = imquantize(I,thresh);
Convert segmented image into color image using label2rgb and display it.
RGB = label2rgb(seg_I);
figure;
imshow(RGB)
axis off
title('RGB Segmented Image')
I = imread('peppers.png');
imshow(I)
axis off
title('RGB Image');
Generate thresholds for seven levels from the entire RGB image.
threshRGB = multithresh(I,7);
Generate thresholds for each plane of the RGB image.
threshForPlanes = zeros(3,7);
for i = 1:3
threshForPlanes(i,:) = multithresh(I(:,:,i),7);
end
Process the entire image with the set of threshold values computed from entire image.
for i = 1:3
value = [0 threshForPlanes(i,2:end) 255];
quantPlane(:,:,i) = imquantize(I(:,:,i),threshForPlanes(i,:),value);
end
quantPlane = uint8(quantPlane);
Display both posterized images and note the visual differences in the two thresholding schemes.
imshowpair(quantRGB,quantPlane,'montage')
axis off
title('Full RGB Image Quantization Plane-by-Plane Quantization')
To compare the results, calculate the number of unique RGB pixel vectors in each output image. Note that the plane-by-plane
thresholding scheme yields about 23% more colors than the full RGB image scheme.
I = imread('coins.png');
imshow(I)
axis off
title('Grayscale Image')
Split the image into eight levels by obtaining seven thresholds from multithresh.
thresh = multithresh(I,7);
Construct the valuesMax vector such that the maximum value in each quantization interval is assigned to the eight levels of the
output image.
Similarly, construct the valuesMin vector such that the minimum value in each quantization interval is assigned to the eight
levels of the output image. Instead of calling imquantize again with the vector valuesMin , use the output argument index to
assign those values to the output image.
imshowpair(quant8_I_min,quant8_I_max,'montage')
title('Minimum Interval Value Maximum Interval Value')
Input Arguments
collapse all
A — Input imageimage
Input image, specified as a numeric array of any dimension.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 |uint32 | uint64
levels — Quantization levelsvector
Quantization levels, specified as an N element vector. Values of the discrete quantization levels must be in monotonically
increasing order.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 |uint32 | uint64
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 |uint32 | uint64
Output Arguments
collapse all
More About
collapse all
Tips
This function supports the generation of C code using MATLAB Coder™. For more information, see Code Generation
®
multithresh
Syntax
thresh = multithresh(A)example
thresh = multithresh(A,N)example
[thresh,metric] = multithresh(___)example
Description
example
thresh = multithresh(A) returns the single threshold value threshcomputed for image A using Otsu's method. You can
use thresh as an input argument to imquantize to convert an image into a two-level image.
example
thresh = multithresh(A,N) returns thresh a 1-by-N vector containing N threshold values using Otsu's method. You can
use threshas an input argument to imquantize to convert image A into an image with N + 1 discrete levels.
example
[thresh,metric] = multithresh(___) returns metric, a measure of the effectiveness of the computed thresholds. metric is in the
range[0 1] and a higher value indicates greater effectiveness of the thresholds in separating the input image into N + 1 regions
based on Otsu's objective criterion.
Examples
collapse all
I = imread('coins.png');
imshow(I)
Calculate a single threshold value for the image.
level = multithresh(I);
Segment the image into two regions using imquantize , specifying the threshold level returned by multithresh .
seg_I = imquantize(I,level);
figure
imshow(seg_I,[])
I = imread('circlesBrightDark.png');
imshow(I)
axis off
title('Original Image')
Calculate two threshold levels.
thresh = multithresh(I,2);
Segment the image into three levels using imquantize .
seg_I = imquantize(I,thresh);
Convert segmented image into color image using label2rgb and display it.
RGB = label2rgb(seg_I);
figure;
imshow(RGB)
axis off
title('RGB Segmented Image')
I = imread('peppers.png');
imshow(I)
axis off
title('RGB Image');
Generate thresholds for seven levels from the entire RGB image.
threshRGB = multithresh(I,7);
Generate thresholds for each plane of the RGB image.
threshForPlanes = zeros(3,7);
for i = 1:3
threshForPlanes(i,:) = multithresh(I(:,:,i),7);
end
Process the entire image with the set of threshold values computed from entire image.
quantPlane = uint8(quantPlane);
Display both posterized images and note the visual differences in the two thresholding schemes.
imshowpair(quantRGB,quantPlane,'montage')
axis off
title('Full RGB Image Quantization Plane-by-Plane Quantization')
To compare the results, calculate the number of unique RGB pixel vectors in each output image. Note that the plane-by-plane
thresholding scheme yields about 23% more colors than the full RGB image scheme.
I = imread('circlesBrightDark.png');
Find all unique grayscale values in image.
uniqLevels = unique(I(:));
Nvals = [1 2 4 8];
for i = 1:length(Nvals)
[thresh, metric] = multithresh(I, Nvals(i) );
disp(['N = ' int2str(Nvals(i)) ' | metric = ' num2str(metric)]);
end
N = 1 | metric = 0.54767
N = 2 | metric = 0.98715
N = 4 | metric = 0.99648
N = 8 | metric = 0.99902
Apply the set of 8 threshold values to obtain a 9-level segmentation using imquantize .
seg_Neq8 = imquantize(I,thresh);
uniqLevels = unique( seg_Neq8(:) )
uniqLevels =
1
2
3
4
5
6
7
8
9
Threshold the image using seg_Neq8 as an input to multithresh. Set N equal to 8, which is 1 less than the number of levels in
this segmented image. multithreshreturns a metric value of 1.
Columns 1 through 7
Column 8
8.1216
metric =
Threshold the image again, this time increasing the value of N by 1. This value now equals the number of levels in the image.
Note how the input is degenerate because the number of levels in the image is too few for the number of requested thresholds.
Hence, multithresh returns a metric value of 0.
thresh =
1 2 3 4 5 6 7 8 9
metric =
0
Input Arguments
collapse all
multithresh uses the range of the input image A, [min(A(:)) max(A(:))], as the limits for computing the histogram used in
subsequent computations.multithresh ignores any NaNs in computation. Any Infs and -Infs are counted in the first and last bin
of the histogram, respectively.
For degenerate inputs where the number of unique values in A is less than or equal to N, there is no viable solution using Otsu's
method. For such inputs, the return valuethresh contains all the unique values from A and possibly some extra values that are
chosen arbitrarily.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 |uint32 | uint64
Output Arguments
collapse all
These thresholds are in the same range as the input image A, unlike thegraythresh function, which returns a normalized
threshold in the range [0, 1].
metric — Measure of the effectiveness of the thresholdsscalar
Measure of the effectiveness of the thresholds, returned as a scalar value. Higher values indicates greater effectiveness of the
thresholds in separating the input image into N+1 classes based on Otsu's objective criterion. For degenerate inputs where the
number of unique values in A is less than or equal to N, metric equals 0.
More About
collapse all
Tips
This function supports the generation of C code using MATLAB Coder™. Note that if you choose the
®
generic MATLAB Host Computer target platform, the function 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. For more information, seeUnderstanding Code Generation with Image Processing Toolbox.
If the input image is not a grayscale image, im2bw converts the input image to grayscale, and then converts this grayscale image
to binary by thresholding.
Class Support
The input image can be of class uint8, uint16, single, int16, or double, and must be nonsparse. The output image BW is of
class logical. I and X must be 2-D. RGB images are M-by-N-by-3.
Examples
collapse all
load trees
BW = im2bw(X,map,0.4);
imshow(X,map), figure, imshow(BW)
graythresh
Syntax
level = graythresh(I)
[level EM] = graythresh(I)
Description
level = graythresh(I) computes a global threshold (level) that can be used to convert an intensity image to a binary image
with im2bw. level is a normalized intensity value that lies in the range [0, 1].
The graythresh function uses Otsu's method, which chooses the threshold to minimize the intraclass variance of the black and
white pixels.
Multidimensional arrays are converted automatically to 2-D arrays using reshape. Thegraythresh function ignores any nonzero
imaginary part of I.
[level EM] = graythresh(I) returns the effectiveness metric, EM, as the second output argument. The effectiveness metric is a
value in the range [0 1] that indicates the effectiveness of the thresholding of the input image. The lower bound is attainable only
by images having a single gray level, and the upper bound is attainable only by two-valued images.
Class Support
The input image I can be of class uint8, uint16, int16, single,or double and it must be nonsparse. The return value level is a
double scalar. The effectiveness metric EM is a double scalar.
Examples
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
imshow(BW)
grayslice
Syntax
X = grayslice(I, n)
Description
X = grayslice(I, n) thresholds the intensity image I returning an indexed image inX. grayslice uses the threshold values:
X = grayslice(I, v) thresholds the intensity image I using the values of v, where vis a vector of values between 0 and 1,
returning an indexed image in X.
You can view the thresholded image using imshow(X,map) with a colormap of appropriate length.
Class Support
The input image I can be of class uint8, uint16, int16, single, or double, and must be nonsparse. Note that the threshold values
are always between 0 and 1, even if I is of class uint8 or uint16. In this case, each threshold value is multiplied by 255 or 65535
to determine the actual threshold to use.
The class of the output image X depends on the number of threshold values, as specified by n or length(v). If the number of
threshold values is less than 256, then X is of class uint8, and the values in X range from 0 to n or length(v). If the number of
threshold values is 256 or greater, X is of class double, and the values in X range from 1 to n+1 or length(v)+1.
Examples
I = imread('snowflakes.png');
X = grayslice(I,16);
imshow(I)
figure, imshow(X,jet(16))
im2double
Syntax
I2 = im2double(I)example
I2 = im2double(I,'indexed')
Description
example
I2 = im2double(I) converts the intensity image I to double precision, rescaling the data if necessary. I can be a grayscale
intensity image, a truecolor image, or a binary image.
If the input image is of class double, then the output image is identical.
I2 = im2double(I,'indexed') converts the indexed image I to double precision, offsetting the data if necessary.
Examples
collapse all
I2 = im2double(I);
whos I2
Name Size Bytes Class Attributes
I1 = gpuArray(reshape(uint8(linspace(1,255,25)),[5 5]));
I2 = im2double(I1);
Input Arguments
collapse all
If the Parallel Computing Toolbox is installed, I can be a gpuArray and im2doubleconverts I on a GPU.
im2int16
I2 = im2int16(I)
RGB2 = im2int16(RGB)
I = im2int16(BW)
gpuarrayB = im2int16(gpuarrayA,___)
Description
I2 = im2int16(I) converts the intensity image I to int16, rescaling the data if necessary. If the input image is of class int16, the
output image is identical to it.
RGB2 = im2int16(RGB) converts the truecolor image RGB to int16, rescaling the data if necessary.
I = im2int16(BW) converts the binary image BW to an int16 intensity image, changing false-valued elements to -32768 and
true-valued elements to 32767.
gpuarrayB = im2int16(gpuarrayA,___) performs the conversion on a GPU. The input image and output image are gpuArrays.
This syntax requires the Parallel Computing Toolbox™.
Class Support
Intensity and truecolor images can be uint8, uint16, int16, single, double, orlogical. Binary images must be logical. The
output image is int16.
Intensity and truecolor gpuArray images can be uint8, uint16, int16, logical,single, or double. Binary gpuArray images must
be logical. The output gpuArray image is int16.
Examples
I = reshape(linspace(0,1,20),[5 4])
I2 = im2int16(I)
I1 =
I1 = gpuArray(reshape(linspace(0,1,20),[5 4]))
I2 = im2int16(I1)
im2java2d
Syntax
jimage = im2java2d(I)
jimage = im2java2d(X,MAP)
Description
jimage = im2java2d(I) converts the image I to an instance of the Java image classjava.awt.image.BufferedImage. The
image I can be an intensity (grayscale), RGB, or binary image.
jimage = im2java2d(X,MAP) converts the indexed image X with colormap MAP to an instance of the Java
class java.awt.image.BufferedImage.
Intensity, indexed, and RGB input images can be of class uint8, uint16, or double. Binary input images must be of
class logical.
Examples
Read an image into the MATLAB workspace and then use im2java2d to convert it into an instance of the Java
®
class java.awt.image.BufferedImage.
I = imread('moon.tif');
javaImage = im2java2d(I);
frame = javax.swing.JFrame;
icon = javax.swing.ImageIcon(javaImage);
label = javax.swing.JLabel(icon);
frame.getContentPane.add(label);
frame.pack
frame.show
im2single
Syntax
I2 = im2single(I)
RGB2 = im2single(RGB)
I = im2single(BW)
X2 = im2single(X,'indexed')
gpuarrayB = im2single(gpuarrayA,___)
Description
I2 = im2single(I) converts the intensity image I to single, rescaling the data if necessary. If the input image is of class single,
the output image is identical to it.
RGB2 = im2single(RGB) converts the truecolor image RGB to single, rescaling the data if necessary.
X2 = im2single(X,'indexed') converts the indexed image X to single precision, offsetting the data if necessary.
gpuarrayB = im2single(gpuarrayA,___) performs the conversion on a GPU. The input image and the output image are
gpuArrays. This syntax requires the Parallel Computing Toolbox™.
Class Support
Intensity and truecolor images can be uint8, uint16, int16, logical,single, ordouble. Indexed images can
be uint8, uint16, double or logical. Binary input images must be logical. The output image is single.
If input gpuArray gpuarrayA is an intensity and truecolor image, it can be uint8, uint16,int16, logical,single, or double.
If gpuarrayA is an indexed image, it can be uint8,uint16, double or logical. If gpuarrayA is a binary image, it must
be logical. The output gpuArray image is single.
Examples
collapse all
I = reshape(uint8(linspace(1,255,25)),[5 5])
I=
I2 = im2single(I)
I2 =
I2 = im2single(I)
I2 =
im2uint16
Syntax
I2 = im2uint16(I)
RGB2 = im2uint16(RGB)
I = im2uint16(BW)
X2 = im2uint16(X,'indexed')
gpuarrayB = im2uint16(gpuarrayA,___)
Description
I2 = im2uint16(I) converts the intensity image I to uint16, rescaling the data if necessary. If the input image is of class uint16,
the output image is identical to it.
RGB2 = im2uint16(RGB) converts the truecolor image RGB to uint16, rescaling the data if necessary.
I = im2uint16(BW) converts the binary image BW to a uint16 intensity image, changing 1-valued elements to 65535.
X2 = im2uint16(X,'indexed') converts the indexed image X to uint16, offsetting the data if necessary. If X is of
class double, max(X(:)) must be 65536 or less.
gpuarrayB = im2uint16(gpuarrayA,___) performs the conversion on a GPU. The input image and the output image are
gpuArrays. This syntax requires the Parallel Computing Toolbox™.
Code Generation
im2uint16 supports the generation of efficient, production-quality C/C++ code from MATLAB. Generated code for this function
uses a precompiled platform-specific shared library. To see a complete list of toolbox functions that support code generation,
see List of Supported Functions with Usage Notes.
Class Support
Intensity and truecolor images can be uint8, uint16, double, logical, single, orint16. Indexed images can
be uint8, uint16, double, or logical. Binary input images must be logical. The output image is uint16.
If the input gpuArray gpuarrayA is an intensity or truecolor image, it can be uint8,uint16, int16, logical, single, or double.
If gpuarrayA is an indexed image, it can be uint8, uint16, double or logical. If gpuarrayA is a binary image, it must
belogical. The output gpuArray image is uint16.
Examples
I1 = reshape(linspace(0,1,20),[5 4])
I2 = im2uint16(I1)
I1 =
I2 =
I1 = gpuArray(reshape(linspace(0,1,20),[5 4]))
I2 = im2uint16(I1)
im2uint8
Syntax
I2 = im2uint8(I)example
RGB2 = im2uint8(RGB)
I = im2uint8(BW)
X2 = im2uint8(X,'indexed')
gpuarrayB = im2uint8(gpuarrayA,___)example
Description
example
I2 = im2uint8(I) converts the grayscale image I to uint8. If the input image is of class uint8, the output image is identical to the
input image. If the input image is not uint8, im2uint8 returns the equivalent image of class uint8, rescaling or offsetting the data
as necessary.
RGB2 = im2uint8(RGB) converts the truecolor image RGB to uint8, rescaling the data if necessary.
I = im2uint8(BW) converts the binary image BW to a uint8 grayscale image, changing 1-valued elements to 255.
X2 = im2uint8(X,'indexed') converts the indexed image X to uint8, offsetting the data if necessary. Note that it is not always
possible to convert an indexed image to uint8. If X is of class double, the maximum value of X must be 256 or less. If X is of
class uint16, the maximum value of X must be 255 or less.
example
gpuarrayB = im2uint8(gpuarrayA,___) performs the conversion on a GPU. The input image, gpuarrayA, can be a grayscale,
truecolor, binary, or indexed gpuArray image. The output image is a gpuArray. This syntax requires the Parallel Computing
Toolbox™.
Examples
collapse all
Convert uint16 Array to uint8
I1 = reshape(uint16(linspace(0,65535,25)),[5 5])
I1 =
I2 = im2uint8(I1)
I2 =
I1 = gpuArray(reshape(uint16(linspace(0,65535,25)),[5 5]))
Convert array to uint8.
I2 = im2uint8(I1);
Input Arguments
collapse all
Example: I = imread('cameraman.tif');
Example: BW = imread('text.png');
Example: I = gpuArray(imread('cameraman.tif'));
Output Arguments
collapse all