imread
Read image from graphics file
Syntax
Description
Examples
Read and Display Image
Read a sample image.
A = imread('ngc6543a.jpg');
imread
returns a 650-by-600-by-3 array,A
。
Display the image.
image(A)
Convert Indexed Image to RGB
Read the first image in the sample indexed image file,玉米
。
[X,cmap] = imread('corn.tif');
这indexed imageX
是a 415-by-312 array of typeuint8
。结肠cmap
是a 256-by-3 matrix of typedouble
, therefore there are 256 colors in the indexed image. Display the image.
imshow(x,cmap)
Convert the indexed image to an RGB image. The result is a 415-by-312-by-3 array of typedouble
。
RGB = Ind2RGB(X,CMAP);
Check that values of the RGB image are in the range [0, 1].
disp(['RGB图像的范围是[,num2str(min(RGB(:))),', ',num2str(max(RGB(:))),']。])
RGB图像的范围为[0.0078431,0.97647]。
Read Specific Image in Multipage TIFF File
Return Alpha Channel of PNG Image
返回样品图像的α通道,peppers.png
。
[x,map,alpha] = imread('peppers.png');谁是alpha
Name Size Bytes Class Attributes alpha 0x0 0 double
没有α频道,所以alpha
是空的。
Read Specified Region of TIFF Image
阅读样品图像的像素的特定区域,玉米
。
指定'PixelRegion'
具有向量的单元格数组的参数,指示要读取区域的边界。第一个向量指定要读取的行的范围,第二个向量指定要读取的列范围。
A = imread('corn.tif','PixelRegion',{[1,2],[2,5]});
imread
从1-2行读取图像数据,并从第2-5列中读取图像数据玉米
和returns the 2-by-4 array,A
。
输入参数
filename
—Name of graphics file
character vector|string scalar
Name of graphics file, specified as a character vector or string scalar.
Depending on the location of your file,filename
可以采用这些形式之一。
Location |
Form |
||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder or folder on the MATLAB®path | 在 Example: |
||||||||
File in a folder |
如果是file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name. Example: Example: |
||||||||
URL | 如果文件是由Internet URL定位的,则 Example: |
||||||||
Remote Location |
如果文件存储在远程位置,则
Based on the remote location,
有关更多信息,请参阅Work with Remote Data。 Example: |
有关每个文件类型支持的位深度,压缩方案和颜色空间的信息,请参见万博1manbetx算法。
Data Types:char
|string
fmt
—图像格式
character vector|string scalar
图像格式, specified as a character vector or string scalar indicating the standard file extension. Callimformats
查看支持格式及其文件扩展名的列表。万博1manbetx
Example:'PNG'
Data Types:char
|string
idx
—Image to read
整数标量|vector of integers
Image to read, specified as an integer scalar or, for GIF files, a vector of integers. For example, ifidx
是3,然后imread
returns the third image in the file. For a GIF file, ifidx
是1:5
, thenimread
returns only the first five frames. Theidx
仅针对多图像GIF,CU万博1manbetxR,ICO和HDF4文件支持参数。
When reading multiple frames from the same GIF file, specifyidx
作为帧的向量或使用'Frames','all'
name-value pair argument. Because of the way that GIF files are structured, these syntaxes provide faster performance compared to callingimread
in a loop.
For HDF4 files,idx
corresponds to the reference number of the image to read. Reference numbers do not necessarily correspond to the order of the images in the file. You can useimfinfo
将图像顺序与参考号匹配。
Example:3
Data Types:double
Name-Value Arguments
将可选的参数对Name1=Value1,...,NameN=ValueN
, whereName
是the argument name and价值
是the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and encloseName
in quotes.
Example:'指数',5
reads the fifth image of a TIFF file.
帧
—Frame to read
1(default) |positive integer|vector of integers|'全部'
帧to read, specified as the comma-separated pair consisting of'Frames'
和a positive integer, a vector of integers, or'全部'
。例如,if you specify the value 3,imread
读取文件中的第三个帧。如果指定'全部'
, thenimread
reads all frames and returns them in the order in which they appear in the file.
Example:'frames',5
PixelRegion
—Subimage to read
单元阵列in the form{rows,cols}
Subimage to read, specified as the comma-separated pair consisting of'PixelRegion'
和a cell array of the form{rows,cols}
。这行
input specifies the range of rows to read. Thecols
输入指定要读取的列范围。Both行
和cols
must be two-element vectors containing 1-based indices. For example,'PixelRegion',{[1 2],[3 4]}
读取图像数据中的第1和2行和第3和4列的子图像。如果是'ReductionLevel'
value is greater than 0, then行
和cols
are coordinates of the subimage.
Example:'PixelRegion',{[1 100],[4 500]}
ReductionLevel
—减少图像分辨率
0(default) |nonnegative integer
减少图像分辨率,指定为逗号分隔对'ReductionLevel'
和a nonnegative integer. For reduction levelL,图像分辨率减少了2^的系数L。这reduction level is limited by the total number of decomposition levels as specified by the'WaveletDecompositionLevels'
输出中的字段imfinfo
function.
Example:'ReductionLevel',5
Data Types:single
|double
V79Compatible
—Compatibility with MATLAB 7.9 (R2009b) and earlier
false
(default) |真的
Compatibility with MATLAB 7.9 (R2009b) and earlier, specified as the comma-separated pair consisting of'V79Compatible'
和either真的
或者false
。如果指定真的
, then the returned grayscale or RGB image is consistent with previous versions ofimread
(MATLAB 7.9(R2009b)及以更早)。
Example:“ v79 compatible',是真的
Data Types:logical
BackgroundColor
—背景颜色
'none'
|整数|3-element vector of integers
背景颜色, specified as'none'
, an integer, or a three-element vector of integers. IfBackgroundColor
是'none'
, thenimread
不执行任何合成。否则,imread
blends transparent pixels with the background color.
如果是input image is indexed, then the value of
BackgroundColor
必须是该范围内的整数[1,P]
, whereP
是the colormap length.如果是input image is grayscale, then the value of
BackgroundColor
必须是该范围内的整数[0,1]
。如果是input image is RGB, then the value of
BackgroundColor
必须三元素向量values in the range[0,1]
。
的默认值BackgroundColor
depends on the presence of the透明度
output argument and the image type:
If you request the
透明度
输出参数,然后BackgroundColor
是'none'
。If you do not request the
透明度
output and the PNG file contains a background color chunk, then that color is the default value forBackgroundColor
。If you do not request the
透明度
output and the file does not contain a background color chunk, then the default value forBackgroundColor
是1
for indexed images,0
for grayscale images, and[0 0 0]
用于TrueColor(RGB)图像。
Index
—Image to read
1(default) |positive integer
Image to read, specified as the comma-separated pair consisting of'指数'
和a positive integer. For example, if the value ofIndex
是3,然后imread
读取文件中的第三张图像。
Data Types:single
|double
信息
—有关图像的信息
结构ure array
有关图像的信息,指定为逗号分隔对'Info'
和一个结构阵列由imfinfo
function. Use the信息
名称值对参数有帮助imread
locate the images in a multi-image TIFF file more quickly.
Data Types:结构
PixelRegion
—Region boundary
单元阵列
区域边界,指定为逗号分隔对'PixelRegion'
和a cell array of the form{rows,cols}
。这行
input specifies the range of rows to read. Thecols
输入指定要读取的列范围。行
和cols
必须是1基索引的两元素或三元素向量。两个元素向量指定要读取的第一行或最后一行或列。例如,'PixelRegion',{[1 2],[3 4]}
在图像数据中读取以第1和2行和第3和4列界定的区域。
A three-element vector must be in the form[start increment stop]
, wherestart
是the first row or column to read,increment
是an incremental value, andstop
是the last row or column to read. This syntax allows image downsampling. For example,'pixelRegion',{[1 2 10],[4 3 12]}
reads the region bounded by rows 1 and 10 and columns 4 and 12, and samples data from every 2 pixels in the vertical direction, and every 3 pixels in the horizontal direction.
Example:'PixelRegion',{[1 100],[4 500]}
Data Types:细胞
Output Arguments
A
— Image data
array
Image data, returned as an array.
如果是file contains a grayscale image, then A is an
m
-by-n
大批。如果是file contains an indexed image, then A is an
m
-by-n
array of index values corresponding to the color at that index in地图
。如果是file contains a truecolor image, then A is an
m
-by-n
-by-3 array.如果是file is a TIFF file containing color images that use the CMYK color space, then A is an
m
-by-n
-by-4 array.
班级A
取决于图像格式和图像数据的位深度。有关更多信息,请参阅算法
地图
— Colormap
m
-by-3 matrix
Colormap associated with the indexed image data inA
, returned as anm
-by-3 matrix of classdouble
。
透明度
— Transparency information
matrix
Transparency information, returned as a matrix. For PNG files,透明度
是the alpha channel, if present. If no alpha channel is present, or if you specify the'BackgroundColor'
name-value pair argument, then透明度
是空的。For CUR and ICO files,透明度
是the AND mask. For cursor files, this mask sometimes contains the only useful data.
More About
Bit Depth
Bit depth is the number of bits used to represent each image pixel.
Bit depth is calculated by multiplying the bits-per-sample with the samples-per-pixel. Thus, a format that uses 8 bits for each color component (or sample) and three samples per pixel has a bit depth of 24. Sometimes the sample size associated with a bit depth can be ambiguous. For example, does a 48-bit bit depth represent six 8-bit samples, four 12-bit samples, or three 16-bit samples? See算法for sample size information to avoid this ambiguity.
算法
For most image file formats,imread
使用每个彩色平面8位或更少的位来存储图像像素。此表列出了返回的图像数组的类A
, for the bit depths used by the file formats.
文件中的位深度 |
返回的数组类 |
---|---|
1 bit per pixel |
|
2 to 8 bits per color plane |
|
每个像素9至16位 |
对于16位BMP包装格式(5-6-5),MATLAB返回 |
这following sections provide information about the support for specific formats, listed in alphabetical order by format name.
BMP—视窗Bitmap
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | No Compression | RLE Compression | 输出类 | Notes |
---|---|---|---|---|
1 bit | ✓ | – | logical |
|
4位 | ✓ | ✓ | uint8 |
|
8 bit | ✓ | ✓ | uint8 |
|
16 bit | ✓ | – | uint8 |
1个样品/像素 |
24 bit | ✓ | – | uint8 |
3 samples/pixel |
32 bit | ✓ | – | uint8 |
3 samples/pixel (1 byte padding) |
CUR — Cursor File
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | No Compression | 压缩 | 输出类 |
---|---|---|---|
1 bit | ✓ | – | logical |
4位 | ✓ | – | uint8 |
8 bit | ✓ | – | uint8 |
Note
By default, Microsoft®视窗®cursors are 32-by-32 pixels. Since MATLAB pointers must be 16-by-16, you might need to scale your image. You can use theimresize
function for this operation.
GIF — Graphics Interchange Format
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | No Compression | 压缩 | 输出类 |
---|---|---|---|
1 bit | ✓ | – | logical |
2 bit to 8 bit | ✓ | – | uint8 |
HDF4 — Hierarchical Data Format
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | Raster Image with colormap | Raster image without colormap | 输出类 | Notes |
---|---|---|---|---|
8 bit | ✓ | ✓ | uint8 |
|
24 bit | – | ✓ | uint8 |
3 samples/pixel |
ICO - 图标文件
JPEG — Joint Photographic Experts Group
imread
reads any baseline JPEG image, as well as JPEG images with some commonly used extensions. For information on JPEG 2000 file support, seeJPEG 2000。
万博1manbetx每个样品支撑位 | Lossy Compression | Lossless Compression | 输出类 | Notes |
---|---|---|---|---|
8 bit | ✓ | ✓ | uint8 |
灰度或RGB |
12 bit | ✓ | ✓ | uint16 |
灰度或RGB |
16 bit | – | ✓ | uint16 |
灰度 |
JPEG 2000 — Joint Photographic Experts Group 2000
For information about JPEG files, seeJPEG。
Note
Indexed JPEG 2000 images are not supported. Only JP2 compatible color spaces are supported for JP2/JPX files. By default, all image channels are returned in the order they are stored in the file.
万博1manbetx每个样品支撑位 |
Lossy Compression | Lossless Compression | 输出类 | Notes |
---|---|---|---|---|
1 bit | ✓ | ✓ | logical |
灰度only |
2 bit to 8 bit | ✓ | ✓ | uint8 或者int8 |
灰度 或者RGB |
9 bit to 16 bit | ✓ | ✓ | uint16 或者int16 |
灰度 或者RGB |
PBM - 便携式位图
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | 原始二进制 | ASCII(平原)Encoded | 输出类 |
---|---|---|---|
1 bit | ✓ | ✓ | logical |
PCX —视窗Paintbrush
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | 输出类 | Notes |
---|---|---|
1 bit | logical |
灰度only |
8 bit | uint8 |
灰度或者indexed |
24 bit | uint8 |
RGB Three 8-bit samples/pixel |
PGM - 便携式灰色斑块
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | 原始二进制 | ASCII(平原)Encoded | 输出类 | Notes |
---|---|---|---|---|
8 bit | ✓ | – | uint8 |
|
16 bit | ✓ | – | uint16 |
|
Arbitrary | – | ✓ | 1位至8位:uint8 9位至16位: uint16 |
值缩放 |
PNG — Portable Network Graphics
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | 输出类 | Notes |
---|---|---|
1 bit | logical |
灰度 |
2 bit | uint8 |
灰度 |
4位 | uint8 |
灰度 |
8 bit | uint8 |
灰度或者Indexed |
16 bit | uint16 |
灰度或者Indexed |
24 bit | uint8 |
RGB Three 8-bit samples/pixel. |
48 bit | uint16 |
RGB 三个16位样品/像素。 |
PPM — Portable Pixmap
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | 原始二进制 | ASCII(平原)Encoded | 输出类 |
---|---|---|---|
Up to 16 bit | ✓ | – | uint8 |
Arbitrary | – | ✓ |
RAS —SunRaster
This table lists supported bit depths and the data type of the output image data array.
Supported Bit Depths | 输出类 | Notes |
---|---|---|
1 bit | logical |
Bitmap |
8 bit | uint8 |
Indexed |
24 bit | uint8 |
RGB Three 8-bit samples/pixel |
32 bit | uint8 |
带有alpha的RGB Four 8-bit samples/pixel |
Aperio SVS -AperioScanScopeVirtual Slide
tiff-based image file format.imread
supports reading uncompressed and compressed images, including images with JPEG2000 compression. For more information, seeTIFF - 标记的图像文件格式。
TIFF - 标记的图像文件格式
imread
读取大多数由TIFF规范或LIBTI万博1manbetxFF支持的图像。这imread
function supports these TIFF capabilities:
每个像素的任何数量样本
CCITT第3和4组传真,包装,JPEG,LZW,Deflate,Thunderscan压缩和未压缩图像
Logical, grayscale, indexed color, truecolor and hyperspectral images
RGB, CMYK, CIELAB, ICCLAB color spaces. If the color image uses the CMYK color space,
A
是anm
-by-n
-by-4 array. To determine which color space is used, useimfinfo
要获取有关图形文件的信息,并查看PhotometricInterpretation
field. If a file contains CIELAB color data,imread
converts it to ICCLAB before bringing it into the MATLAB workspace. This conversion is necessary because 8-bit or 16-bit TIFF CIELAB-encoded values use a mixture of signed and unsigned data types that cannot be represented as a single MATLAB array.组织成瓷砖或扫描线的数据
imread
读取和转换TIFF图像如下:
YCbCr images are converted into the RGB colorspace.
所有灰度图像都读成黑色=
0
, white = largest value.1位图像作为课程返回
logical
。16-bit floating-point images are returned as class
single
。CIELab images are converted into ICCLab colorspace.
For copyright information, open thelibtiffcopyright.txt
file.
XWD — X Window Dump
该表列出了XWD文件的受支持的位深度,压缩万博1manbetx和输出类。
Supported Bit Depths | ZpixMaps | XYBitmaps | XYPixmaps | 输出类 |
---|---|---|---|---|
1 bit | ✓ | – | ✓ | logical |
8 bit | ✓ | – | – | uint8 |
Extended Capabilities
C/C++ Code Generation
使用MATLAB®CODER™生成C和C ++代码。
Usage notes and limitations:
Supports reading of 8-bit JPEG images only. The input argument
filename
必须是有效的绝对路径或相对路径。这个函数使用precompil生成代码ed,特定于平台的共享库(Image Processing Toolbox)。
GPU Code Generation
使用GPU CODER™为NVIDIA®GPU生成CUDA®代码。
Usage notes and limitations:
Supports reading of 8-bit JPEG images only. The input argument
filename
必须是有效的绝对路径或相对路径。这个函数使用precompil生成代码ed,特定于平台的共享库(Image Processing Toolbox)。
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
或者accelerate code with Parallel Computing Toolbox™螺纹池
。
Usage notes and limitations:
imread
does not support reading Hierarchical Data Format (HDF), SVS, or TIFF files when you runimread
在背景中使用backgroundPool
或并行计算工具箱™螺纹池
。
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择一个网站
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:。
您还可以从以下列表中选择一个网站:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
美洲
- América Latina(Español)
- 加拿大(English)
- 美国(English)
欧洲
- Netherlands(English)
- 挪威(English)
- Österreich(德意志)
- 葡萄牙(English)
- Sweden(English)
- 瑞士
- United Kingdom(English)