Main Content

imread

Read image from graphics file

Description

example

A= imread(filename)reads the image from the file specified byfilename, inferring the format of the file from its contents. Iffilename是a multi-image file, thenimread读取第一个图像文件。

A= imread(filename,fmt)additionally specifies the format of the file with the standard file extension indicated byfmt。Ifimreadcannot find a file with the name specified byfilename, it looks for a file namedfilename.fmt

A= imread(___,idx)reads the specified image or images from a multi-image file. This syntax applies only to GIF, PGM, PBM, PPM, CUR, ICO, TIF, SVS, and HDF4 files. You must specify afilenameinput, and you can optionally specifyfmt

example

A= imread(___,名称,价值)specifies format-specific options using one or more name-value pair arguments, in addition to any of the input arguments in the previous syntaxes.

example

[A,地图] = imread(___)reads the indexed image infilenameintoA和reads its associated colormap into地图。Colormap values in the image file are automatically rescaled into the range[0,1]

example

[A,地图,透明度] = imread(___)additionally returns the image transparency. This syntax applies only to PNG, CUR, and ICO files. For PNG files,透明度是the alpha channel, if one is present. For CUR and ICO files, it is the AND (opacity) mask.

Examples

全部收缩

Read a sample image.

A = imread('ngc6543a.jpg');

imreadreturns a 650-by-600-by-3 array,A

Display the image.

image(A)

Figure contains an axes object. The axes object contains an object of type image.

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)

Figure contains an axes object. The axes object contains an object of type image.

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 the third image in the sample file,玉米

[x,map] = imread('corn.tif',3);

返回样品图像的α通道,peppers.png

[x,map,alpha] = imread('peppers.png');谁是alpha
Name Size Bytes Class Attributes alpha 0x0 0 double

没有α频道,所以alpha是空的。

阅读样品图像的像素的特定区域,玉米

指定'PixelRegion'具有向量的单元格数组的参​​数,指示要读取区域的边界。第一个向量指定要读取的行的范围,第二个向量指定要读取的列范围。

A = imread('corn.tif','PixelRegion',{[1,2],[2,5]});

imread从1-2行读取图像数据,并从第2-5列中读取图像数据玉米和returns the 2-by-4 array,A

输入参数

全部收缩

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

filename

Example:'myImage.jpg'

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:'C:\myFolder\myImage.ext'

Example:'\imgDir\myImage.ext'

URL

如果文件是由Internet URL定位的,则filenamemust contain the protocol type such as,http://

Example:'http://hostname/path_to_file/my_image.jpg'

Remote Location

如果文件存储在远程位置,则filename必须包含指定为表格的统一资源定位器(URL)的文件的完整路径:

scheme_name://path_to_file/my_file.ext

Based on the remote location,scheme_name可以是此表中的值之一。

Remote Location scheme_name
Amazon S3™ s3
Windows Azure®斑点存储 wasb,wasbs
HDFS™ hdfs

有关更多信息,请参阅Work with Remote Data

Example:'s3://bucketname/path_to_file/my_image.jpg'

有关每个文件类型支持的位深度,压缩方案和颜色​​空间的信息,请参见万博1manbetx算法

Data Types:char|string

图像格式, specified as a character vector or string scalar indicating the standard file extension. Callimformats查看支持格式及其文件扩展名的列表。万博1manbetx

Example:'PNG'

Data Types:char|string

Image to read, specified as an integer scalar or, for GIF files, a vector of integers. For example, ifidx是3,然后imreadreturns the third image in the file. For a GIF file, ifidx1:5, thenimreadreturns 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 callingimreadin a loop.

For HDF4 files,idxcorresponds 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 encloseNamein quotes.

Example:'指数',5reads the fifth image of a TIFF file.

GIF文件

全部收缩

帧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读取文件中的第三个帧。如果指定'全部', thenimreadreads all frames and returns them in the order in which they appear in the file.

Example:'frames',5

JPEG 2000文件

全部收缩

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输入指定要读取的列范围。Bothcolsmust 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, thencolsare coordinates of the subimage.

Example:'PixelRegion',{[1 100],[4 500]}

减少图像分辨率,指定为逗号分隔对'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'输出中的字段imfinfofunction.

Example:'ReductionLevel',5

Data Types:single|double

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

PNG Files

全部收缩

背景颜色, specified as'none', an integer, or a three-element vector of integers. IfBackgroundColor'none', thenimread不执行任何合成。否则,imreadblends transparent pixels with the background color.

  • 如果是input image is indexed, then the value ofBackgroundColor必须是该范围内的整数[1,P], whereP是the colormap length.

  • 如果是input image is grayscale, then the value ofBackgroundColor必须是该范围内的整数[0,1]

  • 如果是input image is RGB, then the value ofBackgroundColor必须三元素向量values in the range[0,1]

的默认值BackgroundColordepends 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 forBackgroundColor1for indexed images,0for grayscale images, and[0 0 0]用于TrueColor(RGB)图像。

tiffFiles

全部收缩

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

有关图像的信息,指定为逗号分隔对'Info'和一个结构阵列由imfinfofunction. Use the信息名称值对参数有帮助imreadlocate the images in a multi-image TIFF file more quickly.

Data Types:结构

区域边界,指定为逗号分隔对'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

全部收缩

Image data, returned as an array.

  • 如果是file contains a grayscale image, then A is anm-by-n大批。

  • 如果是file contains an indexed image, then A is anm-by-narray of index values corresponding to the color at that index in地图

  • 如果是file contains a truecolor image, then A is anm-by-n-by-3 array.

  • 如果是file is a TIFF file containing color images that use the CMYK color space, then A is anm-by-n-by-4 array.

班级A取决于图像格式和图像数据的位深度。有关更多信息,请参阅算法

Colormap associated with the indexed image data inA, returned as anm-by-3 matrix of classdouble

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.

文件中的位深度

返回的数组类imread

1 bit per pixel

logical

2 to 8 bits per color plane

uint8

每个像素9至16位

uint16(BMP,JPEG,PNG, 和tiff)

对于16位BMP包装格式(5-6-5),MATLAB返回uint8

这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 theimresizefunction 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

imreadreads 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.imreadsupports reading uncompressed and compressed images, including images with JPEG2000 compression. For more information, seeTIFF - 标记的图像文件格式

TIFF - 标记的图像文件格式

imread读取大多数由TIFF规范或LIBTI万博1manbetxFF支持的图像。这imreadfunction 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要获取有关图形文件的信息,并查看PhotometricInterpretationfield. If a file contains CIELAB color data,imreadconverts 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 classsingle

  • CIELab images are converted into ICCLab colorspace.

For copyright information, open thelibtiffcopyright.txtfile.

XWD — X Window Dump

该表列出了XWD文件的受支持的位深度,压缩万博1manbetx和输出类。

Supported Bit Depths ZpixMaps XYBitmaps XYPixmaps 输出类
1 bit logical
8 bit uint8

Extended Capabilities

Version History

在R2006a之前引入